Skip to content

Commit 6a08ec5

Browse files
authored
[core] Add type and format properties to model of inline response (#6153)
1 parent 1be98b4 commit 6a08ec5

File tree

37 files changed

+204
-3
lines changed

37 files changed

+204
-3
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,15 @@ private Schema modelFromProperty(OpenAPI openAPI, Schema object, String path) {
621621
XML xml = object.getXml();
622622
Map<String, Schema> properties = object.getProperties();
623623
Schema model = new Schema();
624+
if (object.getType() != null) {
625+
model.setType(object.getType());
626+
}
627+
if (object.getFormat() != null) {
628+
// Even though the `format` keyword typically applies to primitive types only,
629+
// the JSON schema specification states `format` can be used for any model type instance
630+
// including object types.
631+
model.setFormat(object.getFormat());
632+
}
624633
model.setDescription(description);
625634
model.setExample(example);
626635
model.setName(object.getName());

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ public void testInlineResponseModel() {
263263
assertTrue(model.getProperties().get("name") instanceof StringSchema);
264264
}
265265

266+
@Test
267+
public void testInlineResponseModelType() {
268+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/6150_model_json_inline.yaml");
269+
new InlineModelResolver().flatten(openAPI);
270+
271+
Schema InlineResponse200 = openAPI.getComponents().getSchemas().get("inline_response_200");
272+
assertEquals("object", InlineResponse200.getType());
273+
assertEquals("unknown", InlineResponse200.getFormat());
274+
Schema FooBarObject = openAPI.getComponents().getSchemas().get("FooBarObject");
275+
assertEquals("object", FooBarObject.getType());
276+
assertEquals("date-time", FooBarObject.getFormat());
277+
Schema Animal = openAPI.getComponents().getSchemas().get("Animal");
278+
assertEquals("object", Animal.getType());
279+
Schema Dog = openAPI.getComponents().getSchemas().get("Dog");
280+
assertNull(Dog.getType());
281+
}
282+
266283
@Test
267284
public void testInlineResponseModelWithTitle() {
268285
OpenAPI openapi = new OpenAPI();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Test inline response model
5+
description: Test inline response model.
6+
license:
7+
name: MIT
8+
paths:
9+
/foobar:
10+
get:
11+
operationId: testOperation
12+
description: No type property in modelJson of InlineResponse200
13+
responses:
14+
200:
15+
description: InlineResponse200 itself.
16+
content:
17+
application/json:
18+
schema:
19+
type: object
20+
# It is legal to use the `format` keyword for object types. The JSON schema specification explicitly allows this.
21+
# Even if in practice most OAS authors use `format` for primitive types, it should still be allowed to use format for object types.
22+
format: unknown
23+
properties:
24+
foo:
25+
type: string
26+
bar:
27+
type: string
28+
post:
29+
operationId: testOperationPost
30+
description: No type property in modelJson of InlineResponse200
31+
responses:
32+
400:
33+
description: InlineResponse200 itself.
34+
content:
35+
application/json:
36+
schema:
37+
title: FooBarObject
38+
type: object
39+
# It is legal to use the `format` keyword for object types. The JSON schema specification explicitly allows this.
40+
# Even if in practice most OAS authors use `format` for primitive types, it should still be allowed to use format for object types.
41+
format: date-time
42+
properties:
43+
foo:
44+
type: string
45+
components:
46+
schemas:
47+
Animal:
48+
type: object
49+
discriminator: className
50+
required:
51+
- className
52+
properties:
53+
className:
54+
type: string
55+
color:
56+
type: string
57+
default: 'red'
58+
Dog:
59+
allOf:
60+
- $ref: '#/components/schemas/Animal'
61+
- type: object
62+
properties:
63+
breed:
64+
type: string
65+
Cat:
66+
allOf:
67+
- $ref: '#/components/schemas/Animal'
68+
- type: object
69+
properties:
70+
declawed:
71+
type: boolean
72+
HugeCat:
73+
allOf:
74+
- $ref: '#/components/schemas/Cat'
75+
- type: object
76+
properties:
77+
kind:
78+
type: string
79+
enum: [lions, tigers, leopards, jaguars]

samples/client/petstore/go-experimental/go-petstore/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,10 +2090,12 @@ components:
20902090
properties:
20912091
breed:
20922092
type: string
2093+
type: object
20932094
Cat_allOf:
20942095
properties:
20952096
declawed:
20962097
type: boolean
2098+
type: object
20972099
BigCat_allOf:
20982100
properties:
20992101
kind:
@@ -2103,6 +2105,7 @@ components:
21032105
- leopards
21042106
- jaguars
21052107
type: string
2108+
type: object
21062109
securitySchemes:
21072110
petstore_auth:
21082111
flows:

samples/client/petstore/go/go-petstore-withXml/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,10 +2090,12 @@ components:
20902090
properties:
20912091
breed:
20922092
type: string
2093+
type: object
20932094
Cat_allOf:
20942095
properties:
20952096
declawed:
20962097
type: boolean
2098+
type: object
20972099
BigCat_allOf:
20982100
properties:
20992101
kind:
@@ -2103,6 +2105,7 @@ components:
21032105
- leopards
21042106
- jaguars
21052107
type: string
2108+
type: object
21062109
securitySchemes:
21072110
petstore_auth:
21082111
flows:

samples/client/petstore/go/go-petstore/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,10 +2090,12 @@ components:
20902090
properties:
20912091
breed:
20922092
type: string
2093+
type: object
20932094
Cat_allOf:
20942095
properties:
20952096
declawed:
20962097
type: boolean
2098+
type: object
20972099
BigCat_allOf:
20982100
properties:
20992101
kind:
@@ -2103,6 +2105,7 @@ components:
21032105
- leopards
21042106
- jaguars
21052107
type: string
2108+
type: object
21062109
securitySchemes:
21072110
petstore_auth:
21082111
flows:

samples/client/petstore/haskell-http-client/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,10 +2090,12 @@ components:
20902090
properties:
20912091
breed:
20922092
type: string
2093+
type: object
20932094
Cat_allOf:
20942095
properties:
20952096
declawed:
20962097
type: boolean
2098+
type: object
20972099
BigCat_allOf:
20982100
properties:
20992101
kind:
@@ -2103,6 +2105,7 @@ components:
21032105
- leopards
21042106
- jaguars
21052107
type: string
2108+
type: object
21062109
securitySchemes:
21072110
petstore_auth:
21082111
flows:

samples/client/petstore/java/feign/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/google-api-client/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/jersey1/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/jersey2-java8/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/native-async/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/native/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/okhttp-gson-parcelableModel/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/okhttp-gson/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

samples/client/petstore/java/rest-assured-jackson/api/openapi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,12 @@ components:
21512151
properties:
21522152
breed:
21532153
type: string
2154+
type: object
21542155
Cat_allOf:
21552156
properties:
21562157
declawed:
21572158
type: boolean
2159+
type: object
21582160
BigCat_allOf:
21592161
properties:
21602162
kind:
@@ -2164,6 +2166,7 @@ components:
21642166
- leopards
21652167
- jaguars
21662168
type: string
2169+
type: object
21672170
securitySchemes:
21682171
petstore_auth:
21692172
flows:

0 commit comments

Comments
 (0)