-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Kotlinx polymorphism with custom discriminator support #21531
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
Changes from all commits
ae65f53
5d74cdf
73a9d0e
2a1e8fa
1f8973a
06d993c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
generatorName: kotlin | ||
outputDir: samples/client/petstore/kotlin-allOff-discriminator-kotlinx-serialization | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/polymorphism.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-client | ||
additionalProperties: | ||
artifactId: kotlin-allOff-discriminator | ||
serializableModel: "false" | ||
dateLibrary: java8 | ||
enumUnknownDefaultCase: true | ||
serializationLibrary: kotlinx_serialization |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -963,6 +963,44 @@ public ModelsMap postProcessModels(ModelsMap objs) { | |
return objects; | ||
} | ||
|
||
@Override | ||
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) { | ||
objs = super.postProcessAllModels(objs); | ||
if (getSerializationLibrary() == SERIALIZATION_LIBRARY_TYPE.kotlinx_serialization) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @some00 thank you so much!! |
||
// The loop removes unneeded variables so commas are handled correctly in the related templates | ||
for (Map.Entry<String, ModelsMap> modelsMap : objs.entrySet()) { | ||
for (ModelMap mo : modelsMap.getValue().getModels()) { | ||
some00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CodegenModel cm = mo.getModel(); | ||
CodegenDiscriminator discriminator = cm.getDiscriminator(); | ||
if (discriminator == null) { | ||
continue; | ||
} | ||
// Remove discriminator property from the base class, it is not needed in the generated code | ||
getAllVarProperties(cm).forEach(list -> list.removeIf(var -> var.name == discriminator.getPropertyName())); | ||
|
||
for (CodegenDiscriminator.MappedModel mappedModel : discriminator.getMappedModels()) { | ||
// Add the mapping name to additionalProperties.disciminatorValue | ||
// The mapping name is used to define SerializedName, which in result makes derived classes | ||
// found by kotlinx-serialization during deserialization | ||
CodegenProperty additionalProperties = mappedModel.getModel().getAdditionalProperties(); | ||
if (additionalProperties == null) { | ||
additionalProperties = new CodegenProperty(); | ||
mappedModel.getModel().setAdditionalProperties(additionalProperties); | ||
} | ||
additionalProperties.discriminatorValue = mappedModel.getMappingName(); | ||
// Remove the discriminator property from the derived class, it is not needed in the generated code | ||
getAllVarProperties(mappedModel.getModel()).forEach(list -> list.removeIf(prop -> prop.name == discriminator.getPropertyName())); | ||
} | ||
|
||
} | ||
} | ||
} | ||
return objs; | ||
} | ||
private Stream<List<CodegenProperty>> getAllVarProperties(CodegenModel model) { | ||
return Stream.of(model.vars, model.allVars, model.optionalVars, model.requiredVars, model.readOnlyVars, model.readWriteVars); | ||
} | ||
|
||
private boolean usesRetrofit2Library() { | ||
return getLibrary() != null && getLibrary().contains(JVM_RETROFIT2); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: Example | ||
description: An example | ||
version: '0.1' | ||
contact: | ||
email: contact@example.org | ||
url: 'https://example.org' | ||
servers: | ||
- url: http://example.org | ||
tags: | ||
- name: bird | ||
paths: | ||
'/v1/bird/{id}': | ||
get: | ||
tags: | ||
- bird | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/bird' | ||
operationId: get-bird | ||
parameters: | ||
- schema: | ||
type: string | ||
format: uuid | ||
name: id | ||
in: path | ||
required: true | ||
components: | ||
schemas: | ||
animal: | ||
title: An animal | ||
type: object | ||
properties: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this spec incomplete? According to https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/#discriminator, the discriminator property should be part of the listed properties. I know this PR is already merged, but I'm hoping to get some clarification since we're looking forward to using this, but our spec does contain the discriminator property in the list of properties so I'm worried it won't work for us. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @F43nd1r I don't know about the spec being incomplete. I'm no expert there. |
||
id: | ||
type: string | ||
format: uuid | ||
optional_property: | ||
type: number | ||
required: | ||
- id | ||
discriminator: | ||
propertyName: discriminator | ||
mapping: | ||
BIRD: '#/components/schemas/bird' | ||
bird: | ||
title: A bird | ||
type: object | ||
allOf: | ||
- $ref: '#/components/schemas/animal' | ||
- properties: | ||
featherType: | ||
type: string | ||
required: | ||
- featherType |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# OpenAPI Generator Ignore | ||
# Generated by openapi-generator https://github.com/openapitools/openapi-generator | ||
|
||
# Use this file to prevent files from being overwritten by the generator. | ||
# The patterns follow closely to .gitignore or .dockerignore. | ||
|
||
# As an example, the C# client generator defines ApiClient.cs. | ||
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: | ||
#ApiClient.cs | ||
|
||
# You can match any string of characters against a directory, file or extension with a single asterisk (*): | ||
#foo/*/qux | ||
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux | ||
|
||
# You can recursively match patterns against a directory, file or extension with a double asterisk (**): | ||
#foo/**/qux | ||
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux | ||
|
||
# You can also negate patterns with an exclamation (!). | ||
# For example, you can ignore all files in a docs folder with the file extension .md: | ||
#docs/*.md | ||
# Then explicitly reverse the ignore rule for a single file: | ||
#!docs/README.md |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
README.md | ||
build.gradle | ||
docs/Animal.md | ||
docs/Bird.md | ||
docs/BirdApi.md | ||
gradle/wrapper/gradle-wrapper.jar | ||
gradle/wrapper/gradle-wrapper.properties | ||
gradlew | ||
gradlew.bat | ||
proguard-rules.pro | ||
settings.gradle | ||
src/main/kotlin/org/openapitools/client/apis/BirdApi.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/ApiAbstractions.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/ApiResponse.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/AtomicBooleanAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/AtomicIntegerAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/AtomicLongAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/BigDecimalAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/BigIntegerAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/ResponseExtensions.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/StringBuilderAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/URIAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/URLAdapter.kt | ||
src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt | ||
src/main/kotlin/org/openapitools/client/models/Animal.kt | ||
src/main/kotlin/org/openapitools/client/models/Bird.kt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.15.0-SNAPSHOT |
Uh oh!
There was an error while loading. Please reload this page.