Skip to content

Commit 44997dc

Browse files
authored
Merge pull request #2345 from swagger-api/bugfix/schema-simple-objects
Schema of String and Wrapper Objects, OpenApi version to 3.0.0
2 parents cb7cc2e + 71b0ab4 commit 44997dc

File tree

7 files changed

+567
-15
lines changed

7 files changed

+567
-15
lines changed

modules/swagger-core/src/test/resources/ModelWithSecurityRequirements.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"openapi": "3.0.0-rc2",
2+
"openapi": "3.0.0",
33
"info": {
44
"title": "Swagger Petstore",
55
"contact": {

modules/swagger-jaxrs2/src/main/java/io/swagger/jaxrs2/OperationParser.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ public static Optional<Schema> getSchemaFromAnnotation(io.swagger.oas.annotation
174174
schemaObject.setExclusiveMinimum(schema.exclusiveMinimum());
175175
isEmpty = false;
176176
}
177-
if (schema.maxLength() > 0)
177+
if (schema.maxLength() > 0) {
178178
if (schema.maxProperties() > 0) {
179179
schemaObject.setMaxProperties(schema.maxProperties());
180180
isEmpty = false;
181181
}
182+
}
182183
if (schema.minProperties() > 0) {
183184
schemaObject.setMinProperties(schema.minProperties());
184185
isEmpty = false;
@@ -362,13 +363,18 @@ public static Optional<Content> getContent(io.swagger.oas.annotations.media.Cont
362363
Class<?> schemaImplementation = annotationContent.schema().implementation();
363364
Map<String, Schema> schemaMap;
364365
if (schemaImplementation != Void.class) {
365-
schemaMap = ModelConverters.getInstance().readAll(schemaImplementation);
366-
schemaMap.forEach((key, schema) -> {
367-
components.addSchemas(key, schema);
368-
});
369366
Schema schemaObject = new Schema();
370-
schemaObject.set$ref(COMPONENTS_REF + schemaImplementation.getSimpleName());
367+
if (schemaImplementation.getName().startsWith("java.lang")) {
368+
schemaObject.setType(schemaImplementation.getSimpleName().toLowerCase());
369+
} else {
370+
schemaMap = ModelConverters.getInstance().readAll(schemaImplementation);
371+
schemaMap.forEach((key, schema) -> {
372+
components.addSchemas(key, schema);
373+
});
374+
schemaObject.set$ref(COMPONENTS_REF + schemaImplementation.getSimpleName());
375+
}
371376
mediaType.setSchema(schemaObject);
377+
372378
} else {
373379
getSchemaFromAnnotation(annotationContent.schema()).ifPresent(mediaType::setSchema);
374380
}

modules/swagger-jaxrs2/src/test/java/io/swagger/jaxrs2/annotations/operations/AnnotatedOperationMethodTests.java

+202-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.swagger.jaxrs2.annotations.AbstractAnnotationTest;
44
import io.swagger.jaxrs2.resources.PetResource;
5+
import io.swagger.jaxrs2.resources.UserResource;
56
import io.swagger.oas.annotations.Operation;
67
import io.swagger.oas.annotations.media.Content;
78
import io.swagger.oas.annotations.media.ExampleObject;
@@ -208,13 +209,13 @@ public void simpleGet() {
208209
}
209210
}
210211

211-
@Test(description = "reads an operation from sample")
212-
public void testCompleteOperation() {
212+
@Test(description = "reads the pet resource from sample")
213+
public void testCompletePetResource() {
213214
String openApiYAML = readIntoYaml(PetResource.class);
214215
int start = 0;
215216
int end = openApiYAML.length() - 1;
216217
String extractedYAML = openApiYAML.substring(start, end);
217-
String expectedYAML = "openapi: 3.0.0-rc2\n" +
218+
String expectedYAML = "openapi: 3.0.0\n" +
218219
"paths:\n" +
219220
" /pet/{petId}:\n" +
220221
" get:\n" +
@@ -226,7 +227,6 @@ public void testCompleteOperation() {
226227
" in: path\n" +
227228
" description: ID of pet that needs to be fetched\n" +
228229
" required: true\n" +
229-
" explode: false\n" +
230230
" schema:\n" +
231231
" type: integer\n" +
232232
" format: int64\n" +
@@ -288,7 +288,6 @@ public void testCompleteOperation() {
288288
" in: query\n" +
289289
" description: Status values that need to be considered for filter\n" +
290290
" required: true\n" +
291-
" explode: false\n" +
292291
" schema:\n" +
293292
" type: string\n" +
294293
" - name: skip\n" +
@@ -319,7 +318,6 @@ public void testCompleteOperation() {
319318
" in: query\n" +
320319
" description: Tags to filter by\n" +
321320
" required: true\n" +
322-
" explode: false\n" +
323321
" schema:\n" +
324322
" type: string\n" +
325323
" responses:\n" +
@@ -341,6 +339,8 @@ public void testCompleteOperation() {
341339
" format: int64\n" +
342340
" name:\n" +
343341
" type: string\n" +
342+
" xml:\n" +
343+
" name: Category\n" +
344344
" Tag:\n" +
345345
" type: object\n" +
346346
" properties:\n" +
@@ -349,6 +349,8 @@ public void testCompleteOperation() {
349349
" format: int64\n" +
350350
" name:\n" +
351351
" type: string\n" +
352+
" xml:\n" +
353+
" name: Tag\n" +
352354
" Pet:\n" +
353355
" type: object\n" +
354356
" properties:\n" +
@@ -361,17 +363,210 @@ public void testCompleteOperation() {
361363
" type: string\n" +
362364
" photoUrls:\n" +
363365
" type: array\n" +
366+
" xml:\n" +
367+
" wrapped: true\n" +
364368
" items:\n" +
365369
" type: string\n" +
370+
" xml:\n" +
371+
" name: photoUrl\n" +
366372
" tags:\n" +
367373
" type: array\n" +
374+
" xml:\n" +
375+
" wrapped: true\n" +
368376
" items:\n" +
369377
" $ref: '#/components/schemas/Tag'\n" +
370378
" status:\n" +
371379
" type: string\n" +
372380
" description: pet status in the store\n" +
373381
" enum:\n" +
374-
" - available,pending,sold";
382+
" - available,pending,sold\n" +
383+
" xml:\n" +
384+
" name: Pet";
385+
assertEquals(extractedYAML, expectedYAML);
386+
}
387+
388+
@Test(description = "reads the user resource from sample")
389+
public void testCompleteUserResource() {
390+
String openApiYAML = readIntoYaml(UserResource.class);
391+
int start = 0;
392+
int end = openApiYAML.length() - 1;
393+
String extractedYAML = openApiYAML.substring(start, end);
394+
String expectedYAML = "openapi: 3.0.0\n" +
395+
"paths:\n" +
396+
" /user:\n" +
397+
" post:\n" +
398+
" summary: Create user\n" +
399+
" description: This can only be done by the logged in user.\n" +
400+
" operationId: createUser\n" +
401+
" requestBody:\n" +
402+
" description: Created user object\n" +
403+
" content:\n" +
404+
" '*/*':\n" +
405+
" schema:\n" +
406+
" $ref: '#/components/schemas/User'\n" +
407+
" required: true\n" +
408+
" responses:\n" +
409+
" default:\n" +
410+
" description: no description\n" +
411+
" /user/createWithArray:\n" +
412+
" post:\n" +
413+
" summary: Creates list of users with given input array\n" +
414+
" operationId: createUsersWithArrayInput\n" +
415+
" requestBody:\n" +
416+
" description: List of user object\n" +
417+
" content:\n" +
418+
" '*/*':\n" +
419+
" schema:\n" +
420+
" type: array\n" +
421+
" items:\n" +
422+
" $ref: '#/components/schemas/User'\n" +
423+
" required: true\n" +
424+
" responses:\n" +
425+
" default:\n" +
426+
" description: no description\n" +
427+
" /user/createWithList:\n" +
428+
" post:\n" +
429+
" summary: Creates list of users with given input array\n" +
430+
" operationId: createUsersWithListInput\n" +
431+
" requestBody:\n" +
432+
" description: List of user object\n" +
433+
" content:\n" +
434+
" '*/*':\n" +
435+
" schema:\n" +
436+
" type: array\n" +
437+
" items:\n" +
438+
" $ref: '#/components/schemas/User'\n" +
439+
" required: true\n" +
440+
" responses:\n" +
441+
" default:\n" +
442+
" description: no description\n" +
443+
" /user/{username}:\n" +
444+
" get:\n" +
445+
" summary: Get user by user name\n" +
446+
" operationId: getUserByName\n" +
447+
" parameters:\n" +
448+
" - name: username\n" +
449+
" in: path\n" +
450+
" description: 'The name that needs to be fetched. Use user1 for testing. '\n" +
451+
" required: true\n" +
452+
" schema:\n" +
453+
" type: string\n" +
454+
" responses:\n" +
455+
" default:\n" +
456+
" description: The user\n" +
457+
" content:\n" +
458+
" application/json:\n" +
459+
" schema:\n" +
460+
" $ref: '#/components/schemas/User'\n" +
461+
" 400:\n" +
462+
" description: User not found\n" +
463+
" put:\n" +
464+
" summary: Updated user\n" +
465+
" description: This can only be done by the logged in user.\n" +
466+
" operationId: updateUser\n" +
467+
" parameters:\n" +
468+
" - name: username\n" +
469+
" in: path\n" +
470+
" description: name that need to be deleted\n" +
471+
" required: true\n" +
472+
" schema:\n" +
473+
" type: string\n" +
474+
" requestBody:\n" +
475+
" description: Updated user object\n" +
476+
" content:\n" +
477+
" '*/*':\n" +
478+
" schema:\n" +
479+
" $ref: '#/components/schemas/User'\n" +
480+
" required: true\n" +
481+
" responses:\n" +
482+
" 200:\n" +
483+
" description: user updated\n" +
484+
" 400:\n" +
485+
" description: Invalid user supplied\n" +
486+
" 404:\n" +
487+
" description: User not found\n" +
488+
" delete:\n" +
489+
" summary: Delete user\n" +
490+
" description: This can only be done by the logged in user.\n" +
491+
" operationId: deleteUser\n" +
492+
" parameters:\n" +
493+
" - name: username\n" +
494+
" in: path\n" +
495+
" description: The name that needs to be deleted\n" +
496+
" required: true\n" +
497+
" schema:\n" +
498+
" type: string\n" +
499+
" responses:\n" +
500+
" 200:\n" +
501+
" description: user deteled\n" +
502+
" 400:\n" +
503+
" description: Invalid username supplied\n" +
504+
" 404:\n" +
505+
" description: User not found\n" +
506+
" /user/login:\n" +
507+
" get:\n" +
508+
" summary: Logs user into the system\n" +
509+
" operationId: loginUser\n" +
510+
" parameters:\n" +
511+
" - name: username\n" +
512+
" in: query\n" +
513+
" description: The user name for login\n" +
514+
" required: true\n" +
515+
" schema:\n" +
516+
" type: string\n" +
517+
" - name: password\n" +
518+
" in: query\n" +
519+
" description: The password for login in clear text\n" +
520+
" required: true\n" +
521+
" schema:\n" +
522+
" type: string\n" +
523+
" responses:\n" +
524+
" default:\n" +
525+
" description: Successfully logged in\n" +
526+
" content:\n" +
527+
" application/json:\n" +
528+
" schema:\n" +
529+
" type: string\n" +
530+
" application/xml:\n" +
531+
" schema:\n" +
532+
" type: string\n" +
533+
" 400:\n" +
534+
" description: Invalid username/password supplied\n" +
535+
" /user/logout:\n" +
536+
" get:\n" +
537+
" summary: Logs out current logged in user session\n" +
538+
" operationId: logoutUser\n" +
539+
" responses:\n" +
540+
" default:\n" +
541+
" description: no description\n" +
542+
"components:\n" +
543+
" schemas:\n" +
544+
" User:\n" +
545+
" type: object\n" +
546+
" properties:\n" +
547+
" id:\n" +
548+
" type: integer\n" +
549+
" format: int64\n" +
550+
" username:\n" +
551+
" type: string\n" +
552+
" firstName:\n" +
553+
" type: string\n" +
554+
" lastName:\n" +
555+
" type: string\n" +
556+
" email:\n" +
557+
" type: string\n" +
558+
" password:\n" +
559+
" type: string\n" +
560+
" phone:\n" +
561+
" type: string\n" +
562+
" userStatus:\n" +
563+
" type: integer\n" +
564+
" description: User Status\n" +
565+
" format: int32\n" +
566+
" enum:\n" +
567+
" - null\n" +
568+
" xml:\n" +
569+
" name: User";
375570
assertEquals(extractedYAML, expectedYAML);
376571
}
377572
}

0 commit comments

Comments
 (0)