Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,14 @@ public void postProcessParameter(CodegenParameter parameter) {
@Override
@SuppressWarnings("unused")
public void preprocessOpenAPI(OpenAPI openAPI) {
if (openAPI.getComponents() != null && openAPI.getComponents().getSchemas() != null) {
// Remove duplicate oneOf
Map<String, Schema> schemas = new HashMap<>(openAPI.getComponents().getSchemas());
for (var schema : schemas.values()) {
ModelUtils.deduplicateOneOfSchema(schema);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

what about doing this in the normalizer instead when looping through all schemas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, good call.
If we do that, we can deduplicate before calling processSimplifyOneOf() which will help further simplify the models :)

if (useOneOfInterfaces && openAPI.getComponents() != null) {
// we process the openapi schema here to find oneOf schemas and create interface models for them
Map<String, Schema> schemas = new HashMap<>(openAPI.getComponents().getSchemas());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,23 @@ public static Schema simplyOneOfAnyOfWithOnlyOneNonNullSubSchema(OpenAPI openAPI
return schema;
}

/**
* Removes duplicate `oneOf` from a given schema if it does not also have a discriminator.
*
* @param schema Schema
*/
public static void deduplicateOneOfSchema(Schema<?> schema) {
if (schema.getOneOf() == null) {
return;
}
if (schema.getDiscriminator() != null) {
return; // Duplicate oneOf are allowed if there is a discriminator that can be used to separate them.
}

Set<Schema> deduplicated = new LinkedHashSet<>(schema.getOneOf());
schema.setOneOf(new ArrayList<>(deduplicated));
}

/**
* Check if the schema is of type 'null' or schema itself is pointing to null
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,16 @@ components:
allOf:
- $ref: '#/components/schemas/existing_tags_array'
- description: This is a test for allOf with metadata only fields
DuplicateOneOf:
type: object
oneOf:
- $ref: '#/components/schemas/Order'
- $ref: '#/components/schemas/Order'
WithInnerOneOf:
type: object
properties:
foo:
type: object
oneOf:
- $ref: '#/components/schemas/Order'
- $ref: '#/components/schemas/Order'
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ docs/ApiResponse.md
docs/ArrayItemRefTest.md
docs/Baz.md
docs/Category.md
docs/DuplicateOneOf.md
docs/DuplicateTest.md
docs/Duplicatetest.md
docs/EnumArrayTesting.md
Expand All @@ -32,6 +33,8 @@ docs/UniqueItemArrayTesting.md
docs/User.md
docs/UserApi.md
docs/Vehicle.md
docs/WithInnerOneOf.md
docs/WithInnerOneOfFoo.md
git_push.sh
src/apis/client.rs
src/apis/configuration.rs
Expand All @@ -50,6 +53,7 @@ src/models/api_response.rs
src/models/array_item_ref_test.rs
src/models/baz.rs
src/models/category.rs
src/models/duplicate_one_of.rs
src/models/duplicate_test.rs
src/models/duplicatetest.rs
src/models/enum_array_testing.rs
Expand All @@ -69,3 +73,5 @@ src/models/type_testing.rs
src/models/unique_item_array_testing.rs
src/models/user.rs
src/models/vehicle.rs
src/models/with_inner_one_of.rs
src/models/with_inner_one_of_foo.rs
3 changes: 3 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Class | Method | HTTP request | Description
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [DuplicateOneOf](docs/DuplicateOneOf.md)
- [DuplicateTest](docs/DuplicateTest.md)
- [Duplicatetest](docs/Duplicatetest.md)
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Expand All @@ -80,6 +81,8 @@ Class | Method | HTTP request | Description
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
- [User](docs/User.md)
- [Vehicle](docs/Vehicle.md)
- [WithInnerOneOf](docs/WithInnerOneOf.md)
- [WithInnerOneOfFoo](docs/WithInnerOneOfFoo.md)


To get access to the crate's generated documentation, use:
Expand Down
11 changes: 11 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/docs/DuplicateOneOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# DuplicateOneOf

## Enum Variants

| Name | Description |
|---- | -----|
| Order | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


11 changes: 11 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/docs/WithInnerOneOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WithInnerOneOf

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**foo** | Option<[**models::WithInnerOneOfFoo**](WithInnerOneOf_foo.md)> | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WithInnerOneOfFoo

## Enum Variants

| Name | Description |
|---- | -----|
| Order | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DuplicateOneOf {
Order(Box<models::Order>),
}

impl Default for DuplicateOneOf {
fn default() -> Self {
Self::Order(Default::default())
}
}
/// Order Status
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Status {
#[serde(rename = "placed")]
Placed,
#[serde(rename = "approved")]
Approved,
#[serde(rename = "delivered")]
Delivered,
}

impl Default for Status {
fn default() -> Status {
Self::Placed
}
}

6 changes: 6 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod baz;
pub use self::baz::Baz;
pub mod category;
pub use self::category::Category;
pub mod duplicate_one_of;
pub use self::duplicate_one_of::DuplicateOneOf;
pub mod duplicate_test;
pub use self::duplicate_test::DuplicateTest;
pub mod duplicatetest;
Expand Down Expand Up @@ -48,6 +50,10 @@ pub mod user;
pub use self::user::User;
pub mod vehicle;
pub use self::vehicle::Vehicle;
pub mod with_inner_one_of;
pub use self::with_inner_one_of::WithInnerOneOf;
pub mod with_inner_one_of_foo;
pub use self::with_inner_one_of_foo::WithInnerOneOfFoo;
use serde::{Deserialize, Deserializer, Serializer};
use serde_with::{de::DeserializeAsWrap, ser::SerializeAsWrap, DeserializeAs, SerializeAs};
use std::marker::PhantomData;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct WithInnerOneOf {
#[serde(rename = "foo", skip_serializing_if = "Option::is_none")]
pub foo: Option<Box<models::WithInnerOneOfFoo>>,
}

impl WithInnerOneOf {
pub fn new() -> WithInnerOneOf {
WithInnerOneOf {
foo: None,
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum WithInnerOneOfFoo {
Order(Box<models::Order>),
}

impl Default for WithInnerOneOfFoo {
fn default() -> Self {
Self::Order(Default::default())
}
}
/// Order Status
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Status {
#[serde(rename = "placed")]
Placed,
#[serde(rename = "approved")]
Approved,
#[serde(rename = "delivered")]
Delivered,
}

impl Default for Status {
fn default() -> Status {
Self::Placed
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ docs/ApiResponse.md
docs/ArrayItemRefTest.md
docs/Baz.md
docs/Category.md
docs/DuplicateOneOf.md
docs/DuplicateTest.md
docs/Duplicatetest.md
docs/EnumArrayTesting.md
Expand All @@ -32,6 +33,8 @@ docs/UniqueItemArrayTesting.md
docs/User.md
docs/UserApi.md
docs/Vehicle.md
docs/WithInnerOneOf.md
docs/WithInnerOneOfFoo.md
git_push.sh
src/apis/configuration.rs
src/apis/fake_api.rs
Expand All @@ -48,6 +51,7 @@ src/models/api_response.rs
src/models/array_item_ref_test.rs
src/models/baz.rs
src/models/category.rs
src/models/duplicate_one_of.rs
src/models/duplicate_test.rs
src/models/duplicatetest.rs
src/models/enum_array_testing.rs
Expand All @@ -67,3 +71,5 @@ src/models/type_testing.rs
src/models/unique_item_array_testing.rs
src/models/user.rs
src/models/vehicle.rs
src/models/with_inner_one_of.rs
src/models/with_inner_one_of_foo.rs
3 changes: 3 additions & 0 deletions samples/client/petstore/rust/hyper0x/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Class | Method | HTTP request | Description
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [DuplicateOneOf](docs/DuplicateOneOf.md)
- [DuplicateTest](docs/DuplicateTest.md)
- [Duplicatetest](docs/Duplicatetest.md)
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Expand All @@ -80,6 +81,8 @@ Class | Method | HTTP request | Description
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
- [User](docs/User.md)
- [Vehicle](docs/Vehicle.md)
- [WithInnerOneOf](docs/WithInnerOneOf.md)
- [WithInnerOneOfFoo](docs/WithInnerOneOfFoo.md)


To get access to the crate's generated documentation, use:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# DuplicateOneOf

## Enum Variants

| Name | Description |
|---- | -----|
| Order | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WithInnerOneOf

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**foo** | Option<[**models::WithInnerOneOfFoo**](WithInnerOneOf_foo.md)> | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WithInnerOneOfFoo

## Enum Variants

| Name | Description |
|---- | -----|
| Order | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DuplicateOneOf {
Order(Box<models::Order>),
}

impl Default for DuplicateOneOf {
fn default() -> Self {
Self::Order(Default::default())
}
}
/// Order Status
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Status {
#[serde(rename = "placed")]
Placed,
#[serde(rename = "approved")]
Approved,
#[serde(rename = "delivered")]
Delivered,
}

impl Default for Status {
fn default() -> Status {
Self::Placed
}
}

Loading
Loading