Skip to content

Commit

Permalink
Merge pull request #1858 from Azquelt/requestbody-default-required
Browse files Browse the repository at this point in the history
Require requestBody by default
  • Loading branch information
MikeEdgar authored Jun 6, 2024
2 parents e9b7241 + a5376ab commit 1eb6dc7
Show file tree
Hide file tree
Showing 65 changed files with 207 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class RequestBodyImpl extends ExtensibleImpl<RequestBody> implements Requ
private String description;
private Content content;
private Boolean required;
private Boolean requiredDefault;
private boolean isRequiredSet = false;

/**
* @see org.eclipse.microprofile.openapi.models.Reference#getRef()
Expand Down Expand Up @@ -73,7 +75,11 @@ public void setContent(Content content) {
*/
@Override
public Boolean getRequired() {
return this.required;
if (isRequiredSet) {
return this.required;
} else {
return this.requiredDefault;
}
}

/**
Expand All @@ -82,6 +88,35 @@ public Boolean getRequired() {
@Override
public void setRequired(Boolean required) {
this.required = required;
isRequiredSet = true;
}

/**
* Sets the value to use for {@code required} if {@link #setRequired(Boolean)} has not been called.
* <p>
* If this method is called, {@link #getRequired()} will return this value unless {@link #setRequired(Boolean)} is called.
*
* @param requiredDefault the default value for {@code required}
* @return this instance
*/
public RequestBodyImpl setRequiredDefault(Boolean requiredDefault) {
this.requiredDefault = requiredDefault;
return this;
}

/**
* Returns whether {@link #setRequired(Boolean)} has been called on a request body.
* <p>
* Always returns {@code true} if the request body is not a {@code RequestBodyImpl}
*
* @param requestBody the request body
* @return {@code true} if {@code setRequired} has been called
*/
public static boolean isRequiredSet(RequestBody requestBody) {
if (requestBody instanceof RequestBodyImpl) {
return ((RequestBodyImpl) requestBody).isRequiredSet;
} else {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ public List<AnnotationInstance> getRepeatableAnnotations(AnnotationTarget target
@Override
public RequestBody read(AnnotationInstance annotation) {
IoLogging.logger.singleAnnotation("@RequestBody");
RequestBody requestBody = new RequestBodyImpl();
RequestBodyImpl requestBody = new RequestBodyImpl();
requestBody.setDescription(value(annotation, PROP_DESCRIPTION));
requestBody.setContent(contentIO().read(annotation.value(PROP_CONTENT), ContentIO.Direction.INPUT));
requestBody.setRequired(value(annotation, PROP_REQUIRED));
requestBody.setRef(ReferenceType.REQUEST_BODY.refValue(annotation));
requestBody.setExtensions(extensionIO().readExtensible(annotation));
Boolean required = value(annotation, PROP_REQUIRED);
if (required != null) {
requestBody.setRequired(required);
} else {
requestBody.setRequiredDefault(Boolean.TRUE);
}
return requestBody;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ default RequestBody processRequestBody(final AnnotationScannerContext context,
}
}

if (requestBody.getRequired() == null && TypeUtil.isOptional(requestBodyType)) {
if (!RequestBodyImpl.isRequiredSet(requestBody) && TypeUtil.isOptional(requestBodyType)) {
requestBody.setRequired(Boolean.FALSE);
}

Expand All @@ -818,7 +818,7 @@ default RequestBody processRequestBody(final AnnotationScannerContext context,
&& getConsumes(context) != null) {
if (params.getFormBodyContent() != null) {
if (requestBody == null) {
requestBody = new RequestBodyImpl();
requestBody = new RequestBodyImpl().setRequiredDefault(Boolean.TRUE);
}
requestBody.setContent(params.getFormBodyContent());
} else {
Expand All @@ -844,14 +844,14 @@ && getConsumes(context) != null) {
}

if (requestBody == null) {
requestBody = new RequestBodyImpl();
requestBody = new RequestBodyImpl().setRequiredDefault(Boolean.TRUE);
}

if (schema != null) {
ModelUtil.setRequestBodySchema(requestBody, schema, getConsumesForRequestBody(context));
}

if (requestBody.getRequired() == null && TypeUtil.isOptional(requestBodyType)) {
if (!RequestBodyImpl.isRequiredSet(requestBody) && TypeUtil.isOptional(requestBodyType)) {
requestBody.setRequired(Boolean.FALSE);
}

Expand Down Expand Up @@ -929,7 +929,7 @@ default void setRequestBodyConstraints(AnnotationScannerContext context, Request
.filter(Objects::nonNull)
.forEach(schema -> constraintScanner.get().applyConstraints(paramTarget, schema, null,
(target, name) -> {
if (requestBody.getRequired() == null) {
if (!RequestBodyImpl.isRequiredSet(requestBody)) {
requestBody.setRequired(Boolean.TRUE);
}
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*
* @author Michael Edgar {@literal <[email protected]>}
*/
@OpenAPIDefinition(info = @Info(title = "Test Request Body", version = "1.0"), components = @Components(requestBodies = @RequestBody(name = "test", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class)), required = true)))
@OpenAPIDefinition(info = @Info(title = "Test Request Body", version = "1.0"), components = @Components(requestBodies = {
@RequestBody(name = "test", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class)), required = true),
@RequestBody(name = "testRequiredDefault", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class))),
@RequestBody(name = "testNotRequired", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class)), required = false) }))
public class RequestBodyTestApplication extends Application {

public static class SomeObject {
Expand Down Expand Up @@ -56,7 +59,7 @@ public void testRef(@RequestBody(ref = "test") SomeObject body) {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("defaults")
public void testDefaults(@RequestBody(required = true) SomeObject body) {
public void testDefaults(@RequestBody SomeObject body) {
return;
}

Expand All @@ -66,5 +69,12 @@ public void testDefaults(@RequestBody(required = true) SomeObject body) {
public void testAny(SomeObject body) {
return;
}

@POST
@Path("notRequired")
@Consumes(MediaType.APPLICATION_JSON)
public void testNotRequired(@RequestBody(required = false) SomeObject body) {
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*
* @author Michael Edgar {@literal <[email protected]>}
*/
@OpenAPIDefinition(info = @Info(title = "Test Request Body", version = "1.0"), components = @Components(requestBodies = @RequestBody(name = "test", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class)), required = true)))
@OpenAPIDefinition(info = @Info(title = "Test Request Body", version = "1.0"), components = @Components(requestBodies = {
@RequestBody(name = "test", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class)), required = true),
@RequestBody(name = "testRequiredDefault", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class))),
@RequestBody(name = "testNotRequired", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = DifferentObject.class)), required = false) }))
public class RequestBodyTestApplication extends Application {

public static class SomeObject {
Expand Down Expand Up @@ -56,7 +59,7 @@ public void testRef(@RequestBody(ref = "test") SomeObject body) {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("defaults")
public void testDefaults(@RequestBody(required = true) SomeObject body) {
public void testDefaults(@RequestBody SomeObject body) {
return;
}

Expand All @@ -66,5 +69,12 @@ public void testDefaults(@RequestBody(required = true) SomeObject body) {
public void testAny(SomeObject body) {
return;
}

@POST
@Path("notRequired")
@Consumes(MediaType.APPLICATION_JSON)
public void testNotRequired(@RequestBody(required = false) SomeObject body) {
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"Test"
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/ext": {
"post": {
"requestBody": {
"required": true,
"content": {
"text/plain": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
}
],
"requestBody": {
"required": true,
"content": {
"application/x-www-form-urlencoded": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"/uploadIcon": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
Expand Down Expand Up @@ -91,6 +92,7 @@
"/uploadIcon/reactive": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"/optional/n4": {
"post": {
"requestBody": {
"required": true,
"content": {
"text/plain": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"post": {
"requestBody": {
"required": true,
"content": {
"application/x-www-form-urlencoded": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/multipart-form-data-input/post": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/multipart-form-data-map/post": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/multipart-mixed-array/post": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/mixed": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/multipart-mixed/post": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/mixed": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
],
"summary": "create",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"post": {
"requestBody": {
"required": true,
"content": {
"text/plain": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/v1/generics": {
"post": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"post" : {
"requestBody" : {
"required": true,
"content" : {
"application/json" : {
"schema" : {
Expand Down Expand Up @@ -49,6 +50,7 @@
},
"delete" : {
"requestBody" : {
"required": true,
"content" : {
"application/json" : {
"schema" : {
Expand Down
Loading

0 comments on commit 1eb6dc7

Please sign in to comment.