Contributions
Re: Can I have a polymorphic property that is not of type "Object"?
Tried with swagger-codegen - the models look good. I find thatOneOfSinglePetResponsePet andOneOfAllPetsResponseItemsduplicate each other though and can thus potentially be merged intoOneOfPet. But I do not know how feasible that would be for you. About array - I think there was a misunderstanding. By looking at your tool's output I can only notice small things, conceptually everything is all right. --- To summarize: AWESOME! Thank you very much. If that would be a feasible goal then I'd propose to dump the two interfaces in favor of a single one as illustrated below. Otherwise - already very good. public class SinglePetResponse { private OneOfPet pet = null; } public class AllPetsResponse extends ArrayList<OneOfPet> { private List<OneOfPet> pets = null; }4.2KViews0likes0CommentsRe: Can I have a polymorphic property that is not of type "Object"?
(re-posting because my previous post seems to be gone). Looking further at the generated files I notice that this does not seem to be solved in the extensible way. Original state (at the time of opening this ticket): public class SinglePetResponse { private @Valid Object pet = null; } Current state (ignoring the fact the OneOfDogCat is not generated): public class SinglePetResponse { private @Valid OneOfDogCat pet = null; } Desired state: public abstract class Pet { } public class SinglePetResponse { private @Valid Pet pet = null; } We can argue about the Pet class being abstract, of course, but the pet field being of type Pet and not something else is crucial. Otherwise if I extend my API later by adding a type Turtle, my existing code will break because some classes would be renamed.4.3KViews0likes3CommentsRe: Can I have a polymorphic property that is not of type "Object"?
Hihugomario. Sorry, I might have missed your previous update to this ticket, otherwise I would have tested this long ago. I have tried the following (same YAML as in the OP): 1)java -jaropenapi-generator-cli-4.3.0.jargenerate -i openapi.yaml -g jaxrs-spec && mvn clean install Result:COMPILATION ERROR. Besides, when looking at the generated models the moduleorg.openapitools.model.OneOfDogCat is imported but doesn't seem to be generated (at all). 2)-g jaxrs-resteasy-eap Same problem. 3) -gjava-undertow-server Same problem. The models are not referenced from the controller, however they reference each other and OneOfDogCat is still missing 4) -gjava-inflector Same problem. 5) -g java This is a client build (all the previous were server ones) and it demonstrates the same problem - OneOfDogCat is used but is not generated. ----------------- UPDATE ---------------- I have re-read my original post in this thread and it seems that there is another file that is not generated now:AllPetsResponse. Sorry, I'm taking this back. The file is indeed not generated, but it is also not used so that's fine.4.3KViews0likes4CommentsCan I have a polymorphic property that is not of type "Object"?
Hello. I am trying to come up with a single openapi.yaml that would serve for both code generation and API documentation. However, I'm having troubles with reaching both goals with a single file because of a polymorphism. Every time I try I either get ugly "Example Values" in the online documentation or just blunt "Object" types in generated classes or both. Let me illustrate: I have two endpoints, one of which returns a single polymorphic object and other returns a list of these. That is "GET /allPets" - would yield a list of Pets and Dogs and "GET /randomPet" would return a single Pet. If I would be writing my classes in a plain Java, then Pet would be an abstract class or an interface and Cat and Dog would inherit from/implement it. Therefore this abstract type would be used for both endpoints. However, this is not something I achieve with OpenAPI. May be I am just approaching the problem from the wrong end. Here is the definition: openapi: "3.0.0" info: version: 1.0.0 title: PetStore paths: /randomPet: get: responses: '200': description: "a single random pet" content: application/json: schema: $ref: '#/components/schemas/SinglePetResponse' /allPets: get: responses: '200': description: "a single random pet" content: application/json: schema: $ref: '#/components/schemas/AllPetsResponse' components: schemas: SinglePetResponse: type: object properties: pet: oneOf: - $ref: "#/components/schemas/Dog" - $ref: "#/components/schemas/Cat" discriminator: propertyName: pet_type AllPetsResponse: type: array items: oneOf: - $ref: "#/components/schemas/Dog" - $ref: "#/components/schemas/Cat" discriminator: propertyName: pet_type Pet: type: object required: - pet_type properties: pet_type: type: string discriminator: propertyName: pet_type Dog: # "Dog" is a value for the pet_type property (the discriminator value) allOf: # Combines the main `Pet` schema with `Dog`-specific properties - $ref: '#/components/schemas/Pet' - type: object # all other properties specific to a `Dog` properties: bark: type: boolean breed: type: string enum: [Dingo, Husky, Retriever, Shepherd] Cat: # "Cat" is a value for the pet_type property (the discriminator value) allOf: # Combines the main `Pet` schema with `Cat`-specific properties - $ref: '#/components/schemas/Pet' - type: object # all other properties specific to a `Cat` properties: hunts: type: boolean age: type: integer The classes I end up with essentially boil down to: public class SinglePetResponse { private @Valid Object pet = null; } and public class AllPetsResponse extends ArrayList<Object> { } On top of that the "SinglePetResponse" is rendered as an empty object in the UI. It is important for me to nest these properties under other objects - because in the real system the responses are much more than just a list of objects - they have other fields, too. What I would like to see is the "Pet" class used instead of "Object" and a better rendering in the UI (a random type or just a first time would suffice). A nice, but not required property would be the Pet class being abstract. Is there a way to achieve this with OpenAPI 3? Best regards, Alexey Nezhdanov5.6KViews0likes8Comments