-
-
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 4 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,38 @@ 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!! |
||
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; | ||
} | ||
getAllVarProperties(cm).forEach(list -> list.removeIf(var -> var.name == discriminator.getPropertyName())); | ||
|
||
for (CodegenDiscriminator.MappedModel mappedModel : discriminator.getMappedModels()) { | ||
CodegenProperty additionalProperties = mappedModel.getModel().getAdditionalProperties(); | ||
if (additionalProperties == null) { | ||
additionalProperties = new CodegenProperty(); | ||
mappedModel.getModel().setAdditionalProperties(additionalProperties); | ||
} | ||
additionalProperties.discriminatorValue = mappedModel.getMappingName(); | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# org.openapitools.client - Kotlin client library for Example | ||
|
||
An example | ||
|
||
## Overview | ||
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://github.com/OAI/OpenAPI-Specification) from a remote server, you can easily generate an API client. | ||
|
||
- API version: 0.1 | ||
- Package version: | ||
- Generator version: 7.15.0-SNAPSHOT | ||
- Build package: org.openapitools.codegen.languages.KotlinClientCodegen | ||
For more information, please visit [https://example.org](https://example.org) | ||
|
||
## Requires | ||
|
||
* Kotlin 1.7.21 | ||
* Gradle 7.5 | ||
|
||
## Build | ||
|
||
First, create the gradle wrapper script: | ||
|
||
``` | ||
gradle wrapper | ||
``` | ||
|
||
Then, run: | ||
|
||
``` | ||
./gradlew check assemble | ||
``` | ||
|
||
This runs all tests and packages the library. | ||
|
||
## Features/Implementation Notes | ||
|
||
* Supports JSON inputs/outputs, File inputs, and Form inputs. | ||
* Supports collection formats for query parameters: csv, tsv, ssv, pipes. | ||
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions. | ||
* Implementation of ApiClient is intended to reduce method counts, specifically to benefit Android targets. | ||
|
||
<a id="documentation-for-api-endpoints"></a> | ||
## Documentation for API Endpoints | ||
|
||
All URIs are relative to *http://example.org* | ||
|
||
| Class | Method | HTTP request | Description | | ||
| ------------ | ------------- | ------------- | ------------- | | ||
| *BirdApi* | [**getBird**](docs/BirdApi.md#getbird) | **GET** /v1/bird/{id} | | | ||
|
||
|
||
<a id="documentation-for-models"></a> | ||
## Documentation for Models | ||
|
||
- [org.openapitools.client.models.Animal](docs/Animal.md) | ||
- [org.openapitools.client.models.Bird](docs/Bird.md) | ||
|
||
|
||
<a id="documentation-for-authorization"></a> | ||
## Documentation for Authorization | ||
|
||
Endpoints do not require authorization. | ||
|
||
|
||
|
||
## Author | ||
|
||
contact@example.org |
Uh oh!
There was an error while loading. Please reload this page.