Skip to content

Remove duplicate oneOf schemas during pre-processing #21174

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

Merged
merged 5 commits into from
May 4, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,9 @@ protected Schema normalizeAllOfWithProperties(Schema schema, Set<Schema> visited
}

protected Schema normalizeOneOf(Schema schema, Set<Schema> visitedSchemas) {
// Remove duplicate oneOf entries
ModelUtils.deduplicateOneOfSchema(schema);

// simplify first as the schema may no longer be a oneOf after processing the rule below
schema = processSimplifyOneOf(schema);

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 @@ -32,6 +32,7 @@ docs/UniqueItemArrayTesting.md
docs/User.md
docs/UserApi.md
docs/Vehicle.md
docs/WithInnerOneOf.md
git_push.sh
src/apis/client.rs
src/apis/configuration.rs
Expand Down Expand Up @@ -69,3 +70,4 @@ 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
1 change: 1 addition & 0 deletions samples/client/petstore/rust/hyper/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Class | Method | HTTP request | Description
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
- [User](docs/User.md)
- [Vehicle](docs/Vehicle.md)
- [WithInnerOneOf](docs/WithInnerOneOf.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::Order**](Order.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
}
}

2 changes: 2 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 @@ -48,6 +48,8 @@ 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;
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::Order>>,
}

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 @@ -32,6 +32,7 @@ docs/UniqueItemArrayTesting.md
docs/User.md
docs/UserApi.md
docs/Vehicle.md
docs/WithInnerOneOf.md
git_push.sh
src/apis/configuration.rs
src/apis/fake_api.rs
Expand Down Expand Up @@ -67,3 +68,4 @@ 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
1 change: 1 addition & 0 deletions samples/client/petstore/rust/hyper0x/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Class | Method | HTTP request | Description
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
- [User](docs/User.md)
- [Vehicle](docs/Vehicle.md)
- [WithInnerOneOf](docs/WithInnerOneOf.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::Order**](Order.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
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ 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;
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::Order>>,
}

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

Loading
Loading