Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Add type and format properties to model of inline response #6153

Merged
merged 10 commits into from
Aug 3, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ private Schema modelFromProperty(OpenAPI openAPI, Schema object, String path) {
XML xml = object.getXml();
Map<String, Schema> properties = object.getProperties();
Schema model = new Schema();
if (object.getType() != null) {
ybelenko marked this conversation as resolved.
Show resolved Hide resolved
model.setType(object.getType());
}
if (object.getFormat() != null) {
// Even though the `format` keyword typically applies to primitive types only,
// the JSON schema specification states `format` can be used for any model type instance
// including object types.
model.setFormat(object.getFormat());
ybelenko marked this conversation as resolved.
Show resolved Hide resolved
}
model.setDescription(description);
model.setExample(example);
model.setName(object.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,23 @@ public void testInlineResponseModel() {
assertTrue(model.getProperties().get("name") instanceof StringSchema);
}

@Test
public void testInlineResponseModelType() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/6150_model_json_inline.yaml");
new InlineModelResolver().flatten(openAPI);

Schema InlineResponse200 = openAPI.getComponents().getSchemas().get("inline_response_200");
assertEquals("object", InlineResponse200.getType());
assertEquals("unknown", InlineResponse200.getFormat());
Schema FooBarObject = openAPI.getComponents().getSchemas().get("FooBarObject");
assertEquals("object", FooBarObject.getType());
assertEquals("date-time", FooBarObject.getFormat());
Schema Animal = openAPI.getComponents().getSchemas().get("Animal");
assertEquals("object", Animal.getType());
Schema Dog = openAPI.getComponents().getSchemas().get("Dog");
assertNull(Dog.getType());
}

@Test
public void testInlineResponseModelWithTitle() {
OpenAPI openapi = new OpenAPI();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Test inline response model
description: Test inline response model.
license:
name: MIT
paths:
/foobar:
get:
operationId: testOperation
description: No type property in modelJson of InlineResponse200
responses:
200:
description: InlineResponse200 itself.
content:
application/json:
schema:
type: object
# It is legal to use the `format` keyword for object types. The JSON schema specification explicitly allows this.
# Even if in practice most OAS authors use `format` for primitive types, it should still be allowed to use format for object types.
format: unknown
ybelenko marked this conversation as resolved.
Show resolved Hide resolved
properties:
foo:
type: string
bar:
type: string
post:
operationId: testOperationPost
description: No type property in modelJson of InlineResponse200
responses:
400:
description: InlineResponse200 itself.
content:
application/json:
schema:
title: FooBarObject
type: object
# It is legal to use the `format` keyword for object types. The JSON schema specification explicitly allows this.
# Even if in practice most OAS authors use `format` for primitive types, it should still be allowed to use format for object types.
format: date-time
ybelenko marked this conversation as resolved.
Show resolved Hide resolved
properties:
foo:
type: string
components:
schemas:
Animal:
type: object
discriminator: className
required:
- className
properties:
className:
type: string
color:
type: string
default: 'red'
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
properties:
breed:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
properties:
declawed:
type: boolean
HugeCat:
allOf:
- $ref: '#/components/schemas/Cat'
- type: object
properties:
kind:
type: string
enum: [lions, tigers, leopards, jaguars]
Original file line number Diff line number Diff line change
Expand Up @@ -2090,10 +2090,12 @@ components:
properties:
breed:
type: string
type: object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I agree type: object was missing. It it there in the original document (modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml), it makes sense to add it.

Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2103,6 +2105,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2090,10 +2090,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2103,6 +2105,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/go/go-petstore/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2090,10 +2090,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2103,6 +2105,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/haskell-http-client/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2090,10 +2090,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2103,6 +2105,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/feign/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/jersey1/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/jersey2-java8/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/native-async/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/native/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/java/okhttp-gson/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,12 @@ components:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
BigCat_allOf:
properties:
kind:
Expand All @@ -2164,6 +2166,7 @@ components:
- leopards
- jaguars
type: string
type: object
securitySchemes:
petstore_auth:
flows:
Expand Down
Loading