Skip to content

Commit bbc0fd0

Browse files
committed
Improvements
1 parent 18ac063 commit bbc0fd0

14 files changed

Lines changed: 261 additions & 271 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ protected Schema processReplaceOneOfByMapping(Schema schema) {
16031603
Schema oneOf = (Schema) oneOfObject;
16041604
String refSchema = oneOf.get$ref();
16051605
if (refSchema != null) {
1606-
String name = refSchema.contains("/") ? refSchema.substring(refSchema.lastIndexOf('/') + 1) : refSchema;
1606+
String name = getDiscriminatorValue(refSchema);
16071607
mappings.put(name, refSchema);
16081608
}
16091609
}
@@ -1615,12 +1615,26 @@ protected Schema processReplaceOneOfByMapping(Schema schema) {
16151615
return schema;
16161616
}
16171617

1618+
protected String getDiscriminatorValue(String refSchema) {
1619+
String schemaName = refSchema.contains("/") ? refSchema.substring(refSchema.lastIndexOf('/') + 1) : refSchema;;
1620+
Schema schema = ModelUtils.getSchema(openAPI, schemaName);
1621+
if (schema != null && schema.getExtensions() != null) {
1622+
String discriminatorValue = String.valueOf(schema.getExtensions().get("x-discriminator-value"));
1623+
if (discriminatorValue != null) {
1624+
return discriminatorValue;
1625+
}
1626+
}
1627+
return schemaName;
1628+
}
1629+
16181630

16191631
protected void ensureInheritance(Schema parent, String parentName) {
16201632
Discriminator discriminator = parent.getDiscriminator();
16211633
if (discriminator != null && discriminator.getMapping() != null) {
1622-
for (String mapping : discriminator.getMapping().keySet()) {
1623-
Schema child = ModelUtils.getSchema(openAPI, mapping);
1634+
1635+
for (String mapping : discriminator.getMapping().values()) {
1636+
String refSchemaName = getDiscriminatorValue(mapping);
1637+
Schema child = ModelUtils.getSchema(openAPI, refSchemaName);
16241638
if (child != null) {
16251639
ensureInheritance(parent, child, parentName);
16261640
}
@@ -1629,23 +1643,27 @@ protected void ensureInheritance(Schema parent, String parentName) {
16291643
}
16301644

16311645
protected void ensureInheritance(Schema parent, Schema child, String parentName) {
1646+
String reference = "#/components/schemas/" + parentName;
16321647
List<Schema> allOf = child.getAllOf();
16331648
if (allOf != null) {
1634-
if (hasParent(child, parent)) {
1649+
if (hasParent(child, parent, reference)) {
16351650
return;
16361651
}
16371652
} else {
16381653
allOf = new ArrayList<>();
16391654
child.setAllOf(allOf);
16401655
}
1641-
Schema refToParent = new Schema<>().$ref("#/components/schemas/" + parentName);
1656+
Schema refToParent = new Schema<>().$ref(reference);
16421657
allOf.add(refToParent);
16431658
}
16441659

1645-
private boolean hasParent(Schema child, Schema parent) {
1660+
private boolean hasParent(Schema child, Schema parent, String reference) {
1661+
if (child.get$ref() != null && child.get$ref().equals(reference)) {
1662+
return true;
1663+
}
16461664
List<Schema> allOf = child.getAllOf();
16471665
if (allOf != null) {
1648-
return allOf.stream().anyMatch(s -> hasParent(s, parent));
1666+
return allOf.stream().anyMatch(s -> hasParent(s, parent, reference));
16491667
}
16501668
return false;
16511669
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
15081508
@Test
15091509
public void testREPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING() {
15101510
// to test array schema processing in 3.1 spec
1511-
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/spring/issue_23527.yaml");
1511+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/spring/oneOf_issue_23527.yaml");
15121512

15131513
Map<String, String> inputRules = Map.of("REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING", "true");
15141514
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);

modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import io.swagger.v3.oas.models.media.Schema;
1616
import io.swagger.v3.oas.models.servers.Server;
1717
import io.swagger.v3.parser.core.models.ParseOptions;
18-
import org.apache.commons.io.FileUtils;
1918
import org.openapitools.codegen.java.assertions.JavaFileAssert;
2019
import org.openapitools.codegen.model.ModelMap;
2120
import org.openapitools.codegen.model.ModelsMap;
@@ -343,23 +342,14 @@ public static ModelsMap createCodegenModelWrapper(CodegenModel cm) {
343342
}
344343

345344
public static Path newTempFolder() {
346-
File file = new File("c:\\temp\\generated");
347-
file.mkdir();
345+
final Path tempDir;
348346
try {
349-
FileUtils.cleanDirectory(file);
347+
tempDir = Files.createTempDirectory("test");
350348
} catch (IOException e) {
351349
throw new RuntimeException(e);
352350
}
351+
tempDir.toFile().deleteOnExit();
353352

354-
return file.toPath();
355-
// final Path tempDir;
356-
// try {
357-
// tempDir = Files.createTempDirectory("test");
358-
// } catch (IOException e) {
359-
// throw new RuntimeException(e);
360-
// }
361-
// tempDir.toFile().deleteOnExit();
362-
//
363-
// return tempDir;
353+
return tempDir;
364354
}
365355
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4330,9 +4330,9 @@ public void testJspecify(String library, boolean useSpringBoot4, boolean hasJspe
43304330
@DataProvider(name = "replaceOneOf")
43314331
public Object[][] replaceOneOf() {
43324332
return new Object[][]{
4333-
{"src/test/resources/3_0/spring/issue_23527.yaml"},
4334-
{"src/test/resources/3_0/spring/issue_23527_1.yaml"},
4335-
{"src/test/resources/3_0/spring/issue_23527_2.yaml"}
4333+
{"src/test/resources/3_0/oneOf_issue_23527.yaml"},
4334+
{"src/test/resources/3_0/oneOf_issue_23527_1.yaml"},
4335+
{"src/test/resources/3_0/oneOf_issue_23527_2.yaml"}
43364336
};
43374337
}
43384338

@@ -4355,64 +4355,42 @@ void replaceOneOfByDiscriminatorMapping(String file) {
43554355
.fileContains("List<Double> coordinates");
43564356

43574357
}
4358-
@Test
4359-
void issue_23276() {
4360-
Map<String, File> files = generateFromContract("src/test/resources/3_0/java/issue_23276.yaml", APACHE, Map.of(),
4361-
codegen -> codegen.addOpenapiNormalizer("REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING", "true"));
4362-
4363-
JavaFileAssert.assertThat(files.get("GeoJsonObject.java"))
4364-
.isNormalClass()
4365-
.doesNotExtendsClasses()
4366-
.fileContains("String type")
4367-
.fileDoesNotContain("coordinates")
4368-
.assertTypeAnnotations()
4369-
.containsWithName("JsonSubTypes");
4370-
4371-
JavaFileAssert.assertThat(files.get("Polygon.java"))
4372-
.extendsClass("GeoJsonObject")
4373-
.doesNotImplementInterfaces("GeoJsonObject")
4374-
.fileContains("List<Double> coordinates");
4375-
}
43764358

43774359
@Test
4378-
void issue_15() {
4379-
Map<String, File> files = generateFromContract("src/test/resources/3_0/composed-oneof.yaml", APACHE,
4360+
void issue_912() {
4361+
Map<String, File> files = generateFromContract("src/test/resources/3_0/java/issue_912.yaml", APACHE,
43804362
Map.of(),
43814363
codegen -> codegen.addOpenapiNormalizer("REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING", "true")
43824364
.addInlineSchemaOption("REFACTOR_ALLOF_INLINE_SCHEMAS", "true"));
4383-
4384-
JavaFileAssert.assertThat(files.get("GeoJsonObject.java"))
4365+
JavaFileAssert.assertThat(files.get("CatalogEntity.java"))
43854366
.isNormalClass()
43864367
.doesNotExtendsClasses()
4387-
.fileContains("String type")
4388-
.fileDoesNotContain("coordinates")
4368+
.fileContains("String entityType")
43894369
.assertTypeAnnotations()
4390-
.containsWithName("JsonSubTypes");
4391-
4392-
JavaFileAssert.assertThat(files.get("Polygon.java"))
4393-
.extendsClass("GeoJsonObject")
4394-
.doesNotImplementInterfaces("GeoJsonObject")
4395-
.fileContains("List<Double> coordinates");
4396-
}
4397-
4398-
4399-
@Test
4400-
void issue_912() {
4401-
Map<String, File> files = generateFromContract("src/test/resources/3_0/java/issue_912.yaml", APACHE,
4402-
Map.of(),
4403-
codegen -> codegen.addOpenapiNormalizer("REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING", "true")
4404-
.addInlineSchemaOption("REFACTOR_ALLOF_INLINE_SCHEMAS", "true"));
4405-
4370+
.containsWithNameAndAttributes("JsonTypeInfo", Map.of("include", "JsonTypeInfo.As.PROPERTY", "property", "\"entityType\""))
4371+
.containsWithName("JsonSubTypes")
4372+
.recursivelyContainsWithNameAndAttributes("JsonSubTypes.Type", Map.of("value", "Folder.class", "name", "\"folder\""))
4373+
.recursivelyContainsWithNameAndAttributes("JsonSubTypes.Type", Map.of("value", "Source.class", "name", "\"source\""));
44064374

44074375
}
44084376

44094377
@Test
44104378
void issue_19261() {
4411-
Map<String, File> files = generateFromContract("src/test/resources/3_0/java/issue_19261.yaml", APACHE,
4379+
Map<String, File> files = generateFromContract("src/test/resources/3_0/oneOf_issue_19261.yaml", APACHE,
44124380
Map.of(),
44134381
codegen -> codegen.addOpenapiNormalizer("REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING", "true")
44144382
.addInlineSchemaOption("REFACTOR_ALLOF_INLINE_SCHEMAS", "true"));
4415-
4383+
JavaFileAssert.assertThat(files.get("Product.java"))
4384+
.isNormalClass()
4385+
.doesNotExtendsClasses()
4386+
.fileContains("AboType type")
4387+
.assertTypeAnnotations()
4388+
.containsWithNameAndAttributes("JsonTypeInfo", Map.of("include", "JsonTypeInfo.As.PROPERTY", "property", "\"type\""))
4389+
.containsWithName("JsonSubTypes")
4390+
.recursivelyContainsWithNameAndAttributes("JsonSubTypes.Type", Map.of("value", "HomeProduct.class", "name", "\"home\""))
4391+
.recursivelyContainsWithNameAndAttributes("JsonSubTypes.Type", Map.of("value", "InternetProduct.class", "name", "\"internet\""));
4392+
JavaFileAssert.assertThat(files.get("InternetProduct.java"))
4393+
.extendsClass("Product");
44164394
}
44174395

44184396
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/assertions/AbstractAnnotationsAssert.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ public ACTUAL recursivelyContainsWithName(String name) {
9999
return myself();
100100
}
101101

102+
public ACTUAL recursivelyContainsWithNameAndAttributes(String name, Map<String, String> attributes) {
103+
super
104+
.withFailMessage("Should have annotation with name: " + name)
105+
.anyMatch(annotation -> containsSpecificAnnotationNameAndAttributes(annotation, name, attributes));
106+
107+
return myself();
108+
}
109+
102110
private boolean containsSpecificAnnotationName(Node node, String name) {
103111
if (node == null || name == null)
104112
return false;
@@ -118,4 +126,27 @@ private boolean containsSpecificAnnotationName(Node node, String name) {
118126

119127
return false;
120128
}
129+
130+
private boolean containsSpecificAnnotationNameAndAttributes(Node node, String name, Map<String, String> attributes) {
131+
if (node == null || name == null)
132+
return false;
133+
134+
if (node instanceof AnnotationExpr) {
135+
AnnotationExpr annotation = (AnnotationExpr) node;
136+
137+
if (annotation.getNameAsString().equals(name))
138+
139+
if (hasAttributes(annotation, attributes)) {
140+
return true;
141+
}
142+
143+
}
144+
145+
for(Node child: node.getChildNodes()) {
146+
if (containsSpecificAnnotationNameAndAttributes(child, name, attributes))
147+
return true;
148+
}
149+
150+
return false;
151+
}
121152
}

0 commit comments

Comments
 (0)