From b21266883a803bfc4a863e00fdd190f711f6d39f Mon Sep 17 00:00:00 2001 From: frantuma Date: Mon, 26 Jun 2023 10:30:25 +0200 Subject: [PATCH] allow siblings in request body schemas OAS 3.1 resolving --- .../v3/core/jackson/ModelResolver.java | 2 +- .../v3/core/util/AnnotationsUtils.java | 4 +- .../java/io/swagger/v3/jaxrs2/Reader.java | 6 +-- .../java/io/swagger/v3/jaxrs2/ReaderTest.java | 46 ++++++++++++++++++ .../SiblingsResourceRequestBody.java | 47 +++++++++++++++++++ .../resources/SiblingsResourceSimple.java | 2 +- 6 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceRequestBody.java diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index 8139c90363..5306572c23 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -704,7 +704,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context addRequiredItem(model, propName); } } - if (property.get$ref() == null) { + if (property.get$ref() == null || openapi31) { if (accessMode != null) { switch (accessMode) { case AUTO: diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java index 8a1e4ced80..48428710f1 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java @@ -753,8 +753,8 @@ public static Optional getSchemaFromAnnotation( schemaObject.setReadOnly(true); schemaObject.setWriteOnly(null); } else if (schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.WRITE_ONLY)) { - schemaObject.setReadOnly(false); - schemaObject.setWriteOnly(null); + schemaObject.setReadOnly(null); + schemaObject.setWriteOnly(true); } else if (schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE)) { schemaObject.setReadOnly(null); schemaObject.setWriteOnly(null); diff --git a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java index 740b56b572..430e9936a4 100644 --- a/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java +++ b/modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java @@ -759,7 +759,7 @@ protected void processRequestBody(Parameter requestBodyParameter, Operation oper io.swagger.v3.oas.annotations.parameters.RequestBody requestBodyAnnotation = getRequestBody(Arrays.asList(paramAnnotations)); if (requestBodyAnnotation != null) { - Optional optionalRequestBody = OperationParser.getRequestBody(requestBodyAnnotation, classConsumes, methodConsumes, components, jsonViewAnnotation); + Optional optionalRequestBody = OperationParser.getRequestBody(requestBodyAnnotation, classConsumes, methodConsumes, components, jsonViewAnnotation, config.isOpenAPI31()); if (optionalRequestBody.isPresent()) { RequestBody requestBody = optionalRequestBody.get(); if (StringUtils.isBlank(requestBody.get$ref()) && @@ -1047,7 +1047,7 @@ protected Operation parseMethod( // RequestBody in Method if (apiRequestBody != null && operation.getRequestBody() == null){ - OperationParser.getRequestBody(apiRequestBody, classConsumes, methodConsumes, components, jsonViewAnnotation).ifPresent( + OperationParser.getRequestBody(apiRequestBody, classConsumes, methodConsumes, components, jsonViewAnnotation, config.isOpenAPI31()).ifPresent( operation::setRequestBody); } @@ -1393,7 +1393,7 @@ protected void setOperationObjectFromApiOperationAnnotation( // RequestBody in Operation if (apiOperation.requestBody() != null && operation.getRequestBody() == null) { - OperationParser.getRequestBody(apiOperation.requestBody(), classConsumes, methodConsumes, components, jsonViewAnnotation).ifPresent( + OperationParser.getRequestBody(apiOperation.requestBody(), classConsumes, methodConsumes, components, jsonViewAnnotation, config.isOpenAPI31()).ifPresent( operation::setRequestBody); } diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java index 0db5b4ed0d..35d63f2ea0 100644 --- a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java @@ -18,6 +18,7 @@ import io.swagger.v3.jaxrs2.resources.ResponseReturnTypeResource; import io.swagger.v3.jaxrs2.resources.SchemaPropertiesResource; import io.swagger.v3.jaxrs2.resources.SiblingsResource; +import io.swagger.v3.jaxrs2.resources.SiblingsResourceRequestBody; import io.swagger.v3.jaxrs2.resources.SiblingsResourceSimple; import io.swagger.v3.jaxrs2.resources.SingleExampleResource; import io.swagger.v3.jaxrs2.resources.BasicFieldsResource; @@ -3508,4 +3509,49 @@ public void testSiblingsOnResource() { " description: Pet\n"; SerializationMatchers.assertEqualsToYaml31(openAPI, yaml); } + + @Test + public void testSiblingsOnResourceRequestBody() { + Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + + OpenAPI openAPI = reader.read(SiblingsResourceRequestBody.class); + String yaml = "openapi: 3.1.0\n" + + "paths:\n" + + " /test/bodyimpl:\n" + + " get:\n" + + " operationId: getBodyImpl\n" + + " requestBody:\n" + + " description: aaa\n" + + " content:\n" + + " application/json:\n" + + " schema:\n" + + " $ref: '#/components/schemas/PetSimple'\n" + + " description: resource pet\n" + + " writeOnly: true\n" + + " responses:\n" + + " default:\n" + + " description: default response\n" + + " content:\n" + + " '*/*': {}\n" + + " /test/bodyimplparam:\n" + + " get:\n" + + " operationId: getBodyImplParam\n" + + " requestBody:\n" + + " content:\n" + + " '*/*':\n" + + " schema:\n" + + " $ref: '#/components/schemas/PetSimple'\n" + + " description: resource pet\n" + + " writeOnly: true\n" + + " responses:\n" + + " default:\n" + + " description: default response\n" + + " content:\n" + + " '*/*': {}\n" + + "components:\n" + + " schemas:\n" + + " PetSimple:\n" + + " description: Pet\n"; + SerializationMatchers.assertEqualsToYaml31(openAPI, yaml); + } } diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceRequestBody.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceRequestBody.java new file mode 100644 index 0000000000..2360ae3f7b --- /dev/null +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceRequestBody.java @@ -0,0 +1,47 @@ +package io.swagger.v3.jaxrs2.resources; + +import io.swagger.v3.jaxrs2.resources.siblings.PetSimple; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import io.swagger.v3.oas.annotations.responses.ApiResponse; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +@Path("/test") +public class SiblingsResourceRequestBody { + @Path("/bodyimpl") + @GET + @Operation( + requestBody = + @RequestBody( + description = "aaa", + content = { + @Content( + mediaType = "application/json", + schema = @Schema( + description = "resource pet", + implementation = PetSimple.class, + accessMode = Schema.AccessMode.WRITE_ONLY)) + })) + public Response getBodyImpl(Object body) { + return null; + } + + @Path("/bodyimplparam") + @GET + public Response getBodyImplParam(@RequestBody( + content = { + @Content( + mediaType = "*/*", + schema = @Schema( + description = "resource pet", + implementation = PetSimple.class, + accessMode = Schema.AccessMode.WRITE_ONLY)) + }) Object body) { + return null; + } +} diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceSimple.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceSimple.java index cac69b3557..9d3e871411 100644 --- a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceSimple.java +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/SiblingsResourceSimple.java @@ -1,6 +1,5 @@ package io.swagger.v3.jaxrs2.resources; -import io.swagger.v3.jaxrs2.resources.siblings.Pet; import io.swagger.v3.jaxrs2.resources.siblings.PetSimple; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -48,4 +47,5 @@ public PetSimple getCart() { public PetSimple getCartImpl() { return null; } + }