Skip to content

Commit c80f191

Browse files
committed
apply 'springdoc.trim-kotlin-indent' to schema annotation properties
1 parent 4fd4344 commit c80f191

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import io.swagger.v3.oas.models.PathItem.HttpMethod;
7070
import io.swagger.v3.oas.models.Paths;
7171
import io.swagger.v3.oas.models.SpecVersion;
72+
import io.swagger.v3.oas.models.media.Schema;
7273
import io.swagger.v3.oas.models.media.StringSchema;
7374
import io.swagger.v3.oas.models.parameters.Parameter;
7475
import io.swagger.v3.oas.models.responses.ApiResponses;
@@ -100,6 +101,7 @@
100101
import org.springdoc.core.service.GenericResponseService;
101102
import org.springdoc.core.service.OpenAPIService;
102103
import org.springdoc.core.service.OperationService;
104+
import org.springdoc.core.utils.PropertyResolverUtils;
103105
import org.springdoc.core.utils.SpringDocUtils;
104106

105107
import org.springframework.aop.support.AopUtils;
@@ -352,6 +354,9 @@ protected OpenAPI getOpenApi(Locale locale) {
352354
}
353355
getPaths(mappingsMap, finalLocale, openAPI);
354356

357+
if (springDocConfigProperties.isTrimKotlinIndent())
358+
this.trimIndent(openAPI);
359+
355360
Optional<CloudFunctionProvider> cloudFunctionProviderOptional = springDocProviders.getSpringCloudFunctionProvider();
356361
cloudFunctionProviderOptional.ifPresent(cloudFunctionProvider -> {
357362
List<RouterOperation> routerOperationList = cloudFunctionProvider.getRouterOperations(openAPI);
@@ -384,7 +389,6 @@ protected OpenAPI getOpenApi(Locale locale) {
384389
if (!CollectionUtils.isEmpty(openAPI.getServers()) && !openAPI.getServers().equals(serversCopy))
385390
openAPIService.setServersPresent(true);
386391

387-
388392
openAPIService.setCachedOpenAPI(openAPI, finalLocale);
389393

390394
LOGGER.info("Init duration for springdoc-openapi is: {} ms",
@@ -396,12 +400,62 @@ protected OpenAPI getOpenApi(Locale locale) {
396400
openAPIService.updateServers(openAPI);
397401
}
398402
openAPIService.updateServers(openAPI);
399-
return openAPI; }
400-
finally {
403+
return openAPI;
404+
} finally {
401405
this.reentrantLock.unlock();
402406
}
403407
}
404408

409+
private void trimIndent(OpenAPI openAPI) {
410+
trimComponents(openAPI);
411+
trimPaths(openAPI);
412+
}
413+
414+
private void trimComponents(OpenAPI openAPI) {
415+
final PropertyResolverUtils propertyResolverUtils = operationParser.getPropertyResolverUtils();
416+
if (openAPI.getComponents() == null || openAPI.getComponents().getSchemas() == null) {
417+
return;
418+
}
419+
for (Schema<?> schema : openAPI.getComponents().getSchemas().values()) {
420+
schema.description(propertyResolverUtils.trimIndent(schema.getDescription()));
421+
if (schema.getProperties() == null) {
422+
continue;
423+
}
424+
for (Object prop : schema.getProperties().values()) {
425+
if (prop instanceof Schema<?> schemaProp) {
426+
schemaProp.setDescription(propertyResolverUtils.trimIndent(schemaProp.getDescription()));
427+
}
428+
}
429+
}
430+
}
431+
432+
private void trimPaths(OpenAPI openAPI) {
433+
final PropertyResolverUtils propertyResolverUtils = operationParser.getPropertyResolverUtils();
434+
if (openAPI.getPaths() == null) {
435+
return;
436+
}
437+
for (PathItem value : openAPI.getPaths().values()) {
438+
value.setDescription(propertyResolverUtils.trimIndent(value.getDescription()));
439+
trimIndentOperation(value.getGet());
440+
trimIndentOperation(value.getPut());
441+
trimIndentOperation(value.getPost());
442+
trimIndentOperation(value.getDelete());
443+
trimIndentOperation(value.getOptions());
444+
trimIndentOperation(value.getHead());
445+
trimIndentOperation(value.getPatch());
446+
trimIndentOperation(value.getTrace());
447+
}
448+
}
449+
450+
private void trimIndentOperation(Operation operation) {
451+
final PropertyResolverUtils propertyResolverUtils = operationParser.getPropertyResolverUtils();
452+
if (operation == null) {
453+
return;
454+
}
455+
operation.setSummary(propertyResolverUtils.trimIndent(operation.getSummary()));
456+
operation.setDescription(propertyResolverUtils.trimIndent(operation.getDescription()));
457+
}
458+
405459
/**
406460
* Gets paths.
407461
*

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/OperationService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,12 @@ public Operation mergeOperation(Operation operation, Operation operationModel) {
646646
public JavadocProvider getJavadocProvider() {
647647
return parameterBuilder.getJavadocProvider();
648648
}
649+
650+
/**
651+
* Gets propertyResolverUtils
652+
* @return propertyResolverUtils
653+
*/
654+
public PropertyResolverUtils getPropertyResolverUtils(){
655+
return parameterBuilder.getPropertyResolverUtils();
656+
}
649657
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ public String resolve(String parameterProperty, Locale locale) {
9999
}
100100
if (parameterProperty.equals(result))
101101
try {
102-
if (springDocConfigProperties.isTrimKotlinIndent()) {
103-
parameterProperty = trimIndent(parameterProperty);
104-
}
105102
result = factory.resolveEmbeddedValue(parameterProperty);
106103
}
107104
catch (IllegalArgumentException ex) {
@@ -119,18 +116,23 @@ public String resolve(String parameterProperty, Locale locale) {
119116
* @param text The original string with possible leading indentation.
120117
* @return The string with leading indentation removed from each line.
121118
*/
122-
private String trimIndent(String text) {
123-
if (text == null) {
124-
return null;
119+
public String trimIndent(String text) {
120+
try {
121+
if (text == null) {
122+
return null;
123+
}
124+
final String newLine = "\n";
125+
String[] lines = text.split(newLine);
126+
int minIndent = resolveMinIndent(lines);
127+
return Arrays.stream(lines)
128+
.map(line -> line.substring(Math.min(line.length(), minIndent)))
129+
.reduce((a, b) -> a + newLine + b)
130+
.orElse(StringUtils.EMPTY);
131+
} catch (Exception ex){
132+
LOGGER.warn(ex.getMessage());
133+
return text;
125134
}
126-
final String newLine = "\n";
127-
String[] lines = text.split(newLine);
128-
int minIndent = resolveMinIndent(lines);
129-
return Arrays.stream(lines)
130-
.map(line -> line.substring(Math.min(line.length(), minIndent)))
131-
.reduce((a, b) -> a + newLine + b)
132-
.orElse(StringUtils.EMPTY);
133-
}
135+
}
134136

135137
private int resolveMinIndent(String[] lines) {
136138
return Arrays.stream(lines)
@@ -222,4 +224,4 @@ public Map<String, Object> resolveExtensions(Locale locale, Map<String, Object>
222224
else
223225
return extensions;
224226
}
225-
}
227+
}

0 commit comments

Comments
 (0)