Skip to content

Commit

Permalink
allow siblings in request body schemas OAS 3.1 resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jun 26, 2023
1 parent 5c75e3b commit 361666d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,8 @@ public static Optional<Schema> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestBody> optionalRequestBody = OperationParser.getRequestBody(requestBodyAnnotation, classConsumes, methodConsumes, components, jsonViewAnnotation);
Optional<RequestBody> optionalRequestBody = OperationParser.getRequestBody(requestBodyAnnotation, classConsumes, methodConsumes, components, jsonViewAnnotation, config.isOpenAPI31());
if (optionalRequestBody.isPresent()) {
RequestBody requestBody = optionalRequestBody.get();
if (StringUtils.isBlank(requestBody.get$ref()) &&
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,4 +47,5 @@ public PetSimple getCart() {
public PetSimple getCartImpl() {
return null;
}

}

0 comments on commit 361666d

Please sign in to comment.