Skip to content
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

[Python] Adds allOf/oneOf/anyOf composed models #4446

Merged
merged 3 commits into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ SYNOPSIS
[--model-name-prefix <model name prefix>]
[--model-name-suffix <model name suffix>]
[--model-package <model package>]
[(-o <output directory> | --output <output directory>)]
[(-o <output directory> | --output <output directory>)]
[(-p <additional properties> | --additional-properties <additional properties>)...]
[--package-name <package name>] [--release-note <release note>]
[--remove-operation-id-prefix]
Expand Down Expand Up @@ -562,7 +562,7 @@ Here is a list of community-conitributed IDE plug-ins that integrate with OpenAP
- Visual Studio: [REST API Client Code Generator](https://marketplace.visualstudio.com/items?itemName=ChristianResmaHelle.ApiClientCodeGenerator) by [Christian Resma Helle](https://christian-helle.blogspot.com/)
- Visual Studio Code: [Codewind OpenAPI Tools](https://marketplace.visualstudio.com/items?itemName=IBM.codewind-openapi-tools) by [IBM](https://marketplace.visualstudio.com/publishers/IBM)


## [4 - Companies/Projects using OpenAPI Generator](#table-of-contents)
Here are some companies/projects (alphabetical order) using OpenAPI Generator in production. To add your company/project to the list, please visit [README.md](README.md) and click on the icon to edit the page.

Expand Down Expand Up @@ -727,7 +727,7 @@ OpenAPI Generator core team members are contributors who have been making signif
* [@jmini](https://github.com/jmini) (2018/04) [:heart:](https://www.patreon.com/jmini)
* [@etherealjoy](https://github.com/etherealjoy) (2019/06)

:heart: = Link to support the contributor directly
:heart: = Link to support the contributor directly

#### Template Creator

Expand Down Expand Up @@ -782,8 +782,9 @@ Here is a list of template creators:
* Perl: @wing328 [:heart:](https://www.patreon.com/wing328)
* PHP (Guzzle): @baartosz
* PowerShell: @beatcracker
* Python-experimental: @spacether
* R: @ramnov
* Ruby (Faraday): @meganemura @dkliban
* Ruby (Faraday): @meganemura @dkliban
* Rust: @farcaller
* Rust (rust-server): @metaswitch
* Scala (scalaz & http4s): @tbrown1979
Expand All @@ -793,7 +794,7 @@ Here is a list of template creators:
* Swift 4: @ehyche
* TypeScript (Angular1): @mhardorf
* TypeScript (Angular2): @roni-frantchi
* TypeScript (Angular6): @akehir
* TypeScript (Angular6): @akehir
* TypeScript (Angular7): @topce
* TypeScript (Axios): @nicokoenig
* TypeScript (Fetch): @leonyu
Expand All @@ -805,7 +806,7 @@ Here is a list of template creators:
* Server Stubs
* Ada: @stcarrez
* C# ASP.NET 5: @jimschubert [:heart:](https://www.patreon.com/jimschubert)
* C# ASP.NET Core 3.0: @A-Joshi
* C# ASP.NET Core 3.0: @A-Joshi
* C# NancyFX: @mstefaniuk
* C++ (Qt5 QHttpEngine): @etherealjoy
* C++ Pistache: @sebymiano
Expand Down Expand Up @@ -848,7 +849,7 @@ Here is a list of template creators:
* Configuration
* Apache2: @stkrwork
* Schema
* Avro: @sgadouar
* Avro: @sgadouar
* GraphQL: @wing328 [:heart:](https://www.patreon.com/wing328)
* MySQL: @ybelenko
* Protocol Buffer: @wing328
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1807,17 +1807,22 @@ public CodegenModel fromModel(String name, Schema schema) {
m.allVars = new ArrayList<CodegenProperty>();
if (composed.getAllOf() != null) {
int modelImplCnt = 0; // only one inline object allowed in a ComposedModel
int modelDiscriminators = 0; // only one discriminator allowed in a ComposedModel
for (Schema innerSchema : composed.getAllOf()) { // TOOD need to work with anyOf, oneOf as well
if (m.discriminator == null) {
if (m.discriminator == null && innerSchema.getDiscriminator() != null) {
LOGGER.debug("discriminator is set to null (not correctly set earlier): {}", name);
m.discriminator = createDiscriminator(name, schema);
m.discriminator = createDiscriminator(name, innerSchema);
Copy link
Contributor Author

@spacether spacether Dec 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per here: https://swagger.io/specification/#discriminatorObject
We should be able to set the discriminator on the parent composed schema, but that's not yet working because of this bug in swagger parser: swagger-api/swagger-parser#1269
In place of having discriminator at the root level of a composed schema, we should be able to use it from our definition in our allOf schemas.
I updated this code to set the discriminator on self if one of its ancestor allOf schemas defines discriminator.
Please see my ParentPet class which demonstrates this usage at: https://github.com/spacether/openapi-generator/blob/allof_anyof_oneof_addition/modules/openapi-generator/src/test/resources/2_0/python-client-experimental/petstore-with-fake-endpoints-models-for-testing.yaml#L2072

NOTE: the original invocation here m.discriminator = createDiscriminator(name, schema); did not do anything because we already ran that exact line above in line 1772.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this seems reasonable, but I'd like to have someone else on the @OpenAPITools/generator-core-team review as well since it affects all generators.

modelDiscriminators++;
}

if (innerSchema.getXml() != null) {
m.xmlPrefix = innerSchema.getXml().getPrefix();
m.xmlNamespace = innerSchema.getXml().getNamespace();
m.xmlName = innerSchema.getXml().getName();
}
if (modelDiscriminators > 1) {
LOGGER.error("Allof composed schema is inheriting >1 discriminator. Only use one discriminator: {}", composed);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should error out if the user tries to define multiple discriminators in one schema.


if (modelImplCnt++ > 1) {
LOGGER.warn("More than one inline schema specified in allOf:. Only the first one is recognized. All others are ignored.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void processOpts() {

}

private static String dropDots(String str) {
protected static String dropDots(String str) {
return str.replaceAll("\\.", "_");
}

Expand Down
Loading