Skip to content

Commit

Permalink
Merge pull request swagger-api#2345 from swagger-api/bugfix/schema-si…
Browse files Browse the repository at this point in the history
…mple-objects

Schema of String and Wrapper Objects, OpenApi version to 3.0.0
  • Loading branch information
frantuma authored Aug 2, 2017
2 parents cb7cc2e + 71b0ab4 commit 44997dc
Show file tree
Hide file tree
Showing 7 changed files with 567 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"openapi": "3.0.0-rc2",
"openapi": "3.0.0",
"info": {
"title": "Swagger Petstore",
"contact": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ public static Optional<Schema> getSchemaFromAnnotation(io.swagger.oas.annotation
schemaObject.setExclusiveMinimum(schema.exclusiveMinimum());
isEmpty = false;
}
if (schema.maxLength() > 0)
if (schema.maxLength() > 0) {
if (schema.maxProperties() > 0) {
schemaObject.setMaxProperties(schema.maxProperties());
isEmpty = false;
}
}
if (schema.minProperties() > 0) {
schemaObject.setMinProperties(schema.minProperties());
isEmpty = false;
Expand Down Expand Up @@ -362,13 +363,18 @@ public static Optional<Content> getContent(io.swagger.oas.annotations.media.Cont
Class<?> schemaImplementation = annotationContent.schema().implementation();
Map<String, Schema> schemaMap;
if (schemaImplementation != Void.class) {
schemaMap = ModelConverters.getInstance().readAll(schemaImplementation);
schemaMap.forEach((key, schema) -> {
components.addSchemas(key, schema);
});
Schema schemaObject = new Schema();
schemaObject.set$ref(COMPONENTS_REF + schemaImplementation.getSimpleName());
if (schemaImplementation.getName().startsWith("java.lang")) {
schemaObject.setType(schemaImplementation.getSimpleName().toLowerCase());
} else {
schemaMap = ModelConverters.getInstance().readAll(schemaImplementation);
schemaMap.forEach((key, schema) -> {
components.addSchemas(key, schema);
});
schemaObject.set$ref(COMPONENTS_REF + schemaImplementation.getSimpleName());
}
mediaType.setSchema(schemaObject);

} else {
getSchemaFromAnnotation(annotationContent.schema()).ifPresent(mediaType::setSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.jaxrs2.annotations.AbstractAnnotationTest;
import io.swagger.jaxrs2.resources.PetResource;
import io.swagger.jaxrs2.resources.UserResource;
import io.swagger.oas.annotations.Operation;
import io.swagger.oas.annotations.media.Content;
import io.swagger.oas.annotations.media.ExampleObject;
Expand Down Expand Up @@ -208,13 +209,13 @@ public void simpleGet() {
}
}

@Test(description = "reads an operation from sample")
public void testCompleteOperation() {
@Test(description = "reads the pet resource from sample")
public void testCompletePetResource() {
String openApiYAML = readIntoYaml(PetResource.class);
int start = 0;
int end = openApiYAML.length() - 1;
String extractedYAML = openApiYAML.substring(start, end);
String expectedYAML = "openapi: 3.0.0-rc2\n" +
String expectedYAML = "openapi: 3.0.0\n" +
"paths:\n" +
" /pet/{petId}:\n" +
" get:\n" +
Expand All @@ -226,7 +227,6 @@ public void testCompleteOperation() {
" in: path\n" +
" description: ID of pet that needs to be fetched\n" +
" required: true\n" +
" explode: false\n" +
" schema:\n" +
" type: integer\n" +
" format: int64\n" +
Expand Down Expand Up @@ -288,7 +288,6 @@ public void testCompleteOperation() {
" in: query\n" +
" description: Status values that need to be considered for filter\n" +
" required: true\n" +
" explode: false\n" +
" schema:\n" +
" type: string\n" +
" - name: skip\n" +
Expand Down Expand Up @@ -319,7 +318,6 @@ public void testCompleteOperation() {
" in: query\n" +
" description: Tags to filter by\n" +
" required: true\n" +
" explode: false\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
Expand All @@ -341,6 +339,8 @@ public void testCompleteOperation() {
" format: int64\n" +
" name:\n" +
" type: string\n" +
" xml:\n" +
" name: Category\n" +
" Tag:\n" +
" type: object\n" +
" properties:\n" +
Expand All @@ -349,6 +349,8 @@ public void testCompleteOperation() {
" format: int64\n" +
" name:\n" +
" type: string\n" +
" xml:\n" +
" name: Tag\n" +
" Pet:\n" +
" type: object\n" +
" properties:\n" +
Expand All @@ -361,17 +363,210 @@ public void testCompleteOperation() {
" type: string\n" +
" photoUrls:\n" +
" type: array\n" +
" xml:\n" +
" wrapped: true\n" +
" items:\n" +
" type: string\n" +
" xml:\n" +
" name: photoUrl\n" +
" tags:\n" +
" type: array\n" +
" xml:\n" +
" wrapped: true\n" +
" items:\n" +
" $ref: '#/components/schemas/Tag'\n" +
" status:\n" +
" type: string\n" +
" description: pet status in the store\n" +
" enum:\n" +
" - available,pending,sold";
" - available,pending,sold\n" +
" xml:\n" +
" name: Pet";
assertEquals(extractedYAML, expectedYAML);
}

@Test(description = "reads the user resource from sample")
public void testCompleteUserResource() {
String openApiYAML = readIntoYaml(UserResource.class);
int start = 0;
int end = openApiYAML.length() - 1;
String extractedYAML = openApiYAML.substring(start, end);
String expectedYAML = "openapi: 3.0.0\n" +
"paths:\n" +
" /user:\n" +
" post:\n" +
" summary: Create user\n" +
" description: This can only be done by the logged in user.\n" +
" operationId: createUser\n" +
" requestBody:\n" +
" description: Created user object\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" $ref: '#/components/schemas/User'\n" +
" required: true\n" +
" responses:\n" +
" default:\n" +
" description: no description\n" +
" /user/createWithArray:\n" +
" post:\n" +
" summary: Creates list of users with given input array\n" +
" operationId: createUsersWithArrayInput\n" +
" requestBody:\n" +
" description: List of user object\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" type: array\n" +
" items:\n" +
" $ref: '#/components/schemas/User'\n" +
" required: true\n" +
" responses:\n" +
" default:\n" +
" description: no description\n" +
" /user/createWithList:\n" +
" post:\n" +
" summary: Creates list of users with given input array\n" +
" operationId: createUsersWithListInput\n" +
" requestBody:\n" +
" description: List of user object\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" type: array\n" +
" items:\n" +
" $ref: '#/components/schemas/User'\n" +
" required: true\n" +
" responses:\n" +
" default:\n" +
" description: no description\n" +
" /user/{username}:\n" +
" get:\n" +
" summary: Get user by user name\n" +
" operationId: getUserByName\n" +
" parameters:\n" +
" - name: username\n" +
" in: path\n" +
" description: 'The name that needs to be fetched. Use user1 for testing. '\n" +
" required: true\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: The user\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" $ref: '#/components/schemas/User'\n" +
" 400:\n" +
" description: User not found\n" +
" put:\n" +
" summary: Updated user\n" +
" description: This can only be done by the logged in user.\n" +
" operationId: updateUser\n" +
" parameters:\n" +
" - name: username\n" +
" in: path\n" +
" description: name that need to be deleted\n" +
" required: true\n" +
" schema:\n" +
" type: string\n" +
" requestBody:\n" +
" description: Updated user object\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" $ref: '#/components/schemas/User'\n" +
" required: true\n" +
" responses:\n" +
" 200:\n" +
" description: user updated\n" +
" 400:\n" +
" description: Invalid user supplied\n" +
" 404:\n" +
" description: User not found\n" +
" delete:\n" +
" summary: Delete user\n" +
" description: This can only be done by the logged in user.\n" +
" operationId: deleteUser\n" +
" parameters:\n" +
" - name: username\n" +
" in: path\n" +
" description: The name that needs to be deleted\n" +
" required: true\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" 200:\n" +
" description: user deteled\n" +
" 400:\n" +
" description: Invalid username supplied\n" +
" 404:\n" +
" description: User not found\n" +
" /user/login:\n" +
" get:\n" +
" summary: Logs user into the system\n" +
" operationId: loginUser\n" +
" parameters:\n" +
" - name: username\n" +
" in: query\n" +
" description: The user name for login\n" +
" required: true\n" +
" schema:\n" +
" type: string\n" +
" - name: password\n" +
" in: query\n" +
" description: The password for login in clear text\n" +
" required: true\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: Successfully logged in\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: string\n" +
" application/xml:\n" +
" schema:\n" +
" type: string\n" +
" 400:\n" +
" description: Invalid username/password supplied\n" +
" /user/logout:\n" +
" get:\n" +
" summary: Logs out current logged in user session\n" +
" operationId: logoutUser\n" +
" responses:\n" +
" default:\n" +
" description: no description\n" +
"components:\n" +
" schemas:\n" +
" User:\n" +
" type: object\n" +
" properties:\n" +
" id:\n" +
" type: integer\n" +
" format: int64\n" +
" username:\n" +
" type: string\n" +
" firstName:\n" +
" type: string\n" +
" lastName:\n" +
" type: string\n" +
" email:\n" +
" type: string\n" +
" password:\n" +
" type: string\n" +
" phone:\n" +
" type: string\n" +
" userStatus:\n" +
" type: integer\n" +
" description: User Status\n" +
" format: int32\n" +
" enum:\n" +
" - null\n" +
" xml:\n" +
" name: User";
assertEquals(extractedYAML, expectedYAML);
}
}
Loading

0 comments on commit 44997dc

Please sign in to comment.