Skip to content

Commit 6211c2b

Browse files
committed
Merge branch 'bugfix/one-of-three' of https://github.com/fkellner/openapi-generator into fkellner-bugfix/one-of-three
2 parents 1c2fd67 + b520604 commit 6211c2b

File tree

97 files changed

+1593
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1593
-34
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,17 +1573,18 @@ public static String getParentName(Schema composedSchema, Map<String, Schema> al
15731573
List<String> refedWithoutDiscriminator = new ArrayList<>();
15741574

15751575
if (interfaces != null && !interfaces.isEmpty()) {
1576+
List<String> parentNameCandidates = new ArrayList<>(interfaces.size());
15761577
for (Schema schema : interfaces) {
15771578
// get the actual schema
15781579
if (StringUtils.isNotEmpty(schema.get$ref())) {
15791580
String parentName = getSimpleRef(schema.get$ref());
15801581
Schema s = allSchemas.get(parentName);
15811582
if (s == null) {
15821583
LOGGER.error("Failed to obtain schema from {}", parentName);
1583-
return "UNKNOWN_PARENT_NAME";
1584+
parentNameCandidates.add("UNKNOWN_PARENT_NAME");
15841585
} else if (hasOrInheritsDiscriminator(s, allSchemas, new ArrayList<Schema>())) {
15851586
// discriminator.propertyName is used or x-parent is used
1586-
return parentName;
1587+
parentNameCandidates.add(parentName);
15871588
} else {
15881589
// not a parent since discriminator.propertyName or x-parent is not set
15891590
hasAmbiguousParents = true;
@@ -1600,6 +1601,12 @@ public static String getParentName(Schema composedSchema, Map<String, Schema> al
16001601
}
16011602
}
16021603
}
1604+
if (parentNameCandidates.size() > 1) {
1605+
// unclear which one should be the parent
1606+
return null;
1607+
} else if (parentNameCandidates.size() == 1) {
1608+
return parentNameCandidates.get(0);
1609+
}
16031610
if (refedWithoutDiscriminator.size() == 1 && nullSchemaChildrenCount == 1) {
16041611
// One schema is a $ref and the other is the 'null' type, so the parent is obvious.
16051612
// In this particular case there is no need to specify a discriminator.

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,10 @@ public void testComposedSchemaOneOfWithProperties() {
766766
Set<String> oneOf = new TreeSet<>();
767767
oneOf.add("Apple");
768768
oneOf.add("Banana");
769+
oneOf.add("Orange");
769770
assertEquals(fruit.oneOf, oneOf);
770-
assertEquals(3, fruit.optionalVars.size());
771-
assertEquals(3, fruit.vars.size());
771+
assertEquals(4, fruit.optionalVars.size());
772+
assertEquals(4, fruit.vars.size());
772773
// make sure that fruit has the property color
773774
boolean colorSeen = false;
774775
for (CodegenProperty cp : fruit.vars) {
@@ -788,6 +789,32 @@ public void testComposedSchemaOneOfWithProperties() {
788789
assertTrue(colorSeen);
789790
}
790791

792+
@Test
793+
public void testComposedSchemaOneOfWithInnerModel() {
794+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf_innerModel.yaml");
795+
final DefaultCodegen codegen = new DefaultCodegen();
796+
797+
final Schema schema = openAPI.getComponents().getSchemas().get("RandomAnimalsResponse_animals_inner");
798+
codegen.setOpenAPI(openAPI);
799+
CodegenModel randomAnimalsResponseInner = codegen.fromModel("RandomAnimalsResponse_animals_inner", schema);
800+
801+
Set<String> oneOf = new TreeSet<>();
802+
oneOf.add("Mouse");
803+
oneOf.add("Cat");
804+
oneOf.add("Dog");
805+
assertEquals(oneOf, randomAnimalsResponseInner.oneOf);
806+
assertEquals(4, randomAnimalsResponseInner.vars.size());
807+
// make sure that RandomAnimalsResponseInner has the property species
808+
boolean speciesSeen = false;
809+
for (CodegenProperty cp : randomAnimalsResponseInner.vars) {
810+
if ("species".equals(cp.name)) {
811+
speciesSeen = true;
812+
break;
813+
}
814+
}
815+
assertTrue(speciesSeen);
816+
}
817+
791818
@Test
792819
public void testEscapeText() {
793820
final DefaultCodegen codegen = new DefaultCodegen();

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
import org.openapitools.codegen.java.assertions.JavaFileAssert;
4141
import org.openapitools.codegen.languages.AbstractJavaCodegen;
4242
import org.openapitools.codegen.languages.JavaClientCodegen;
43+
import org.openapitools.codegen.languages.RubyClientCodegen;
4344
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
4445
import org.openapitools.codegen.languages.features.CXFServerFeatures;
4546
import org.openapitools.codegen.meta.features.SecurityFeature;
4647
import org.openapitools.codegen.model.OperationMap;
4748
import org.openapitools.codegen.model.OperationsMap;
4849
import org.openapitools.codegen.testutils.ConfigAssert;
50+
import org.testng.Assert;
4951
import org.testng.annotations.DataProvider;
5052
import org.testng.annotations.Parameters;
5153
import org.testng.annotations.Test;
@@ -67,6 +69,8 @@
6769
import static org.assertj.core.api.Assertions.assertThat;
6870
import static org.assertj.core.api.Assertions.entry;
6971
import static org.assertj.core.api.InstanceOfAssertFactories.FILE;
72+
import static org.junit.jupiter.api.Assertions.assertEquals;
73+
import static org.junit.jupiter.api.Assertions.assertTrue;
7074
import static org.openapitools.codegen.CodegenConstants.*;
7175
import static org.openapitools.codegen.TestUtils.*;
7276
import static org.openapitools.codegen.languages.JavaClientCodegen.*;
@@ -3837,4 +3841,49 @@ public void queryParameterJsonSerialization(String library) {
38373841
public static Object[] springClients() {
38383842
return new Object[]{RESTCLIENT, WEBCLIENT};
38393843
}
3844+
3845+
@Test(description = "test oneOf (OAS3)")
3846+
public void oneOfTest() {
3847+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf.yaml");
3848+
final JavaClientCodegen codegen = new JavaClientCodegen();
3849+
3850+
final Schema schema = openAPI.getComponents().getSchemas().get("fruit");
3851+
codegen.setOpenAPI(openAPI);
3852+
CodegenModel fruit = codegen.fromModel("Fruit", schema);
3853+
3854+
Set<String> oneOf = new TreeSet<String>();
3855+
oneOf.add("Apple");
3856+
oneOf.add("Banana");
3857+
oneOf.add("Orange");
3858+
Assert.assertEquals(fruit.oneOf, oneOf);
3859+
3860+
assertEquals(4, fruit.optionalVars.size());
3861+
assertEquals(4, fruit.vars.size());
3862+
}
3863+
3864+
@Test
3865+
public void oneOfWithInnerModelTest() {
3866+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf_innerModel.yaml");
3867+
final JavaClientCodegen codegen = new JavaClientCodegen();
3868+
3869+
final Schema schema = openAPI.getComponents().getSchemas().get("RandomAnimalsResponse_animals_inner");
3870+
codegen.setOpenAPI(openAPI);
3871+
CodegenModel randomAnimalsResponseInner = codegen.fromModel("RandomAnimalsResponse_animals_inner", schema);
3872+
3873+
Set<String> oneOf = new TreeSet<>();
3874+
oneOf.add("Mouse");
3875+
oneOf.add("Cat");
3876+
oneOf.add("Dog");
3877+
assertEquals(oneOf, randomAnimalsResponseInner.oneOf);
3878+
assertEquals(4, randomAnimalsResponseInner.vars.size());
3879+
// make sure that RandomAnimalsResponseInner has the property species
3880+
boolean speciesSeen = false;
3881+
for (CodegenProperty cp : randomAnimalsResponseInner.vars) {
3882+
if ("species".equals(cp.name)) {
3883+
speciesSeen = true;
3884+
break;
3885+
}
3886+
}
3887+
assertTrue(speciesSeen);
3888+
}
38403889
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ public void oneOfTest() {
368368
Set<String> oneOf = new TreeSet<String>();
369369
oneOf.add("Apple");
370370
oneOf.add("Banana");
371+
oneOf.add("Orange");
371372
Assert.assertEquals(fruit.oneOf, oneOf);
372373
}
373374

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,4 +675,12 @@ public void testModelWithPropertiesOnly() {
675675
testSchema.setAdditionalProperties(new Schema().type("string"));
676676
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
677677
}
678+
679+
@Test
680+
public void getParentNameMultipleInterfacesTest() {
681+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf_innerModel.yaml");
682+
Map<String, Schema> allSchemas = openAPI.getComponents().getSchemas();
683+
Schema composedSchema = allSchemas.get("RandomAnimalsResponse_animals_inner");
684+
assertNull(ModelUtils.getParentName(composedSchema, allSchemas));
685+
}
678686
}

modules/openapi-generator/src/test/resources/3_0/oneOf.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ components:
2222
oneOf:
2323
- $ref: '#/components/schemas/apple'
2424
- $ref: '#/components/schemas/banana'
25+
- $ref: '#/components/schemas/orange'
2526
# additionalProperties:
2627
# type: string
2728
# uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
@@ -37,3 +38,9 @@ components:
3738
properties:
3839
count:
3940
type: number
41+
orange:
42+
title: orange
43+
type: object
44+
properties:
45+
sweet:
46+
type: boolean
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
openapi: 3.0.3
3+
info:
4+
title: randimals
5+
version: "1.0"
6+
paths:
7+
/random-animals:
8+
get:
9+
tags:
10+
- Random Animals Resource
11+
responses:
12+
"200":
13+
description: OK
14+
content:
15+
'*/*':
16+
schema:
17+
$ref: '#/components/schemas/RandomAnimalsResponse'
18+
components:
19+
schemas:
20+
RandomAnimalsResponse:
21+
type: object
22+
properties:
23+
animals:
24+
type: array
25+
items:
26+
oneOf:
27+
- $ref: '#/components/schemas/Dog'
28+
- $ref: '#/components/schemas/Cat'
29+
- $ref: '#/components/schemas/Mouse'
30+
required:
31+
- animals
32+
Dog:
33+
allOf:
34+
- $ref: '#/components/schemas/Animal'
35+
- type: object
36+
properties:
37+
dogId:
38+
type: string
39+
required:
40+
- dogId
41+
Animal:
42+
discriminator:
43+
propertyName: species
44+
mapping:
45+
Dog: '#/components/schemas/Dog'
46+
Cat: '#/components/schemas/Cat'
47+
Mouse: '#/components/schemas/Mouse'
48+
properties:
49+
species:
50+
type: string
51+
required:
52+
- species
53+
Cat:
54+
allOf:
55+
- $ref: '#/components/schemas/Animal'
56+
- type: object
57+
properties:
58+
catId:
59+
type: string
60+
required:
61+
- catId
62+
Mouse:
63+
allOf:
64+
- $ref: '#/components/schemas/Animal'
65+
- type: object
66+
properties:
67+
mouseId:
68+
type: string
69+
required:
70+
- mouseId

modules/openapi-generator/src/test/resources/3_0/protobuf-schema/fruitOneOf.proto

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ package openapitools;
1414

1515
import public "models/apple.proto";
1616
import public "models/banana.proto";
17+
import public "models/orange.proto";
1718

1819
message Fruit {
1920

2021
oneof fruit {
2122
Apple apple = 93029210;
22-
2323
Banana banana = 322613405;
24-
24+
Orange orange = 471980499;
2525
}
26-
2726
}

samples/client/petstore/csharp/generichost/net4.7/OneOf/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ docs/apis/DefaultApi.md
77
docs/models/Apple.md
88
docs/models/Banana.md
99
docs/models/Fruit.md
10+
docs/models/Orange.md
1011
docs/scripts/git_push.ps1
1112
docs/scripts/git_push.sh
1213
src/Org.OpenAPITools.Test/Api/DependencyInjectionTests.cs
@@ -36,5 +37,6 @@ src/Org.OpenAPITools/Extensions/IServiceCollectionExtensions.cs
3637
src/Org.OpenAPITools/Model/Apple.cs
3738
src/Org.OpenAPITools/Model/Banana.cs
3839
src/Org.OpenAPITools/Model/Fruit.cs
40+
src/Org.OpenAPITools/Model/Orange.cs
3941
src/Org.OpenAPITools/Org.OpenAPITools.csproj
4042
src/Org.OpenAPITools/README.md

samples/client/petstore/csharp/generichost/net4.7/OneOf/api/openapi.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ components:
2222
oneOf:
2323
- $ref: "#/components/schemas/apple"
2424
- $ref: "#/components/schemas/banana"
25+
- $ref: "#/components/schemas/orange"
2526
properties:
2627
color:
2728
type: string
@@ -38,4 +39,10 @@ components:
3839
type: number
3940
title: banana
4041
type: object
42+
orange:
43+
properties:
44+
sweet:
45+
type: boolean
46+
title: orange
47+
type: object
4148

0 commit comments

Comments
 (0)