diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 3eef324a7bdb..c19703a3e760 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -1976,6 +1976,20 @@ public void testTitleProperty() { final Map testProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("ModelWithTitledProperties").getProperties()); Assertions.assertEquals("Simple-Property-Title", codegen.fromProperty("simpleProperty", (Schema) testProperties.get("simpleProperty")).title); + Assertions.assertEquals("All-Of-Ref-Property-Title", codegen.fromProperty("refProperty", (Schema) testProperties.get("allOfRefProperty")).title); + } + + @Test + public void testTitlePropertyOAS31refSibling() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_1/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()); + + Assertions.assertEquals("Simple-Property-Title", codegen.fromProperty("simpleProperty", (Schema) testProperties.get("simpleProperty")).title); + Assertions.assertEquals("All-Of-Ref-Property-Title", codegen.fromProperty("refProperty", (Schema) testProperties.get("allOfRefProperty")).title); Assertions.assertEquals("Ref-Property-Title", codegen.fromProperty("refProperty", (Schema) testProperties.get("refProperty")).title); } @@ -5009,4 +5023,25 @@ public void testSingleRequestParameter_hasSingleParamTrue() { // When & Then assertThat(codegenOperation.hasSingleParam).isTrue(); } + + /** + * Starting with OpenAPI 3.1, it is legal to have a $ref be a sibling to other properties. + * This test ensures that any sibling properties, "title" in this case, do not get lost while resolving the ref. + */ + @Test + public void testSiblingPropertyWithRefInOAS31() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_1/property-title.yaml"); + final DefaultCodegen codegen = new DefaultCodegen(); + codegen.setOpenAPI(openAPI); + Map properties = openAPI.getComponents().getSchemas().get("ModelWithTitledProperties").getProperties(); + + // These always worked + Assert.assertEquals("Simple-Property-Title", + codegen.fromModel("dummy", properties.get("simpleProperty")).getTitle()); + Assert.assertEquals("All-Of-Ref-Property-Title", + codegen.fromModel("dummy", properties.get("allOfRefProperty")).getTitle()); + // This is new in OpenAPI 3.1, and should work now: "title" is a sibling to the "$ref" and should not be lost + Assert.assertEquals("Ref-Property-Title", + codegen.fromModel("dummy", properties.get("refProperty")).getTitle()); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/property-title.yaml b/modules/openapi-generator/src/test/resources/3_0/property-title.yaml index a7474f08afe7..4c0ba04e761f 100644 --- a/modules/openapi-generator/src/test/resources/3_0/property-title.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/property-title.yaml @@ -19,9 +19,9 @@ components: simpleProperty: type: string title: Simple-Property-Title - refProperty: + allOfRefProperty: type: string - title: Ref-Property-Title + title: All-Of-Ref-Property-Title allOf: - $ref: '#/components/schemas/RefObject' type: object diff --git a/modules/openapi-generator/src/test/resources/3_1/property-title.yaml b/modules/openapi-generator/src/test/resources/3_1/property-title.yaml new file mode 100644 index 000000000000..8047fc8ab15e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/property-title.yaml @@ -0,0 +1,33 @@ +openapi: 3.1.0 +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 + $ref: '#/components/schemas/RefObject' + allOfRefProperty: + type: string + title: All-Of-Ref-Property-Title + allOf: + - $ref: '#/components/schemas/RefObject' + type: object + RefObject: + type: string