Skip to content
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 @@ -22,15 +22,18 @@
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.openapitools.codegen.CliOption;
Expand Down Expand Up @@ -342,6 +345,32 @@ public ModelsMap postProcessModels(ModelsMap objs) {
break;
}
}

// Check if there are duplicate mappings in the discriminator
// ie.
// ```
// mappings:
// student: '#/components/schemas/Person'
// teacher: '#/components/schemas/Person'
// car: '#/components/schemas/Vehicle'
// ```
//
// Should be mapped to an enum with `PersonStudent`, `PersonTeacher`, `Vehicle` to 2 `Person` enum variants. (a compiler error)
if (cm.discriminator.getMapping() != null) {
if (hasDuplicateValues(cm.discriminator.getMapping())) {
var inverted = invertMap(cm.discriminator.getMapping());
for (var s : inverted.entrySet()) {
if (s.getValue().size() > 1) {
LOGGER.debug("Found duplicated enum model (" + s.getKey() + ") in model " + cm.name + ". Adding suffix to model names.");
for (var m : cm.discriminator.getMappedModels()) {
if (s.getValue().contains(m.getMappingName())) {
m.setModelName(m.getModelName() + StringUtils.camelize(m.getMappingName()));
}
}
}
}
}
}
}

// Flag structs with byteArrays in them so that we can annotate them with the serde_as macro
Expand Down Expand Up @@ -789,4 +818,18 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
.put("lifetimeName", new ReplaceAllLambda("^r#", "r_"));
}

public static <K, V> Map<V, List<K>> invertMap(Map<K, V> map) {
Map<V, List<K>> invertedMap = new HashMap<>();

for (Map.Entry<K, V> entry : map.entrySet()) {
invertedMap.computeIfAbsent(entry.getValue(), k -> new ArrayList<>()).add(entry.getKey());
}

return invertedMap;
}

public static <K, V> boolean hasDuplicateValues(Map<K, V> map) {
Set<V> uniqueValues = new HashSet<>(map.values());
return uniqueValues.size() < map.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,26 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/TypeTesting'
'/tests/discriminatorDuplicateEnums':
get:
tags:
- testing
summary: 'Test for duplicate enums when using discriminator. (One of the issues in #20500)'
responses:
'200':
description: test
content:
application/json:
schema:
anyOf:
- $ref: '#/components/schemas/Person'
- $ref: '#/components/schemas/Vehicle'
discriminator:
propertyName: objectType
mapping:
student: '#/components/schemas/Person'
teacher: '#/components/schemas/Person'
car: '#/components/schemas/Vehicle'
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Expand Down Expand Up @@ -1014,4 +1034,24 @@ components:
properties:
foo: {}
required: ["foo"]
Person:
type: object
properties:
type:
type: string
name:
type: string
required:
- type
- name
Vehicle:
type: object
properties:
type:
type: string
speed:
type: number
required:
- type
- speed

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ docs/NullableArray.md
docs/NumericEnumTesting.md
docs/OptionalTesting.md
docs/Order.md
docs/Person.md
docs/Pet.md
docs/PetApi.md
docs/PropertyTest.md
Expand All @@ -22,10 +23,12 @@ docs/Return.md
docs/StoreApi.md
docs/Tag.md
docs/TestingApi.md
docs/TestsDiscriminatorDuplicateEnumsGet200Response.md
docs/TypeTesting.md
docs/UniqueItemArrayTesting.md
docs/User.md
docs/UserApi.md
docs/Vehicle.md
git_push.sh
src/apis/client.rs
src/apis/configuration.rs
Expand All @@ -37,6 +40,7 @@ src/apis/store_api.rs
src/apis/testing_api.rs
src/apis/user_api.rs
src/lib.rs
src/models/_tests_discriminator_duplicate_enums_get_200_response.rs
src/models/action_container.rs
src/models/any_type_test.rs
src/models/api_response.rs
Expand All @@ -51,9 +55,11 @@ src/models/nullable_array.rs
src/models/numeric_enum_testing.rs
src/models/optional_testing.rs
src/models/order.rs
src/models/person.rs
src/models/pet.rs
src/models/property_test.rs
src/models/tag.rs
src/models/type_testing.rs
src/models/unique_item_array_testing.rs
src/models/user.rs
src/models/vehicle.rs
4 changes: 4 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Class | Method | HTTP request | Description
*StoreApi* | [**get_inventory**](docs/StoreApi.md#get_inventory) | **Get** /store/inventory | Returns pet inventories by status
*StoreApi* | [**get_order_by_id**](docs/StoreApi.md#get_order_by_id) | **Get** /store/order/{orderId} | Find purchase order by ID
*StoreApi* | [**place_order**](docs/StoreApi.md#place_order) | **Post** /store/order | Place an order for a pet
*TestingApi* | [**tests_discriminator_duplicate_enums_get**](docs/TestingApi.md#tests_discriminator_duplicate_enums_get) | **Get** /tests/discriminatorDuplicateEnums | Test for duplicate enums when using discriminator. (One of the issues in #20500)
*TestingApi* | [**tests_file_response_get**](docs/TestingApi.md#tests_file_response_get) | **Get** /tests/fileResponse | Returns an image file
*TestingApi* | [**tests_type_testing_get**](docs/TestingApi.md#tests_type_testing_get) | **Get** /tests/typeTesting | Route to test the TypeTesting schema
*UserApi* | [**create_user**](docs/UserApi.md#create_user) | **Post** /user | Create user
Expand All @@ -64,14 +65,17 @@ Class | Method | HTTP request | Description
- [NumericEnumTesting](docs/NumericEnumTesting.md)
- [OptionalTesting](docs/OptionalTesting.md)
- [Order](docs/Order.md)
- [Person](docs/Person.md)
- [Pet](docs/Pet.md)
- [PropertyTest](docs/PropertyTest.md)
- [Ref](docs/Ref.md)
- [Return](docs/Return.md)
- [Tag](docs/Tag.md)
- [TestsDiscriminatorDuplicateEnumsGet200Response](docs/TestsDiscriminatorDuplicateEnumsGet200Response.md)
- [TypeTesting](docs/TypeTesting.md)
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
- [User](docs/User.md)
- [Vehicle](docs/Vehicle.md)


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

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**r#type** | **String** | |
**name** | **String** | |

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


26 changes: 26 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/docs/TestingApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,37 @@ All URIs are relative to *http://petstore.swagger.io/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
[**tests_discriminator_duplicate_enums_get**](TestingApi.md#tests_discriminator_duplicate_enums_get) | **Get** /tests/discriminatorDuplicateEnums | Test for duplicate enums when using discriminator. (One of the issues in #20500)
[**tests_file_response_get**](TestingApi.md#tests_file_response_get) | **Get** /tests/fileResponse | Returns an image file
[**tests_type_testing_get**](TestingApi.md#tests_type_testing_get) | **Get** /tests/typeTesting | Route to test the TypeTesting schema



## tests_discriminator_duplicate_enums_get

> models::TestsDiscriminatorDuplicateEnumsGet200Response tests_discriminator_duplicate_enums_get()
Test for duplicate enums when using discriminator. (One of the issues in #20500)

### Parameters

This endpoint does not need any parameter.

### Return type

[**models::TestsDiscriminatorDuplicateEnumsGet200Response**](_tests_discriminatorDuplicateEnums_get_200_response.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json

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


## tests_file_response_get

> std::path::PathBuf tests_file_response_get()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TestsDiscriminatorDuplicateEnumsGet200Response

## Enum Variants

| Name | Value |
|---- | -----|

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


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

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**r#type** | **String** | |
**speed** | **f64** | |

[[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
Expand Up @@ -37,12 +37,21 @@ impl<C: Connect> TestingApiClient<C>
}

pub trait TestingApi: Send + Sync {
fn tests_discriminator_duplicate_enums_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::TestsDiscriminatorDuplicateEnumsGet200Response, Error>> + Send>>;
fn tests_file_response_get(&self, ) -> Pin<Box<dyn Future<Output = Result<std::path::PathBuf, Error>> + Send>>;
fn tests_type_testing_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::TypeTesting, Error>> + Send>>;
}

impl<C: Connect>TestingApi for TestingApiClient<C>
where C: Clone + std::marker::Send + Sync {
#[allow(unused_mut)]
fn tests_discriminator_duplicate_enums_get(&self, ) -> Pin<Box<dyn Future<Output = Result<models::TestsDiscriminatorDuplicateEnumsGet200Response, Error>> + Send>> {
let mut req = __internal_request::Request::new(hyper::Method::GET, "/tests/discriminatorDuplicateEnums".to_string())
;

req.execute(self.configuration.borrow())
}

#[allow(unused_mut)]
fn tests_file_response_get(&self, ) -> Pin<Box<dyn Future<Output = Result<std::path::PathBuf, Error>> + Send>> {
let mut req = __internal_request::Request::new(hyper::Method::GET, "/tests/fileResponse".to_string())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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(tag = "objectType")]
pub enum TestsDiscriminatorDuplicateEnumsGet200Response {
#[serde(rename="car")]
Vehicle {
#[serde(rename = "type")]
r#type: String,
#[serde(rename = "name")]
name: String,
#[serde(rename = "speed")]
speed: f64,
},
#[serde(rename="student")]
PersonStudent {
#[serde(rename = "type")]
r#type: String,
#[serde(rename = "name")]
name: String,
#[serde(rename = "speed")]
speed: f64,
},
#[serde(rename="teacher")]
PersonTeacher {
#[serde(rename = "type")]
r#type: String,
#[serde(rename = "name")]
name: String,
#[serde(rename = "speed")]
speed: f64,
},
}

impl Default for TestsDiscriminatorDuplicateEnumsGet200Response {
fn default() -> Self {
Self::Vehicle {
r#type: Default::default(),
name: Default::default(),
speed: Default::default(),
}

}
}


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 @@ -20,6 +20,8 @@ pub mod optional_testing;
pub use self::optional_testing::OptionalTesting;
pub mod order;
pub use self::order::Order;
pub mod person;
pub use self::person::Person;
pub mod pet;
pub use self::pet::Pet;
pub mod property_test;
Expand All @@ -30,9 +32,13 @@ pub mod model_return;
pub use self::model_return::Return;
pub mod tag;
pub use self::tag::Tag;
pub mod _tests_discriminator_duplicate_enums_get_200_response;
pub use self::_tests_discriminator_duplicate_enums_get_200_response::TestsDiscriminatorDuplicateEnumsGet200Response;
pub mod type_testing;
pub use self::type_testing::TypeTesting;
pub mod unique_item_array_testing;
pub use self::unique_item_array_testing::UniqueItemArrayTesting;
pub mod user;
pub use self::user::User;
pub mod vehicle;
pub use self::vehicle::Vehicle;
30 changes: 30 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/src/models/person.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 Person {
#[serde(rename = "type")]
pub r#type: String,
#[serde(rename = "name")]
pub name: String,
}

impl Person {
pub fn new(r#type: String, name: String) -> Person {
Person {
r#type,
name,
}
}
}

Loading