Skip to content

Better handling of metadata in allOf #21342

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 2 commits into from
May 30, 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 @@ -418,18 +418,7 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (schema.getAllOf().size() == 1) {
// handle earlier in this function when looping through properties
} else if (schema.getAllOf().size() > 1) {
// Check if there is only one "non metadata" schema.
// For example, there may be an `description` only schema that is used to override the descrption.
// In these cases, we can simply discard those schemas.
List<Schema> nonMetadataOnlySchemas = (List<Schema>) schema.getAllOf().stream()
.filter(v -> ModelUtils.isMetadataOnlySchema((Schema) v))
.collect(Collectors.toList());

if (nonMetadataOnlySchemas.size() == 1) {
schema.setAllOf(nonMetadataOnlySchemas);
} else {
LOGGER.warn("allOf schema `{}` containing multiple types (not model) is not supported at the moment.", schema.getName());
}
LOGGER.warn("allOf schema `{}` containing multiple types (not model) is not supported at the moment.", schema.getName());
} else {
LOGGER.error("allOf schema `{}` contains no items.", schema.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,52 @@ protected void normalizeProperties(Map<String, Schema> properties, Set<Schema> v
}
}

protected void refactorAllOfWithMetadataOnlySchemas(Schema schema) {
if (schema.getAllOf() == null) {
return;
}

// Check if there are metadata schemas.
// For example, there may be an `description` only schema that is used to override the descrption.
List<Schema> nonMetadataOnlySchemas = new ArrayList<>();
List<Schema> metadataOnlySchemas = new ArrayList<>();

for (Object s: schema.getAllOf()) {
if (s instanceof Schema) {
if (ModelUtils.isMetadataOnlySchema((Schema) s)) {
metadataOnlySchemas.add((Schema) s);
} else {
nonMetadataOnlySchemas.add((Schema) s);
}
}
}

// ReferenceNumber:
// allOf:
// - $ref: '#/components/schemas/IEAN8'
// - description: Product Ref
// - example: IEAN1234
//
// becomes the following after the following code block
//
// ReferenceNumber:
// allOf:
// - $ref: '#/components/schemas/IEAN8'
// description: Product Ref
// example: IEAN1234
//
// which can be further processed by the generator
if (nonMetadataOnlySchemas.size() > 0) {
// keep the non metadata schema(s)
schema.setAllOf(nonMetadataOnlySchemas);

// copy metadata to the allOf schema
for (Schema metadataOnlySchema: metadataOnlySchemas) {
ModelUtils.copyMetadata(metadataOnlySchema, schema);
}
}
}

/*
* Remove unsupported schemas (e.g. if, then) from allOf.
*
Expand Down Expand Up @@ -882,6 +928,8 @@ protected void removeUnsupportedSchemasFromAllOf(Schema schema) {
protected Schema normalizeAllOf(Schema schema, Set<Schema> visitedSchemas) {
removeUnsupportedSchemasFromAllOf(schema);

refactorAllOfWithMetadataOnlySchemas(schema);

if (schema.getAllOf() == null) {
return schema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,63 @@ public static boolean isUnsupportedSchema(OpenAPI openAPI, Schema schema) {
return false;
}

/**
* Copy meta data (e.g. description, default, examples, etc) from one schema to another.
*
* @param from From schema
* @param to To schema
*/
public static void copyMetadata(Schema from, Schema to) {
if (from.getDescription() != null) {
to.setDescription(from.getDescription());
}
if (from.getDefault() != null) {
to.setDefault(from.getDefault());
}
if (from.getDeprecated() != null) {
to.setDeprecated(from.getDeprecated());
}
if (from.getNullable() != null) {
to.setNullable(from.getNullable());
}
if (from.getExample() != null) {
to.setExample(from.getExample());
}
if (from.getExamples() != null) {
to.setExample(from.getExamples());
}
if (from.getReadOnly() != null) {
to.setReadOnly(from.getReadOnly());
}
if (from.getWriteOnly() != null) {
to.setWriteOnly(from.getWriteOnly());
}
if (from.getExtensions() != null) {
to.setExtensions(from.getExtensions());
}
if (from.getMaxLength() != null) {
to.setMaxLength(from.getMaxLength());
}
if (from.getMinLength() != null) {
to.setMinLength(from.getMinLength());
}
if (from.getMaxItems() != null) {
to.setMaxItems(from.getMaxItems());
}
if (from.getMinItems() != null) {
to.setMinItems(from.getMinItems());
}
if (from.getMaximum() != null) {
to.setMaximum(from.getMaximum());
}
if (from.getMinimum() != null) {
to.setMinimum(from.getMinimum());
}
if (from.getTitle() != null) {
to.setTitle(from.getTitle());
}
}

/**
* Returns true if a schema is only metadata and not an actual type.
* For example, a schema that only has a `description` without any `properties` or `$ref` defined.
Expand All @@ -2361,7 +2418,7 @@ public static boolean isUnsupportedSchema(OpenAPI openAPI, Schema schema) {
* @return if the schema is only metadata and not an actual type
*/
public static boolean isMetadataOnlySchema(Schema schema) {
return schema.get$ref() != null ||
return !(schema.get$ref() != null ||
schema.getProperties() != null ||
schema.getType() != null ||
schema.getAdditionalProperties() != null ||
Expand All @@ -2375,7 +2432,7 @@ public static boolean isMetadataOnlySchema(Schema schema) {
schema.getContains() != null ||
schema.get$dynamicAnchor() != null ||
schema.get$anchor() != null ||
schema.getContentSchema() != null;
schema.getContentSchema() != null);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithPropertie
assertNull(schema4.getExtensions());
}

@Test
public void testOpenAPINormalizerRefactorAllofWithMetadataOnlySchemas() {
// to test the rule REF_AS_PARENT_IN_ALLOF
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allof_with_metadata_only_schemas.yaml");

Schema schema = openAPI.getComponents().getSchemas().get("ReferenceNumber");
assertEquals(schema.getAllOf().size(), 3);
assertEquals(((Schema) schema.getAllOf().get(2)).getExample(), "IEAN1234");

Map<String, String> options = new HashMap<>();
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
openAPINormalizer.normalize();

Schema schema2 = openAPI.getComponents().getSchemas().get("ReferenceNumber");
assertEquals(schema2.getAllOf().size(), 1);
assertEquals(schema2.getExample(), "IEAN1234");
assertEquals(((Schema) schema2.getAllOf().get(0)).get$ref(), "#/components/schemas/IEAN8");
}

@Test
public void testOpenAPINormalizerEnableKeepOnlyFirstTagInOperation() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
openapi: 3.0.3
info:
version: 1.0.0
title: Test
paths:
/api/productRef:
get:
operationId: getProductRef
summary: Retrieve product reference
description: Returns a product reference based on a given product.
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ProductType'
components:
schemas:
ProductType:
type: object
required:
- referenceNumber
properties:
referenceNumber:
$ref: "#/components/schemas/ReferenceNumber"
ReferenceNumber:
allOf:
- $ref: "#/components/schemas/IEAN8"
- description: Product Ref
- example: IEAN1234
IEAN8:
type: string
minLength: 8
maxLength: 8
example: "IEAN1234"
Order:
type: object
properties:
id:
type: integer
format: int64
foo:
allOf:
- $ref: '#/components/schemas/ProductType'
- description: 'this is foo'
- example: 'this is bar'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct TestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | Option<**i64**> | | [optional]
**foo** | Option<**Vec<String>**> | existing_tags_array | [optional]
**foo** | Option<**Vec<String>**> | This is a test for allOf with metadata only fields | [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
pub struct FooTestAllOfWithMultiMetadataOnly {
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// existing_tags_array
/// This is a test for allOf with metadata only fields
#[serde(rename = "foo", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub foo: Option<Option<Vec<String>>>,
}
Expand Down
Loading
Loading