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

Add an option to generate the alias (map, array) as model #1729

Merged
merged 4 commits into from
Dec 31, 2018

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Dec 21, 2018

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.4.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

Add an option (--generate-alias-as-model in CLI or generateAliasAsModel in the config) to generate the alias (map, array) as model.

The option is default to false.

Copy link
Contributor

@demonfiddler demonfiddler left a comment

Choose a reason for hiding this comment

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

Although I've debugged through it many times I don't profess a deep understanding of the code for converting the specification model into Codegen* models. Nevertheless, from what I can see, setting generateAliasAsModel = true ought to restore the previous behaviour of emitting models that are merely aliases for typed maps or arrays.

However, the default setting of generateAliasAsModel = false currently emits code with compilation errors (in the Java case at least). For example, consider an operation that returns an array whose modelled element type A is defined by a Map<String, B> schema in which B is another modelled type. The CodegenOperation has returnBaseType = 'Map' but returnType = 'List<A>', which won't compile because class A is no longer generated. I didn't see any code below to rectify this issue.

@wing328
Copy link
Member Author

wing328 commented Dec 21, 2018

The CodegenOperation has returnBaseType = 'Map' but returnType = 'List', which won't compile because class A is no longer generated. I didn't see any code below to rectify this issue.

If I understand correctly, this issue already exists in the current master so please open an issue to track it instead.

The goal of this PR is to restore the old behaviour to generate alias (map, array) as model. and I believe your use case is to set generateAliasAsModel to true. If the issue mentioned above still occurs, please kindly mention in the new issue created.

@demonfiddler
Copy link
Contributor

Fair enough - the problem with the current behaviour can be seen as a separate issue, which I'll log in due course. In the meantime I'd be happy for the PR to proceed. Many thanks for the rapid turnaround William.

@demonfiddler
Copy link
Contributor

Looks like there's a problem. I just rebased onto upstream/option_generate_alias_model and now I'm getting an NPE in my code because CodegenModel.additionalPropertiesType is no longer being set (to AttributeValue) for the alias type (AuditEvent extends Map<String, AttributeValue>).

@wing328
Copy link
Member Author

wing328 commented Dec 26, 2018

Does your customization include overriding void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) to include the following code?

        codegenModel.additionalPropertiesType = getSchemaType(ModelUtils.getAdditionalProperties(schema));
        addImport(codegenModel, codegenModel.additionalPropertiesType);

If you include this, it should set the codegen model's additionalPropertiesType properly as my local test result has confirmed. Please give it a try when you've time.

@wing328 wing328 merged commit 3ec90a8 into master Dec 31, 2018
@wing328 wing328 deleted the option_generate_alias_model branch December 31, 2018 04:00
@wing328 wing328 mentioned this pull request Jan 2, 2019
4 tasks
@demonfiddler
Copy link
Contributor

Thanks. With the suggested addAdditionPropertiesToCodeGenModel() override, generating from the CLI with the --generate-alias-as-model option now seems to work, and my AuditEvent (and other alias classes) are generated and referenced correctly in the API's CXF implementation class. My only question is: why is it necessary for the generator sub-class to do this in an override? For backwards compatibility shouldn't it be done automatically by DefaultCodegen?

However, the <generateAliasAsModel>true</generateAliasAsModel> Maven plug-in configuration option has no effect. I tried it in both execution/configuration and execution/configuration/configOptions - under the debugger I can see that additionalProperties does not contain the key "generateAliasAsModel" at DefaultCodegen line 213 (if (additionalProperties.containsKey(CodegenConstants.GENERATE_ALIAS_AS_MODEL)) { etc...).

@wing328 wing328 mentioned this pull request Jan 8, 2019
4 tasks
demonfiddler added a commit to demonfiddler/openapi-generator that referenced this pull request Jan 8, 2019
wing328 pushed a commit that referenced this pull request Jan 9, 2019
Fixes the first part of #1729 (comment) (see #1729 (comment)). The second part was fixed by #1845 (commit d65dd76).
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
…ls#1729)

* add option to generate alias as model

* fix issue due to incorrect merge
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
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.

2 participants