Closed Marcelo00 closed 2 months ago
The code generation is functioning as expected here. The anyOf refs are not known as possible types. For them to be known they would have to be examined at one higher schema level and compared against type info. Currently the generation only knows about input and output types for array and object, types defined in type, and ref only. Because anyOf is not analyzed for type info, the only info comes from absent type info which allows any type.
If you want different functionality here, please consider filing a PR with thorough test cases.
Also, should your anyOf be a oneOf so it matches only one of those schemas rather than one or more?
Thanks for the information. Is it possible to have a short meeting or could you point out the necessary steps for such functionality? We would like to invest some time to add such feature as our use case prefers to work with these classes and it would be nice to have proper type hints. However, we cannot really judge how long it may take.
I would also assume that this behavior also occurs with other modifiers like oneOf
. Is it correct?
Also, should your anyOf be a oneOf so it matches only one of those schemas rather than one or more?
You are right. Our openAPI.yaml
is also auto-generated which sometime causes semantically wrong specification like this.
Adding this is non-trivial. In the latest version of the generators, returned instances only subclass one class. So to do this one would need to:
Doing this is difficult because any number of keywords can exist in the same schema including:
So a new CodegenSchema field that could store this may look like typeToInterfaces: Map<String, List
When are you free to discuss this this week and next week?
Hey, I am free this week. How should we proceed? I can write you a mail to figure out a date. A coworker would potentially join the meeting.
A simpler solution here is to handle this specific case only where a schema only has oneOf defintion and no other json schema keywords. In this case, you could modify the Schema.validate method to do the following:
Another solution here is to use Speakeasey's code generation: https://github.com/speakeasy-api/speakeasy which I believe handles oneOfs in the way that you want
@Marcelo00 how did you decide to proceed forward here? It has been ~ 1 month since we met about this.
If I don't hear back here I will close this issue as inactive.
Hey, I am sorry for not coming back to you. We tried out your suggestion of using speakeasy and it turns out that I fulfills our requirements. Thanks again for your time investing in this project and your suggestion. Really appreciate your help.
Bug Report Checklist
Description
When having an
anyOf
modifier for a schema that is used in a array schema, the resulting python class does not have the correct typing.Given the following schemas
the created class (in
classification_to_return.py
) has the correct typing informationHowever, when we use an
anyOf
inClassificationAttribute
we will get the wrong typing information in
classification_to_return.py
The
AttributesTuple
should have the type informationClassificationAttribute
or at least both of the possible types.openapi-json-schema-generator version
Version 4.5.1
OpenAPI declaration file content or url
Generation Details
Used the cli command to generate the client.
Steps to reproduce
Related issues/PRs
Suggest a fix