Never create inline model for allOf with single $ref (#18945)

Fixes #15077

The previous fix for this in #16096 is incomplete because it still
generates unnecessary inline models when readOnly or
nullable is used in conjunction with other properties like
description.
This commit fixes the logic error and adds testcases.
This commit is contained in:
ReneZeidler
2024-06-19 11:15:33 +02:00
committed by GitHub
parent da57701569
commit 5bc7aa3cd6
7 changed files with 169 additions and 5 deletions

View File

@@ -231,7 +231,9 @@ public class InlineModelResolver {
if (schema.equals(c)) {
return isModelNeeded((Schema) schema.getAllOf().get(0), visitedSchemas);
}
} else if (isSingleAllOf && StringUtils.isNotEmpty(((Schema) schema.getAllOf().get(0)).get$ref())) {
}
if (isSingleAllOf && StringUtils.isNotEmpty(((Schema) schema.getAllOf().get(0)).get$ref())) {
// single allOf and it's a ref
return isModelNeeded((Schema) schema.getAllOf().get(0), visitedSchemas);
}

View File

@@ -1171,4 +1171,30 @@ public class InlineModelResolverTest {
((Schema) inlineFormParaemter.getProperties().get("enum_form_string")).get$ref());
}
@Test
public void doNotWrapSingleAllOfRefs() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/issue_15077.yaml");
new InlineModelResolver().flatten(openAPI);
// None of these cases should be wrapped in an inline schema and should reference the original schema "NumberRange"
Schema limitsModel = (Schema) openAPI.getComponents().getSchemas().get("Limits");
final String numberRangeRef = "#/components/schemas/NumberRange";
Schema allOfRef = (Schema) limitsModel.getProperties().get("allOfRef");
assertNotNull(allOfRef.getAllOf());
assertEquals(numberRangeRef, ((Schema) allOfRef.getAllOf().get(0)).get$ref());
Schema allOfRefWithDescription = (Schema) limitsModel.getProperties().get("allOfRefWithDescription");
assertNotNull(allOfRefWithDescription.getAllOf());
assertEquals(numberRangeRef, ((Schema) allOfRefWithDescription.getAllOf().get(0)).get$ref());
Schema allOfRefWithReadonly = (Schema) limitsModel.getProperties().get("allOfRefWithReadonly");
assertNotNull(allOfRefWithReadonly.getAllOf());
assertEquals(numberRangeRef, ((Schema) allOfRefWithReadonly.getAllOf().get(0)).get$ref());
Schema allOfRefWithDescriptionAndReadonly = (Schema) limitsModel.getProperties().get("allOfRefWithDescriptionAndReadonly");
assertNotNull(allOfRefWithDescriptionAndReadonly.getAllOf());
assertEquals(numberRangeRef, ((Schema) allOfRefWithDescriptionAndReadonly.getAllOf().get(0)).get$ref());
}
}

View File

@@ -0,0 +1,53 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Property refs using allOf
paths:
/limits:
get:
operationId: getLimits
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Limits'
components:
schemas:
NumberRange:
type: object
properties:
min:
type: number
max:
type: number
required:
- min
- max
Limits:
type: object
properties:
allOfRef:
allOf:
- $ref: '#/components/schemas/NumberRange'
allOfRefWithDescription:
description: |
Description for this property
allOf:
- $ref: '#/components/schemas/NumberRange'
allOfRefWithReadonly:
readOnly: true
allOf:
- $ref: '#/components/schemas/NumberRange'
allOfRefWithDescriptionAndReadonly:
description: |
Description for this readonly property
readOnly: true
allOf:
- $ref: '#/components/schemas/NumberRange'
required:
- allOfRef
- allOfRefWithDescription
- allOfRefWithReadonly
- allOfRefWithDescriptionAndReadonly

View File

@@ -1217,7 +1217,7 @@ paths:
operationId: getParameterNameMapping
parameters:
- name: _type
in: header
in: header
description: _type
required: true
schema:
@@ -1230,7 +1230,7 @@ paths:
schema:
type: string
- name: type_
in: header
in: header
description: type_
required: true
schema:
@@ -1246,7 +1246,7 @@ paths:
operationId: getParameterStringNumber
parameters:
- name: string_number
in: header
in: header
description: string number
required: true
schema:
@@ -2600,6 +2600,17 @@ components:
category_allOf_ref:
allOf:
- $ref: '#/components/schemas/Category'
category_allOf_ref_description:
description: |
Adding description to property using allOf
allOf:
- $ref: '#/components/schemas/Category'
category_allOf_ref_description_readonly:
description: |
Adding description to readonly property using allOf
readOnly: true
allOf:
- $ref: '#/components/schemas/Category'
name:
type: string
example: doggie