Repository navigation
Closed
swagger-api/swagger-codegen-generators
#1106Description
Description
When I generate client libs for an OpenAPI 3.0.1 YAML, discriminator settings are ignored in the generated JSON deserialisation code.
This means when deserialising JSON we can't enforce polymorphism/composition.
Swagger-codegen version
3.0.19
Swagger declaration file content or url
openapi: 3.0.1
info:
title: Inheritance
description: A description
contact:
email: [email protected]
version: 0.0.0
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: petType
responses:
'200':
description: Updated
components:
schemas:
Pet:
type: object
required:
- petType
properties:
petType:
type: string
discriminator:
propertyName: petType
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: integerThe command line:
$ java -jar swagger-codegen-cli.jar generate \
-i inheritance.yaml \
-l java \
-o java
Generates this in JSON:
GsonFireBuilder fireBuilder = new GsonFireBuilder()
.registerTypeSelector(Pet.class, new TypeSelector<Pet>() {
@Override
public Class<? extends Pet> getClassForElement(JsonElement readElement) {
Map<String, Class<? extends Pet>> classByDiscriminatorValue = new HashMap<>();
classByDiscriminatorValue.put("Cat".toUpperCase(), Cat.class);
classByDiscriminatorValue.put("Dog".toUpperCase(), Dog.class);
classByDiscriminatorValue.put("Pet".toUpperCase(), Pet.class);
return getClassByDiscriminator(
classByDiscriminatorValue,
getDiscriminatorValue(readElement, ""));
}
})Note the empty "" where the discriminator value (petType) should be.
When I try to deserialize:
java.lang.IllegalArgumentException: missing discriminator field: <>
at io.swagger.client.JSON.getDiscriminatorValue(JSON.java:124)
at io.swagger.client.JSON.access$0(JSON.java:121)
at io.swagger.client.JSON$1.getClassForElement(JSON.java:62)
at io.gsonfire.gson.TypeSelectorTypeAdapterFactory$TypeSelectorTypeAdapter.read(TypeSelectorTypeAdapterFactory.java:65)
at io.gsonfire.gson.NullableTypeAdapter.read(NullableTypeAdapter.java:36)
at io.gsonfire.gson.HooksTypeAdapter.deserialize(HooksTypeAdapter.java:86)
at io.gsonfire.gson.HooksTypeAdapter.read(HooksTypeAdapter.java:54)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:870)
When I generate using OpenAPI Generator::4.3.0:
GsonFireBuilder fireBuilder = new GsonFireBuilder()
.registerTypeSelector(Pet.class, new TypeSelector() {
@Override
public Class getClassForElement(JsonElement readElement) {
Map classByDiscriminatorValue = new HashMap();
classByDiscriminatorValue.put("Dog".toUpperCase(Locale.ROOT), Dog.class);
classByDiscriminatorValue.put("Cat".toUpperCase(Locale.ROOT), Cat.class);
classByDiscriminatorValue.put("Pet".toUpperCase(Locale.ROOT), Pet.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "petType"));
}
})
;(petType is populated here).
Command line used for generation
Using the Gradle plugin, config as above.
Steps to reproduce
gradle generateSwaggerCodeInheritance
Reacted by Markus Schreiber, Yingsheng Tian, Moe, Maksim Gazimzianov, Luiz Ferraz and Alexander Bouriakov
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-][JAVA] Discriminator not used in JSON[/-][+][JAVA] Discriminator not used in JSON.java[/+]Yingsheng-eroad commentedon Jul 16, 2020
Using OpenAPI tools, I have an issue, if I choose an enum as the discriminator.
incompatible types: String cannot be converted to DeviceTypemdashti commentedon Jul 27, 2020
i encountered the exact same issue and took me a while to figure out it's not a misspelling on my side! Would really apprecite if you could fix this issue.
dordal commentedon Oct 13, 2020
This is more than just Java; I've seen it in PHP and Python ( see #10168 ), and I believe it may be an issue in all languages.
mgazim commentedon Jan 29, 2021
Are there any plans to fix this? Experiencing the same issue on swagger-codegen 3.0.24 with Java. I'm not an expert in this so any workarounds are appreciated :)
rhvonlehe commentedon Feb 2, 2021
I'm seeing the same thing with Swagger-codegen 2.4.18. This is the 2nd bug I've encountered, the first being code that was generated was invalid - invalid methods were being called against an imported library.
Actually what I am seeing is a bit different. I have polymorphism using 'allOf' instead of 'oneOf', so I only ever have 1 leaf class from any base class. In my case I was able to just delete the code it was generating to deal with discriminators since it made no sense. The types were already being determined just fine.
dawnstrider commentedon Apr 10, 2021
Bug reproducable with following API spec:
https://developer.atlassian.com/cloud/jira/platform/swagger.v3.json
and swagger codegen:
3.0.25
alexnb commentedon Aug 6, 2021
This seems to be fixed with 3.0.27
dawnstrider commentedon Oct 17, 2022
Still reproducible with the swagger json from Atlassian and codegen 3.0.35
jonmartinsolaas commentedon Jan 27, 2023
Seems to work with openapi-generator. At least for my little hello-world-test.
java -jar openapi-generator-cli.jar generate -i https://developer.atlassian.com/cloud/jira/platform/swagger-v3.v3.json -g java -o ./jira-clientHugoMario commentedon Feb 19, 2023
This has been fixed by swagger-api/swagger-codegen-generators@2bd73fb and #12070