Skip to content

Commit eb85d80

Browse files
committed
Add regression test for attribute copy of flattened composed schemas
1 parent f7f5788 commit eb85d80

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,109 @@ public void objectComposedWithInline() {
768768
checkComposedChildren(openAPI, schema.getOneOf(), "oneOf");
769769
}
770770

771+
772+
@Test
773+
public void inheritanceWithInlineDiscriminator() {
774+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/2_0/regression_6905.yaml");
775+
new InlineModelResolver().flatten(openAPI);
776+
777+
assertTrue(openAPI.getComponents().getSchemas().get("PartyType") instanceof StringSchema);
778+
assertTrue(openAPI.getComponents().getSchemas().get("CustomerType") instanceof StringSchema);
779+
assertTrue(openAPI.getComponents().getSchemas().get("Entity") instanceof ObjectSchema);
780+
781+
assertTrue(openAPI.getComponents().getSchemas().get("Party") instanceof ComposedSchema);
782+
assertTrue(openAPI.getComponents().getSchemas().get("Contact") instanceof ComposedSchema);
783+
assertTrue(openAPI.getComponents().getSchemas().get("Customer") instanceof ComposedSchema);
784+
assertTrue(openAPI.getComponents().getSchemas().get("Person") instanceof ComposedSchema);
785+
assertTrue(openAPI.getComponents().getSchemas().get("Organization") instanceof ComposedSchema);
786+
787+
assertTrue(openAPI.getComponents().getSchemas().get("ApiError") instanceof ObjectSchema);
788+
789+
assertFalse(openAPI.getComponents().getSchemas().get("Party_allOf") instanceof ComposedSchema);
790+
assertFalse(openAPI.getComponents().getSchemas().get("Contact_allOf") instanceof ComposedSchema);
791+
assertFalse(openAPI.getComponents().getSchemas().get("Customer_allOf") instanceof ComposedSchema);
792+
assertFalse(openAPI.getComponents().getSchemas().get("Person_allOf") instanceof ComposedSchema);
793+
assertFalse(openAPI.getComponents().getSchemas().get("Organization_allOf") instanceof ComposedSchema);
794+
795+
// Party
796+
ComposedSchema party = (ComposedSchema) openAPI.getComponents().getSchemas().get("Party");
797+
List<Schema> partySchemas = party.getAllOf();
798+
Schema entity = ModelUtils.getReferencedSchema(openAPI, partySchemas.get(0));
799+
Schema partyAllOf = ModelUtils.getReferencedSchema(openAPI, partySchemas.get(1));
800+
801+
assertEquals(partySchemas.get(0).get$ref(), "#/components/schemas/Entity");
802+
assertEquals(partySchemas.get(1).get$ref(), "#/components/schemas/Party_allOf");
803+
804+
assertNull(party.getDiscriminator());
805+
assertNull(entity.getDiscriminator());
806+
assertNotNull(partyAllOf.getDiscriminator());
807+
assertEquals(partyAllOf.getDiscriminator().getPropertyName(), "party_type");
808+
assertEquals(partyAllOf.getRequired().get(0), "party_type");
809+
810+
811+
// Contact
812+
ComposedSchema contact = (ComposedSchema) openAPI.getComponents().getSchemas().get("Contact");
813+
Schema contactAllOf = ModelUtils.getReferencedSchema(openAPI, contact.getAllOf().get(1));
814+
815+
assertEquals(contact.getExtensions().get("x-discriminator-value"), "contact");
816+
assertEquals(contact.getAllOf().get(0).get$ref(), "#/components/schemas/Party");
817+
assertEquals(contact.getAllOf().get(1).get$ref(), "#/components/schemas/Contact_allOf");
818+
assertNull(contactAllOf.getDiscriminator());
819+
820+
821+
// Customer
822+
ComposedSchema customer = (ComposedSchema) openAPI.getComponents().getSchemas().get("Customer");
823+
List<Schema> customerSchemas = customer.getAllOf();
824+
Schema customerAllOf = ModelUtils.getReferencedSchema(openAPI, customerSchemas.get(1));
825+
826+
assertEquals(customerSchemas.get(0).get$ref(), "#/components/schemas/Party");
827+
assertNull(customer.getDiscriminator());
828+
assertEquals(customer.getExtensions().get("x-discriminator-value"), "customer");
829+
830+
// Discriminators are not defined at this level in the schema doc
831+
assertNull(customerSchemas.get(0).getDiscriminator());
832+
assertEquals(customerSchemas.get(1).get$ref(), "#/components/schemas/Customer_allOf");
833+
assertNull(customerSchemas.get(1).getDiscriminator());
834+
835+
// Customer -> Party where Customer defines it's own discriminator
836+
assertNotNull(customerAllOf.getDiscriminator());
837+
assertEquals(customerAllOf.getDiscriminator().getPropertyName(), "customer_type");
838+
assertEquals(customerAllOf.getRequired().get(0), "customer_type");
839+
840+
841+
// Person
842+
ComposedSchema person = (ComposedSchema) openAPI.getComponents().getSchemas().get("Person");
843+
List<Schema> personSchemas = person.getAllOf();
844+
Schema personAllOf = ModelUtils.getReferencedSchema(openAPI, personSchemas.get(1));
845+
846+
// Discriminators are not defined at this level in the schema doc
847+
assertEquals(personSchemas.get(0).get$ref(), "#/components/schemas/Customer");
848+
assertNull(personSchemas.get(0).getDiscriminator());
849+
assertEquals(personSchemas.get(1).get$ref(), "#/components/schemas/Person_allOf");
850+
assertNull(personSchemas.get(1).getDiscriminator());
851+
852+
// Person -> Customer -> Party, so discriminator is not at this level
853+
assertNull(person.getDiscriminator());
854+
assertEquals(person.getExtensions().get("x-discriminator-value"), "person");
855+
assertNull(personAllOf.getDiscriminator());
856+
857+
858+
// Organization
859+
ComposedSchema organization = (ComposedSchema) openAPI.getComponents().getSchemas().get("Organization");
860+
List<Schema> organizationSchemas = organization.getAllOf();
861+
Schema organizationAllOf = ModelUtils.getReferencedSchema(openAPI, organizationSchemas.get(1));
862+
863+
// Discriminators are not defined at this level in the schema doc
864+
assertEquals(organizationSchemas.get(0).get$ref(), "#/components/schemas/Customer");
865+
assertNull(organizationSchemas.get(0).getDiscriminator());
866+
assertEquals(organizationSchemas.get(1).get$ref(), "#/components/schemas/Organization_allOf");
867+
assertNull(organizationSchemas.get(1).getDiscriminator());
868+
869+
// Organization -> Customer -> Party, so discriminator is not at this level
870+
assertNull(organizationAllOf.getDiscriminator());
871+
assertEquals(organization.getExtensions().get("x-discriminator-value"), "organization");
872+
}
873+
771874
@Test
772875
public void arbitraryObjectModelWithArrayInlineWithTitle() {
773876
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
@@ -871,4 +974,9 @@ public void callbacks() {
871974
assertTrue(properties.get("action") instanceof StringSchema);
872975
assertTrue(properties.get("data") instanceof StringSchema);
873976
}
977+
978+
@Test
979+
public void regresssion_6905() {
980+
981+
}
874982
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
swagger: '2.0'
2+
3+
info:
4+
title: Test Command model generation
5+
description: Test Command model generation
6+
version: 1.0.0
7+
definitions:
8+
PartyType:
9+
description: type
10+
type: string
11+
enum:
12+
- customer
13+
- contact
14+
15+
CustomerType:
16+
description: type
17+
type: string
18+
enum:
19+
- person
20+
- organization
21+
22+
Entity:
23+
type: object
24+
properties:
25+
id:
26+
type: string
27+
readOnly: true
28+
29+
Party:
30+
allOf:
31+
- $ref: '#/definitions/Entity'
32+
- type: object
33+
discriminator: party_type
34+
required:
35+
- party_type
36+
properties:
37+
party_type:
38+
readOnly: true
39+
$ref: '#/definitions/PartyType'
40+
tax_id_number:
41+
type: string
42+
43+
Contact:
44+
x-discriminator-value: contact
45+
allOf:
46+
- $ref: '#/definitions/Party'
47+
- type: object
48+
properties:
49+
first_name:
50+
type: string
51+
last_name:
52+
type: string
53+
suffix:
54+
type: string
55+
dob:
56+
type: string
57+
format: date
58+
59+
Customer:
60+
x-discriminator-value: customer
61+
allOf:
62+
- $ref: '#/definitions/Party'
63+
- type: object
64+
discriminator: customer_type
65+
required:
66+
- customer_type
67+
properties:
68+
customer_type:
69+
readOnly: true
70+
$ref: '#/definitions/CustomerType'
71+
customer_num:
72+
type: string
73+
external_customer_num:
74+
type: string
75+
Person:
76+
x-discriminator-value: person
77+
allOf:
78+
- $ref: '#/definitions/Customer'
79+
- type: object
80+
properties:
81+
first_name:
82+
type: string
83+
last_name:
84+
type: string
85+
86+
Organization:
87+
x-discriminator-value: organization
88+
allOf:
89+
- $ref: '#/definitions/Customer'
90+
- type: object
91+
required:
92+
- organization_name
93+
properties:
94+
organization_name:
95+
type: string
96+
97+
ApiError:
98+
type: object
99+
required:
100+
- code
101+
- message
102+
properties:
103+
code:
104+
type: string
105+
readOnly: true
106+
message:
107+
type: string
108+
readOnly: true
109+
110+
paths:
111+
/customers:
112+
get:
113+
consumes: []
114+
operationId: queryCustomers
115+
tags:
116+
- Customer
117+
summary: Get customers
118+
responses:
119+
200:
120+
description: Success
121+
schema:
122+
type: array
123+
items:
124+
$ref: '#/definitions/Customer'
125+
400:
126+
description: Bad request.
127+
schema:
128+
$ref: '#/definitions/ApiError'
129+
default:
130+
description: Unknown error.
131+
schema:
132+
$ref: '#/definitions/ApiError'
133+
/contacts:
134+
get:
135+
consumes: []
136+
operationId: queryContacts
137+
tags:
138+
- Contact
139+
summary: Get contact
140+
responses:
141+
200:
142+
description: Success
143+
schema:
144+
type: array
145+
items:
146+
$ref: '#/definitions/Contact'
147+
400:
148+
description: Bad request.
149+
schema:
150+
$ref: '#/definitions/ApiError'
151+
default:
152+
description: Unknown error.
153+
schema:
154+
$ref: '#/definitions/ApiError'
155+
/parties:
156+
get:
157+
consumes: []
158+
responses:
159+
200:
160+
description: Success
161+
schema:
162+
type: array
163+
items:
164+
$ref: '#/definitions/Party'
165+
400:
166+
description: Bad request.
167+
schema:
168+
$ref: '#/definitions/ApiError'
169+
default:
170+
description: Unknown error.
171+
schema:
172+
$ref: '#/definitions/ApiError'

0 commit comments

Comments
 (0)