Skip to content

Commit 1561c33

Browse files
authored
[JAVA] Generate client files with multiple enum oneOf types (OpenAPITools#12687)
* handle multiple enum types for oneof * add updates from generate-samples.sh * add test coverage for oneof enum generation * update doc and var names for clarity
1 parent ff414dd commit 1561c33

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,9 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
27362736
addProperties(allProperties, allRequired, refSchema, new HashSet<>());
27372737
} else {
27382738
// composition
2739-
addProperties(properties, required, refSchema, new HashSet<>());
2739+
Map<String, Schema> newProperties = new LinkedHashMap<>();
2740+
addProperties(newProperties, required, refSchema, new HashSet<>());
2741+
mergeProperties(properties, newProperties);
27402742
addProperties(allProperties, allRequired, refSchema, new HashSet<>());
27412743
}
27422744
}
@@ -2812,6 +2814,28 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
28122814
// end of code block for composed schema
28132815
}
28142816

2817+
/**
2818+
* Combines all previously-detected type entries for a schema with newly-discovered ones, to ensure
2819+
* that schema for items like enum include all possible values.
2820+
*/
2821+
private void mergeProperties(Map<String, Schema> existingProperties, Map<String, Schema> newProperties) {
2822+
// https://github.com/OpenAPITools/openapi-generator/issues/12545
2823+
if (null != existingProperties && null != newProperties) {
2824+
Schema existingType = existingProperties.get("type");
2825+
Schema newType = newProperties.get("type");
2826+
existingProperties.putAll(newProperties);
2827+
if (null != existingType && null != newType && !newType.getEnum().isEmpty()) {
2828+
for (Object e : newType.getEnum()) {
2829+
// ensure all interface enum types are added to schema
2830+
if (null != existingType.getEnum() && !existingType.getEnum().contains(e)) {
2831+
existingType.addEnumItemObject(e);
2832+
}
2833+
}
2834+
existingProperties.put("type", existingType);
2835+
}
2836+
}
2837+
}
2838+
28152839
protected void updateModelForObject(CodegenModel m, Schema schema) {
28162840
if (schema.getProperties() != null || schema.getRequired() != null && !(schema instanceof ComposedSchema)) {
28172841
// passing null to allProperties and allRequired as there's no parent

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,36 @@ public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
640640
Assert.assertEquals(type, "oneOf<ObjA,ObjB>");
641641
}
642642

643+
@Test
644+
public void testOneOfEnum() {
645+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue12545.json");
646+
final DefaultCodegen codegen = new DefaultCodegen();
647+
String modelName = "petItems";
648+
649+
final Schema schema = openAPI.getComponents().getSchemas().get(modelName);
650+
codegen.setOpenAPI(openAPI);
651+
CodegenModel petItems = codegen.fromModel(modelName, schema);
652+
653+
Set<String> oneOf = new TreeSet<String>();
654+
oneOf.add("Dog");
655+
oneOf.add("Cat");
656+
Assert.assertEquals(petItems.oneOf, oneOf);
657+
// make sure that animal has the property type
658+
boolean typeSeen = false;
659+
boolean typeContainsEnums = false;
660+
for (CodegenProperty cp : petItems.vars) {
661+
if ("type".equals(cp.name)) {
662+
typeSeen = true;
663+
if (null != cp.get_enum() && cp.get_enum().contains("dog") && cp.get_enum().contains("cat")) {
664+
typeContainsEnums = true;
665+
}
666+
break;
667+
}
668+
}
669+
Assert.assertTrue(typeSeen);
670+
Assert.assertTrue(typeContainsEnums);
671+
}
672+
643673
@Test
644674
public void testComposedSchemaOneOfWithProperties() {
645675
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf.yaml");
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "OpenAPI Petstore"
6+
},
7+
"paths": {
8+
"/": {
9+
"get": {
10+
"description": "get all pets in a household",
11+
"responses": {
12+
"200": {
13+
"description": "successful operation",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/Household"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
},
26+
"components": {
27+
"schemas": {
28+
"Household": {
29+
"title": "Household",
30+
"type" : "object",
31+
"required": [
32+
"pets"
33+
],
34+
"properties": {
35+
"pets": {
36+
"type": "array",
37+
"title": "pets",
38+
"items" : {
39+
"title": "petItems",
40+
"oneOf" : [ {
41+
"$ref" : "#/components/schemas/Dog"
42+
}, {
43+
"$ref" : "#/components/schemas/Cat"
44+
} ]
45+
}
46+
}
47+
}
48+
},
49+
"Dog": {
50+
"type" : "object",
51+
"title": "Dog",
52+
"properties" : {
53+
"type" : {
54+
"type" : "string",
55+
"enum" : [ "dog" ]
56+
}
57+
},
58+
"required" : [ "type" ]
59+
},
60+
"Cat": {
61+
"type" : "object",
62+
"title": "Cat",
63+
"properties" : {
64+
"type" : {
65+
"type" : "string",
66+
"enum" : [ "cat" ]
67+
}
68+
},
69+
"required" : [ "type" ]
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)