Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dynamic evaluation of description field in the RequestBody #2492

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ protected void calculatePath(HandlerMethod handlerMethod, RouterOperation router
// RequestBody in Operation
requestBuilder.getRequestBodyBuilder()
.buildRequestBodyFromDoc(requestBodyDoc, methodAttributes, components,
methodAttributes.getJsonViewAnnotationForRequestBody())
methodAttributes.getJsonViewAnnotationForRequestBody(), locale)
.ifPresent(operation::setRequestBody);
// requests
operation = requestBuilder.build(handlerMethod, requestMethod, operation, methodAttributes, openAPI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ PropertyResolverUtils propertyResolverUtils(ConfigurableBeanFactory factory, Mes
@Bean
@ConditionalOnMissingBean
@Lazy(false)
RequestBodyService requestBodyBuilder(GenericParameterService parameterBuilder) {
return new RequestBodyService(parameterBuilder);
RequestBodyService requestBodyBuilder(GenericParameterService parameterBuilder, PropertyResolverUtils propertyResolverUtils) {
return new RequestBodyService(parameterBuilder, propertyResolverUtils);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public OpenAPI parse(io.swagger.v3.oas.annotations.Operation apiOperation,
}

// RequestBody in Operation
requestBodyService.buildRequestBodyFromDoc(apiOperation.requestBody(), operation.getRequestBody(), methodAttributes, components).ifPresent(operation::setRequestBody);
requestBodyService.buildRequestBodyFromDoc(apiOperation.requestBody(), operation.getRequestBody(), methodAttributes, components, locale)
.ifPresent(operation::setRequestBody);

// build response
buildResponse(components, apiOperation, operation, methodAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.springdoc.core.service;

import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

Expand All @@ -39,6 +40,7 @@
import org.springdoc.core.models.MethodAttributes;
import org.springdoc.core.models.ParameterInfo;
import org.springdoc.core.models.RequestBodyInfo;
import org.springdoc.core.utils.PropertyResolverUtils;
import org.springdoc.core.utils.SpringDocAnnotationsUtils;

import org.springframework.core.MethodParameter;
Expand All @@ -58,14 +60,21 @@ public class RequestBodyService {
*/
private final GenericParameterService parameterBuilder;

/**
* The Property resolver utils.
*/
private final PropertyResolverUtils propertyResolverUtils;

/**
* Instantiates a new Request body builder.
*
* @param parameterBuilder the parameter builder
* @param propertyResolverUtils the property resolver utils
*/
public RequestBodyService(GenericParameterService parameterBuilder) {
public RequestBodyService(GenericParameterService parameterBuilder, PropertyResolverUtils propertyResolverUtils) {
super();
this.parameterBuilder = parameterBuilder;
this.propertyResolverUtils = propertyResolverUtils;
}

/**
Expand All @@ -76,11 +85,12 @@ public RequestBodyService(GenericParameterService parameterBuilder) {
* @param methodAttributes the method attributes
* @param components the components
* @param jsonViewAnnotation the json view annotation
* @param locale the locale
* @return the optional
*/
public Optional<RequestBody> buildRequestBodyFromDoc(
io.swagger.v3.oas.annotations.parameters.RequestBody requestBody, RequestBody requestBodyOp, MethodAttributes methodAttributes,
Components components, JsonView jsonViewAnnotation) {
Components components, JsonView jsonViewAnnotation, Locale locale) {
String[] classConsumes = methodAttributes.getClassConsumes();
String[] methodConsumes = methodAttributes.getMethodConsumes();

Expand All @@ -95,7 +105,7 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
}

if (StringUtils.isNotBlank(requestBody.description())) {
requestBodyObject.setDescription(requestBody.description());
requestBodyObject.setDescription(propertyResolverUtils.resolve(requestBody.description(), locale));
isEmpty = false;
}

Expand Down Expand Up @@ -191,8 +201,7 @@ private String[] getConsumes(String[] classConsumes) {
*/
public Optional<RequestBody> buildRequestBodyFromDoc(io.swagger.v3.oas.annotations.parameters.RequestBody requestBody,
MethodAttributes methodAttributes, Components components) {
return this.buildRequestBodyFromDoc(requestBody, null, methodAttributes,
components, null);
return this.buildRequestBodyFromDoc(requestBody, null, methodAttributes, components, null, null);
}

/**
Expand All @@ -202,12 +211,13 @@ public Optional<RequestBody> buildRequestBodyFromDoc(io.swagger.v3.oas.annotatio
* @param methodAttributes the method attributes
* @param components the components
* @param jsonViewAnnotation the json view annotation
* @param locale the locale
* @return the optional
*/
public Optional<RequestBody> buildRequestBodyFromDoc(io.swagger.v3.oas.annotations.parameters.RequestBody requestBody,
MethodAttributes methodAttributes, Components components, JsonView jsonViewAnnotation) {
MethodAttributes methodAttributes, Components components, JsonView jsonViewAnnotation, Locale locale) {
return this.buildRequestBodyFromDoc(requestBody, null, methodAttributes,
components, jsonViewAnnotation);
components, jsonViewAnnotation, locale);
}

/**
Expand All @@ -221,9 +231,8 @@ public Optional<RequestBody> buildRequestBodyFromDoc(io.swagger.v3.oas.annotatio
*/
public Optional<RequestBody> buildRequestBodyFromDoc(
io.swagger.v3.oas.annotations.parameters.RequestBody requestBody, RequestBody requestBodyOp, MethodAttributes methodAttributes,
Components components) {
return this.buildRequestBodyFromDoc(requestBody, requestBodyOp, methodAttributes,
components, null);
Components components, Locale locale) {
return this.buildRequestBodyFromDoc(requestBody, requestBodyOp, methodAttributes, components, null, locale);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test.org.springdoc.api.app173;

import io.swagger.v3.oas.annotations.media.Schema;

/**
* The Example object
*/
@Schema
public class Example {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package test.org.springdoc.api.app173;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

import static org.springframework.http.HttpStatus.OK;

/**
* The Example Controller
*/
@RestController
public class ExampleController {

@PostMapping("/example")
@Operation(summary = "insert example", description = "Allows to insert an example")
public ResponseEntity<UUID> postExample(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "${example.description}") @RequestBody Example example) {
return new ResponseEntity<>(UUID.randomUUID(), OK);
}

@PutMapping("/example")
@Operation(summary = "update example", description = "Allows to update an example")
public ResponseEntity<UUID> putExample(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "${example2.description:Default description for example}") @RequestBody Example example) {
return new ResponseEntity<>(UUID.randomUUID(), OK);
}

@PatchMapping("/example")
@Operation(summary = "patch example", description = "Allows to patch an example")
public ResponseEntity<UUID> patchExample(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Description without the use of variables") @RequestBody Example example) {
return new ResponseEntity<>(UUID.randomUUID(), OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test.org.springdoc.api.app173;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;
import test.org.springdoc.api.AbstractSpringDocTest;

/**
* The type Spring doc app 173 test.
*/
@TestPropertySource(properties = "example.description=The example object")
public class SpringDocApp173Test extends AbstractSpringDocTest {

/**
* The type Spring doc test app.
*/
@SpringBootApplication
static class SpringDocTestApp {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"tags": [
{
"name": "example-controller",
"description": "The Example Controller"
}
],
"paths": {
"/example": {
"put": {
"tags": [
"example-controller"
],
"summary": "update example",
"description": "Allows to update an example",
"operationId": "putExample",
"requestBody": {
"description": "Default description for example",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Example"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string",
"format": "uuid"
}
}
}
}
}
},
"post": {
"tags": [
"example-controller"
],
"summary": "insert example",
"description": "Allows to insert an example",
"operationId": "postExample",
"requestBody": {
"description": "The example object",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Example"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string",
"format": "uuid"
}
}
}
}
}
},
"patch": {
"tags": [
"example-controller"
],
"summary": "patch example",
"description": "Allows to patch an example",
"operationId": "patchExample",
"requestBody": {
"description": "Description without the use of variables",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Example"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string",
"format": "uuid"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Example": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"description": "The Example object"
}
}
}
}