forked from loafle/openapi-generator-original
fix allOf with properties for ref as parent rule (#20083)
This commit is contained in:
parent
a95ea1f519
commit
301af6050b
@ -755,6 +755,9 @@ public class OpenAPINormalizer {
|
|||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process rule to refactor properties into allOf sub-schema
|
||||||
|
schema = processRefactorAllOfWithPropertiesOnly(schema);
|
||||||
|
|
||||||
for (Object item : schema.getAllOf()) {
|
for (Object item : schema.getAllOf()) {
|
||||||
if (!(item instanceof Schema)) {
|
if (!(item instanceof Schema)) {
|
||||||
throw new RuntimeException("Error! allOf schema is not of the type Schema: " + item);
|
throw new RuntimeException("Error! allOf schema is not of the type Schema: " + item);
|
||||||
@ -762,8 +765,6 @@ public class OpenAPINormalizer {
|
|||||||
// normalize allOf sub schemas one by one
|
// normalize allOf sub schemas one by one
|
||||||
normalizeSchema((Schema) item, visitedSchemas);
|
normalizeSchema((Schema) item, visitedSchemas);
|
||||||
}
|
}
|
||||||
// process rules here
|
|
||||||
schema = processRefactorAllOfWithPropertiesOnly(schema);
|
|
||||||
|
|
||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
@ -1325,9 +1326,8 @@ public class OpenAPINormalizer {
|
|||||||
schema.setTitle(null);
|
schema.setTitle(null);
|
||||||
|
|
||||||
// at this point the schema becomes a simple allOf (no properties) with an additional schema containing
|
// at this point the schema becomes a simple allOf (no properties) with an additional schema containing
|
||||||
// the properties
|
// the properties. Normalize it before returning.
|
||||||
|
return normalizeSchema(schema, new HashSet<>());
|
||||||
return schema;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,6 +56,30 @@ public class OpenAPINormalizerTest {
|
|||||||
assertEquals(schema5.getExtensions().get("x-parent"), "abstract");
|
assertEquals(schema5.getExtensions().get("x-parent"), "abstract");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithProperties() {
|
||||||
|
// to test the both REF_AS_PARENT_IN_ALLOF and REFACTOR_ALLOF_WITH_PROPERTIES_ONLY
|
||||||
|
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_extension_parent.yaml");
|
||||||
|
|
||||||
|
Schema schema = openAPI.getComponents().getSchemas().get("Child");
|
||||||
|
assertNull(schema.getExtensions());
|
||||||
|
|
||||||
|
Schema schema2 = openAPI.getComponents().getSchemas().get("Ancestor");
|
||||||
|
assertNull(schema2.getExtensions());
|
||||||
|
|
||||||
|
Map<String, String> options = new HashMap<>();
|
||||||
|
options.put("REF_AS_PARENT_IN_ALLOF", "true");
|
||||||
|
options.put("REFACTOR_ALLOF_WITH_PROPERTIES_ONLY", "true");
|
||||||
|
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
|
||||||
|
openAPINormalizer.normalize();
|
||||||
|
|
||||||
|
Schema schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
|
||||||
|
assertEquals(schema3.getExtensions().get("x-parent"), true);
|
||||||
|
|
||||||
|
Schema schema4 = openAPI.getComponents().getSchemas().get("Child");
|
||||||
|
assertNull(schema4.getExtensions());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOpenAPINormalizerEnableKeepOnlyFirstTagInOperation() {
|
public void testOpenAPINormalizerEnableKeepOnlyFirstTagInOperation() {
|
||||||
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");
|
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");
|
||||||
|
@ -1776,7 +1776,7 @@ public class JavaClientCodegenTest {
|
|||||||
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
|
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
|
||||||
|
|
||||||
validateJavaSourceFiles(files);
|
validateJavaSourceFiles(files);
|
||||||
assertThat(files).hasSize(27);
|
assertThat(files).hasSize(33);
|
||||||
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
|
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
|
||||||
.content().contains("public class Child extends Person {");
|
.content().contains("public class Child extends Person {");
|
||||||
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
|
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
|
||||||
|
@ -101,4 +101,15 @@ components:
|
|||||||
description: allOf with a single item
|
description: allOf with a single item
|
||||||
nullable: true
|
nullable: true
|
||||||
allOf:
|
allOf:
|
||||||
- $ref: '#/components/schemas/AnotherParent'
|
- $ref: '#/components/schemas/AnotherParent'
|
||||||
|
Ancestor:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
p1:
|
||||||
|
type: integer
|
||||||
|
Offspring:
|
||||||
|
properties:
|
||||||
|
p2:
|
||||||
|
type: string
|
||||||
|
allOf:
|
||||||
|
- $ref: '#/components/schemas/Ancestor'
|
Loading…
x
Reference in New Issue
Block a user