Skip to content

Commit 35ba041

Browse files
authored
[rust] Fix for allOf multi model with only metadata fields (#20892)
* Fix for allof multi model with only metadata fields * Update samples * fixed compiler errors in java 17 and lower * refactored isMetadataOnlySchema to ModelUtils.java
1 parent 1eee603 commit 35ba041

File tree

64 files changed

+559
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+559
-22
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.swagger.v3.oas.models.parameters.RequestBody;
3131
import io.swagger.v3.oas.models.responses.ApiResponse;
3232
import io.swagger.v3.oas.models.responses.ApiResponses;
33+
import java.util.stream.Collectors;
3334
import org.apache.commons.lang3.StringUtils;
3435
import org.openapitools.codegen.utils.ModelUtils;
3536
import org.slf4j.Logger;
@@ -417,7 +418,18 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
417418
if (schema.getAllOf().size() == 1) {
418419
// handle earlier in this function when looping through properties
419420
} else if (schema.getAllOf().size() > 1) {
420-
LOGGER.warn("allOf schema `{}` containing multiple types (not model) is not supported at the moment.", schema.getName());
421+
// Check if there is only one "non metadata" schema.
422+
// For example, there may be an `description` only schema that is used to override the descrption.
423+
// In these cases, we can simply discard those schemas.
424+
List<Schema> nonMetadataOnlySchemas = (List<Schema>) schema.getAllOf().stream()
425+
.filter(v -> ModelUtils.isMetadataOnlySchema((Schema) v))
426+
.collect(Collectors.toList());
427+
428+
if (nonMetadataOnlySchemas.size() == 1) {
429+
schema.setAllOf(nonMetadataOnlySchemas);
430+
} else {
431+
LOGGER.warn("allOf schema `{}` containing multiple types (not model) is not supported at the moment.", schema.getName());
432+
}
421433
} else {
422434
LOGGER.error("allOf schema `{}` contains no items.", schema.getName());
423435
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,32 @@ public static boolean isUnsupportedSchema(OpenAPI openAPI, Schema schema) {
23362336
return false;
23372337
}
23382338

2339+
/**
2340+
* Returns true if a schema is only metadata and not an actual type.
2341+
* For example, a schema that only has a `description` without any `properties` or `$ref` defined.
2342+
*
2343+
* @param schema the schema
2344+
* @return if the schema is only metadata and not an actual type
2345+
*/
2346+
public static boolean isMetadataOnlySchema(Schema schema) {
2347+
return schema.get$ref() != null ||
2348+
schema.getProperties() != null ||
2349+
schema.getType() != null ||
2350+
schema.getAdditionalProperties() != null ||
2351+
schema.getAllOf() != null ||
2352+
schema.getAnyOf() != null ||
2353+
schema.getOneOf() != null ||
2354+
schema.getPrefixItems() != null ||
2355+
schema.getItems() != null ||
2356+
schema.getTypes() != null ||
2357+
schema.getPatternProperties() != null ||
2358+
schema.getContains() != null ||
2359+
schema.get$dynamicAnchor() != null ||
2360+
schema.get$anchor() != null ||
2361+
schema.getContentSchema() != null;
2362+
}
2363+
2364+
23392365
@FunctionalInterface
23402366
private interface OpenAPISchemaVisitor {
23412367

modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,22 @@ components:
10881088
properties:
10891089
name:
10901090
type: string
1091+
existing_tags_array:
1092+
type: array
1093+
items:
1094+
type: string
1095+
nullable: true
1096+
description: 'existing_tags_array'
1097+
example:
1098+
- base-image
1099+
- prod
1100+
TestAllOfWithMultiMetadataOnly:
1101+
type: object
1102+
properties:
1103+
id:
1104+
type: integer
1105+
format: int64
1106+
foo:
1107+
allOf:
1108+
- $ref: '#/components/schemas/existing_tags_array'
1109+
- description: This is a test for allOf with metadata only fields

samples/client/petstore/gdscript/addons/oas.petstore.client/models/DemoActionContainerModel.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class_name DemoActionContainerModel
1212

1313
# Required: True
1414
# isArray: false
15-
@export var action: DemoBazModel:
15+
@export var action: Baz:
1616
set(value):
1717
__action__was__set = true
1818
action = value
@@ -37,7 +37,7 @@ func bzz_normalize() -> Dictionary:
3737
static func bzz_denormalize_single(from_dict: Dictionary):
3838
var me := new()
3939
if from_dict.has("action"):
40-
me.action = DemoBazModel.bzz_denormalize_single(from_dict["action"])
40+
me.action = from_dict["action"]
4141
return me
4242

4343

samples/client/petstore/rust/hyper/petstore/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ docs/Ref.md
2424
docs/Return.md
2525
docs/StoreApi.md
2626
docs/Tag.md
27+
docs/TestAllOfWithMultiMetadataOnly.md
2728
docs/TestingApi.md
2829
docs/TestsDiscriminatorDuplicateEnumsGet200Response.md
2930
docs/TypeTesting.md
@@ -63,6 +64,7 @@ src/models/person.rs
6364
src/models/pet.rs
6465
src/models/property_test.rs
6566
src/models/tag.rs
67+
src/models/test_all_of_with_multi_metadata_only.rs
6668
src/models/type_testing.rs
6769
src/models/unique_item_array_testing.rs
6870
src/models/user.rs

samples/client/petstore/rust/hyper/petstore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Class | Method | HTTP request | Description
7474
- [Ref](docs/Ref.md)
7575
- [Return](docs/Return.md)
7676
- [Tag](docs/Tag.md)
77+
- [TestAllOfWithMultiMetadataOnly](docs/TestAllOfWithMultiMetadataOnly.md)
7778
- [TestsDiscriminatorDuplicateEnumsGet200Response](docs/TestsDiscriminatorDuplicateEnumsGet200Response.md)
7879
- [TypeTesting](docs/TypeTesting.md)
7980
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TestAllOfWithMultiMetadataOnly
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**id** | Option<**i64**> | | [optional]
8+
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
9+
10+
[[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+
12+

samples/client/petstore/rust/hyper/petstore/src/models/action_container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use serde::{Deserialize, Serialize};
1414
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
1515
pub struct ActionContainer {
1616
#[serde(rename = "action")]
17-
pub action: Box<models::Baz>,
17+
pub action: models::Baz,
1818
}
1919

2020
impl ActionContainer {
2121
pub fn new(action: models::Baz) -> ActionContainer {
2222
ActionContainer {
23-
action: Box::new(action),
23+
action,
2424
}
2525
}
2626
}

samples/client/petstore/rust/hyper/petstore/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub mod model_return;
3636
pub use self::model_return::Return;
3737
pub mod tag;
3838
pub use self::tag::Tag;
39+
pub mod test_all_of_with_multi_metadata_only;
40+
pub use self::test_all_of_with_multi_metadata_only::TestAllOfWithMultiMetadataOnly;
3941
pub mod _tests_discriminator_duplicate_enums_get_200_response;
4042
pub use self::_tests_discriminator_duplicate_enums_get_200_response::TestsDiscriminatorDuplicateEnumsGet200Response;
4143
pub mod type_testing;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
use crate::models;
12+
use serde::{Deserialize, Serialize};
13+
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct TestAllOfWithMultiMetadataOnly {
16+
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
17+
pub id: Option<i64>,
18+
/// existing_tags_array
19+
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
20+
pub foo: Option<Option<Vec<String>>>,
21+
}
22+
23+
impl TestAllOfWithMultiMetadataOnly {
24+
pub fn new() -> TestAllOfWithMultiMetadataOnly {
25+
TestAllOfWithMultiMetadataOnly {
26+
id: None,
27+
foo: None,
28+
}
29+
}
30+
}
31+

samples/client/petstore/rust/hyper0x/petstore/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ docs/Ref.md
2424
docs/Return.md
2525
docs/StoreApi.md
2626
docs/Tag.md
27+
docs/TestAllOfWithMultiMetadataOnly.md
2728
docs/TestingApi.md
2829
docs/TestsDiscriminatorDuplicateEnumsGet200Response.md
2930
docs/TypeTesting.md
@@ -61,6 +62,7 @@ src/models/person.rs
6162
src/models/pet.rs
6263
src/models/property_test.rs
6364
src/models/tag.rs
65+
src/models/test_all_of_with_multi_metadata_only.rs
6466
src/models/type_testing.rs
6567
src/models/unique_item_array_testing.rs
6668
src/models/user.rs

samples/client/petstore/rust/hyper0x/petstore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Class | Method | HTTP request | Description
7474
- [Ref](docs/Ref.md)
7575
- [Return](docs/Return.md)
7676
- [Tag](docs/Tag.md)
77+
- [TestAllOfWithMultiMetadataOnly](docs/TestAllOfWithMultiMetadataOnly.md)
7778
- [TestsDiscriminatorDuplicateEnumsGet200Response](docs/TestsDiscriminatorDuplicateEnumsGet200Response.md)
7879
- [TypeTesting](docs/TypeTesting.md)
7980
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TestAllOfWithMultiMetadataOnly
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**id** | Option<**i64**> | | [optional]
8+
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
9+
10+
[[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+
12+

samples/client/petstore/rust/hyper0x/petstore/src/models/action_container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use serde::{Deserialize, Serialize};
1414
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
1515
pub struct ActionContainer {
1616
#[serde(rename = "action")]
17-
pub action: Box<models::Baz>,
17+
pub action: models::Baz,
1818
}
1919

2020
impl ActionContainer {
2121
pub fn new(action: models::Baz) -> ActionContainer {
2222
ActionContainer {
23-
action: Box::new(action),
23+
action,
2424
}
2525
}
2626
}

samples/client/petstore/rust/hyper0x/petstore/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub mod model_return;
3636
pub use self::model_return::Return;
3737
pub mod tag;
3838
pub use self::tag::Tag;
39+
pub mod test_all_of_with_multi_metadata_only;
40+
pub use self::test_all_of_with_multi_metadata_only::TestAllOfWithMultiMetadataOnly;
3941
pub mod _tests_discriminator_duplicate_enums_get_200_response;
4042
pub use self::_tests_discriminator_duplicate_enums_get_200_response::TestsDiscriminatorDuplicateEnumsGet200Response;
4143
pub mod type_testing;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
use crate::models;
12+
use serde::{Deserialize, Serialize};
13+
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct TestAllOfWithMultiMetadataOnly {
16+
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
17+
pub id: Option<i64>,
18+
/// existing_tags_array
19+
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
20+
pub foo: Option<Option<Vec<String>>>,
21+
}
22+
23+
impl TestAllOfWithMultiMetadataOnly {
24+
pub fn new() -> TestAllOfWithMultiMetadataOnly {
25+
TestAllOfWithMultiMetadataOnly {
26+
id: None,
27+
foo: None,
28+
}
29+
}
30+
}
31+

samples/client/petstore/rust/reqwest-trait/petstore/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ docs/Ref.md
2424
docs/Return.md
2525
docs/StoreApi.md
2626
docs/Tag.md
27+
docs/TestAllOfWithMultiMetadataOnly.md
2728
docs/TestingApi.md
2829
docs/TestsDiscriminatorDuplicateEnumsGet200Response.md
2930
docs/TypeTesting.md
@@ -61,6 +62,7 @@ src/models/person.rs
6162
src/models/pet.rs
6263
src/models/property_test.rs
6364
src/models/tag.rs
65+
src/models/test_all_of_with_multi_metadata_only.rs
6466
src/models/type_testing.rs
6567
src/models/unique_item_array_testing.rs
6668
src/models/user.rs

samples/client/petstore/rust/reqwest-trait/petstore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Class | Method | HTTP request | Description
7474
- [Ref](docs/Ref.md)
7575
- [Return](docs/Return.md)
7676
- [Tag](docs/Tag.md)
77+
- [TestAllOfWithMultiMetadataOnly](docs/TestAllOfWithMultiMetadataOnly.md)
7778
- [TestsDiscriminatorDuplicateEnumsGet200Response](docs/TestsDiscriminatorDuplicateEnumsGet200Response.md)
7879
- [TypeTesting](docs/TypeTesting.md)
7980
- [UniqueItemArrayTesting](docs/UniqueItemArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TestAllOfWithMultiMetadataOnly
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**id** | Option<**i64**> | | [optional]
8+
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
9+
10+
[[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+
12+

samples/client/petstore/rust/reqwest-trait/petstore/src/models/action_container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use serde::{Deserialize, Serialize};
1414
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
1515
pub struct ActionContainer {
1616
#[serde(rename = "action")]
17-
pub action: Box<models::Baz>,
17+
pub action: models::Baz,
1818
}
1919

2020
impl ActionContainer {
2121
pub fn new(action: models::Baz) -> ActionContainer {
2222
ActionContainer {
23-
action: Box::new(action),
23+
action,
2424
}
2525
}
2626
}

samples/client/petstore/rust/reqwest-trait/petstore/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub mod model_return;
3636
pub use self::model_return::Return;
3737
pub mod tag;
3838
pub use self::tag::Tag;
39+
pub mod test_all_of_with_multi_metadata_only;
40+
pub use self::test_all_of_with_multi_metadata_only::TestAllOfWithMultiMetadataOnly;
3941
pub mod _tests_discriminator_duplicate_enums_get_200_response;
4042
pub use self::_tests_discriminator_duplicate_enums_get_200_response::TestsDiscriminatorDuplicateEnumsGet200Response;
4143
pub mod type_testing;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
use crate::models;
12+
use serde::{Deserialize, Serialize};
13+
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct TestAllOfWithMultiMetadataOnly {
16+
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
17+
pub id: Option<i64>,
18+
/// existing_tags_array
19+
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
20+
pub foo: Option<Option<Vec<String>>>,
21+
}
22+
23+
impl TestAllOfWithMultiMetadataOnly {
24+
pub fn new() -> TestAllOfWithMultiMetadataOnly {
25+
TestAllOfWithMultiMetadataOnly {
26+
id: None,
27+
foo: None,
28+
}
29+
}
30+
}
31+

samples/client/petstore/rust/reqwest/petstore-async-middleware/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ docs/Ref.md
2424
docs/Return.md
2525
docs/StoreApi.md
2626
docs/Tag.md
27+
docs/TestAllOfWithMultiMetadataOnly.md
2728
docs/TestingApi.md
2829
docs/TestsDiscriminatorDuplicateEnumsGet200Response.md
2930
docs/TypeTesting.md
@@ -61,6 +62,7 @@ src/models/person.rs
6162
src/models/pet.rs
6263
src/models/property_test.rs
6364
src/models/tag.rs
65+
src/models/test_all_of_with_multi_metadata_only.rs
6466
src/models/type_testing.rs
6567
src/models/unique_item_array_testing.rs
6668
src/models/user.rs

0 commit comments

Comments
 (0)