Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix python generation when custom files and templates are specified #9572

Merged
merged 2 commits into from
Jun 2, 2021

Conversation

shylasrinivas
Copy link
Contributor

@shylasrinivas shylasrinivas commented May 24, 2021

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@shylasrinivas
Copy link
Contributor Author

@shylasrinivas
Copy link
Contributor Author

Fix for #9570

@@ -938,7 +938,7 @@ private void processUserDefinedTemplates() {
// TODO: initial behavior is "merge" user defined with built-in templates. consider offering user a "replace" option.
if (userDefinedTemplates != null && !userDefinedTemplates.isEmpty()) {
Map<String, SupportingFile> supportingFilesMap = config.supportingFiles().stream()
.collect(Collectors.toMap(TemplateDefinition::getTemplateFile, Function.identity()));
.collect(Collectors.toMap(TemplateDefinition::getTemplateFile, Function.identity(), (oldValue, newValue) -> oldValue));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this lambda doing? Why is it returning the oldValue?
I have no way to tell from this PR if this code works or not.
If this feature is changed in the future, we will not have any test that fails and the feature will break.
How about adding a test of processUserDefinedTemplates to ensure that this feature will keep working?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spacether Absolutely valid points.
Collectors.toMap throws IllegalStateException when dup keys are found, hence the logic is to return a single value when dups are encountered. oldValue is an existing key in the map, whereas newValue is a key found during later iterations of the list (in this case config.supportingFiles()). The code simply retains the existing key (oldValue) in the map when a dup (newValue) is encountered. Based on my testing, I have found oldValue to be user defined templates, which is desired to be retained in the map.
I will look into adding a test. Agreed there is no way to tell if the code works, how do you typically ensure PR quality?

Copy link
Contributor

@spacether spacether May 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining what this code is doing.
Most of our most basic tests are in: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java
We typically add tests that can avoid generating a new output client/server sample if we can. If you have to, then generating a new sample is okay too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spacether Thank you for the pointers. I have added a test, but it looks like the config (line 684 of DefaultCodegenTest.java) I want to specify is not supported in CodegenConfigurator unless, I am missing something. Lmk if the test looks ok. I can remove the commented check based on your input.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for explaining that and adding the test. Your test looks good.

@spacether spacether added this to the 5.2.0 milestone Jun 2, 2021
Copy link
Contributor

@spacether spacether left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR. This looks good

@spacether spacether merged commit 3c866fb into OpenAPITools:master Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] [Python] Adding supporting files and custom package name with Python generator throws exception
2 participants