make title available to templates for ref properties (#17881)

Even though `title` is technically for the title of the API itself,
it's possible to set it for properties as well.
Right now, the `title` property is available in templates for regular properties,
but for ref-properties it gets lost right here.
This seemed rather surprising to me, so I think it's better to fix.
This commit is contained in:
Felix König 2024-02-19 04:06:26 +01:00 committed by GitHub
parent 28b75377b5
commit 6350d1d191
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View File

@ -4235,6 +4235,9 @@ public class DefaultCodegen implements CodegenConfig {
if (original.getMinimum() != null) { if (original.getMinimum() != null) {
property.setMinimum(String.valueOf(original.getMinimum().doubleValue())); property.setMinimum(String.valueOf(original.getMinimum().doubleValue()));
} }
if (original.getTitle() != null) {
property.setTitle(original.getTitle());
}
} }
// set the default value // set the default value

View File

@ -1916,6 +1916,19 @@ public class DefaultCodegenTest {
Assert.assertFalse(codegen.fromProperty("customerCode", (Schema) requestProperties.get("customerCode")).deprecated); Assert.assertFalse(codegen.fromProperty("customerCode", (Schema) requestProperties.get("customerCode")).deprecated);
} }
@Test
public void testTitleProperty() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/property-title.yaml");
new InlineModelResolver().flatten(openAPI);
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
final Map testProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("ModelWithTitledProperties").getProperties());
Assert.assertEquals("Simple-Property-Title", codegen.fromProperty("simpleProperty", (Schema) testProperties.get("simpleProperty")).title);
Assert.assertEquals("Ref-Property-Title", codegen.fromProperty("refProperty", (Schema) testProperties.get("refProperty")).title);
}
@Test @Test
public void testDeprecatedRef() { public void testDeprecatedRef() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/model-deprecated.yaml"); final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/model-deprecated.yaml");

View File

@ -0,0 +1,29 @@
openapi: 3.0.1
paths:
/foo:
get:
responses:
'200':
description: ok
content:
application/json:
schema:
$ref: '#/components/schemas/test'
info:
title: Title on Properties
version: "1.0"
components:
schemas:
ModelWithTitledProperties:
properties:
simpleProperty:
type: string
title: Simple-Property-Title
refProperty:
type: string
title: Ref-Property-Title
allOf:
- $ref: '#/components/schemas/RefObject'
type: object
RefObject:
type: string