Skip to content

Commit

Permalink
add test showcasing ref property siblings being lost in OpenAPI 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Felk committed Dec 12, 2024
1 parent d29196a commit 0c8dbe5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<String, Schema> 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",

Check failure on line 5044 in modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test results

org.openapitools.codegen.DefaultCodegenTest ► testSiblingPropertyWithRefInOAS31

Failed test found in: modules/openapi-generator/target/surefire-reports/TEST-org.openapitools.codegen.DefaultCodegenTest.xml Error: java.lang.AssertionError: expected [null] but found [Ref-Property-Title]
Raw output
java.lang.AssertionError: expected [null] but found [Ref-Property-Title]
	at org.testng.Assert.fail(Assert.java:111)
	at org.testng.Assert.failNotEquals(Assert.java:1578)
	at org.testng.Assert.assertEqualsImpl(Assert.java:150)
	at org.testng.Assert.assertEquals(Assert.java:132)
	at org.testng.Assert.assertEquals(Assert.java:656)
	at org.testng.Assert.assertEquals(Assert.java:666)
	at org.openapitools.codegen.DefaultCodegenTest.testSiblingPropertyWithRefInOAS31(DefaultCodegenTest.java:5044)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:141)
	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:686)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:230)
	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:63)
	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:992)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:203)
	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:154)
	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:134)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.testng.TestRunner.privateRun(TestRunner.java:739)
	at org.testng.TestRunner.run(TestRunner.java:614)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:421)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:413)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:373)
	at org.testng.SuiteRunner.run(SuiteRunner.java:312)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1274)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1208)
	at org.testng.TestNG.runSuites(TestNG.java:1112)
	at org.testng.TestNG.run(TestNG.java:1079)
codegen.fromModel("dummy", properties.get("refProperty")).getTitle());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0c8dbe5

Please sign in to comment.