-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Open
some00
wants to merge
4
commits into
OpenAPITools:master
Choose a base branch
from
some00:kotlinx_polymorphism
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
bin/configs/kotlin-allOff-discriminator-kotlinx-serialization.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
for (Map.Entry<String, ModelsMap> modelsMap : objs.entrySet()) { | ||
for (ModelMap mo : modelsMap.getValue().getModels()) { | ||
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. minor suggestion: add some comments in this for loop to explain what it tries to archive |
||
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); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
modules/openapi-generator/src/test/resources/3_0/kotlin/polymorphism.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
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 |
23 changes: 23 additions & 0 deletions
23
...ient/petstore/kotlin-allOff-discriminator-kotlinx-serialization/.openapi-generator-ignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
35 changes: 35 additions & 0 deletions
35
...lient/petstore/kotlin-allOff-discriminator-kotlinx-serialization/.openapi-generator/FILES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
...ent/petstore/kotlin-allOff-discriminator-kotlinx-serialization/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.15.0-SNAPSHOT |
68 changes: 68 additions & 0 deletions
68
...les/client/petstore/kotlin-allOff-discriminator-kotlinx-serialization/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the PR. what about adding the new folder to the github workflow so that it will be tested moving forward?
https://github.com/OpenAPITools/openapi-generator/blob/master/.github/workflows/samples-kotlin-client.yaml