-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[BUG][Java] Generated API class has wrong dataType and does not compile #5324
Comments
I have determined this issue occurs both with the "okhttp-gson" and "native" libraries; probably with all libraries. |
Narrowing the issue to https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java#L740 If a composed schema 'allOf' has the additionalAttribute set to "true", ModelUtils.isMapSchema() returns true |
@wing328, @jimschubert , what is the rationale for toAllOfName() to return the name of the first element in the 'allOf' list? The reason I ask is so I can submit a PR that addresses the use cases below. One option is I could do something similar to what has been done for "allOf", i.e. introducing a "x-allOf-name" vendor extension. Class inheritance syslog.Policy:
additionalProperties: {}
allOf:
- $ref: '#/components/schemas/policy.AbstractPolicy'
- type: object
required:
- objectType
properties:
localClients:
type: array
items:
type: string In this example, I find it surprising that toAllOfName returns the name of the parent class "policy.AbstractPolicy". I was expecting the return value to be "syslog.Policy". Wouldn't this be a better default value? It's more likely that the name of the outer schema captures the complete semantics of the collection of the inner schemas. A tangential problem is that one OAS author may decide it makes more sense to put the "parent" class first in the allOf children, and another author may think otherwise, that it makes more sense to put it last (or why not somewhere in the middle). I tend to think it makes more sense to put it first, but there is no such requirement in the OAS spec (since the spec doesn't deal with class inheritance, just validation), and I can see how somebody would argue differently. Composition Sailboat:
allOf:
- $ref: '#/components/schemas/Hull'
- $ref: '#/components/schemas/Rigging'
- $ref: '#/components/schemas/Cabin'
- $ref: '#/components/schemas/Sails'
- $ref: '#/components/schemas/Electronics' |
There might be a problem at this line: https://github.com/OpenAPITools/openapi-generator/blame/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L5251. I don't think it handles the case of a ComposedSchema which also defines additionalproperties: true |
@sebastien-rosset Thanks for bring this conversation! Unfortunately, the logic to grab the first item in the array was an original implementation bridging the gap between our 2.3.x branch (when we forked from Swagger Codegen) and the initial implementation of OpenAPI Generator which included minimal OAS 3.0 support. The method you've linked to would allow generators to override the logic specific to the language's support for composition/inheritance and variance. It was originally added in #2046 and is called for a model via I think we'll have better support for inheritance once Justin's PR #4906 is merged. It still leads the question open about the broken composition use case (see #5097, #1095, and #2080 - probably a few others as well). The issue between these two arises in user intentions. For instance, OpenAPI Specification claims that I made a recommendation here to introduce Right now, I'm thinking cc @OpenAPITools/generator-core-team for discussion |
I will read these issues. I was taking notes for myself for the various options to generate code:
a. Flatten properties from all inline schemas into single class/struct.
Sorry I forgot to mention in our case the discriminator for class inheritance is defined in the most abstract class from which all sub-class are descendants.
I think x-codegen-prefer-polymorphism could lead to some ambiguities. See my notes above. For example, should the code generate a class for inlined schema? Should it flatten inlined schema (like it does today)? There are many ways that people can think about how the code is going to be generated. It would be nice to also provide properties to control the output. In my case, I don't mind adding vendor extensions because I'm both the author and also using the codegenerator, so can tweak both sides, but sometimes people use an existing OAS document and they don't really have the choice to modify it (or it's too cumbersome).
|
@sebastien-rosset for your use case with the inline schema and $ref merge, OpenAPI Specification specifically says:
Seems like we'd need an option or vendor extension to allow this behavior as well. |
Like you wrote before, the OAS spec is not clear. Unfortunately the spec mentions both validation and code generation in the introduction, but it never seem to state which sections or sentences apply to code generation. My understanding is the OAS spec is only about validation, and it's completely outside the scope of the spec to dictate how to generate code (?) So when the spec mentions composition and polymorphism, it's just for validation purpose, correct? Code may be generated in any shape or form, as long as the generated code serializes, deserializes and validates the payload as specified in the spec. Of course in practice, it makes sense for the tooling to use implementation constructs that map the spec, but that is not a requirement. For example, somebody can write code using composition at the implementation level to model the OAS inheritance model, and vice versa. |
This issue is resolved if using the jersey2 library. |
Bug Report Checklist
Description
Using OpenAPI v3 document to generate a client-side Java SDK with the default library (okhttp-gson). The generated classes under the "api" directory cause a compilation failure.
For example, a generated API Java class api/SyslogApi.java has the following createSyslogPolicyCall() method. The requestBody argument is a templatized PolicyAbstractPolicy<String, Object>.
However, the generated model/PolicyAbstractPolicy.java class is not a generic, hence the compilation fails. There are two problems:
I was expecting the generated method signature to be something like:
The mustache templates that are used to generate these classes are:
It looks like the problem is the value of dataType in the API template does not match the class name of the POJO
I am using the default Java library, which is okhttp-gson.
openapi-generator version
master February 14th 2020
OpenAPI declaration file content or url
Operations:
Schema:
Command line used for generation
Steps to reproduce
Edit modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml and add the YAML snippets listed in this issue.
Invoke bin/java-petstore-native.sh
Open samples/......./openapitools/client/api/SyslogApi.java in file editor.
Related issues/PRs
Suggest a fix
The text was updated successfully, but these errors were encountered: