Skip to content

Commit c3d2c32

Browse files
committed
Merge branch 'remove-kotlin-indent' of https://github.com/e-build/springdoc-openapi into e-build-remove-kotlin-indent
2 parents 6ac8766 + 0e94d24 commit c3d2c32

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/SpringDocConfigProperties.java

+23
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,29 @@ public class SpringDocConfigProperties {
256256
*/
257257
private boolean nullableRequestParameterEnabled;
258258

259+
/**
260+
* The trim kotlin indent.
261+
*/
262+
private boolean trimKotlinIndent;
263+
264+
/**
265+
* Gets trim kotlin indent.
266+
*
267+
* @return the trim kotlin indent.
268+
*/
269+
public boolean isTrimKotlinIndent() {
270+
return trimKotlinIndent;
271+
}
272+
273+
/**
274+
* Sets trim kotlin indent
275+
*
276+
* @param trimKotlinIndent the trim kotlin indent.
277+
*/
278+
public void setTrimKotlinIndent(boolean trimKotlinIndent) {
279+
this.trimKotlinIndent = trimKotlinIndent;
280+
}
281+
259282
/**
260283
* Gets override with generic response.
261284
*

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

+43
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
package org.springdoc.core.utils;
2626

27+
import java.util.Arrays;
2728
import java.util.HashMap;
2829
import java.util.Locale;
2930
import java.util.Map;
3031

3132
import io.swagger.v3.oas.models.SpecVersion;
33+
import org.apache.commons.lang3.StringUtils;
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
3436
import org.springdoc.core.properties.SpringDocConfigProperties;
@@ -97,6 +99,9 @@ public String resolve(String parameterProperty, Locale locale) {
9799
}
98100
if (parameterProperty.equals(result))
99101
try {
102+
if (springDocConfigProperties.isTrimKotlinIndent()) {
103+
parameterProperty = trimIndent(parameterProperty);
104+
}
100105
result = factory.resolveEmbeddedValue(parameterProperty);
101106
}
102107
catch (IllegalArgumentException ex) {
@@ -106,6 +111,44 @@ public String resolve(String parameterProperty, Locale locale) {
106111
return result;
107112
}
108113

114+
/**
115+
* Returns a string where all leading indentation has been removed from each line.
116+
* It detects the smallest common indentation of all the lines in the input string,
117+
* and removes it.
118+
*
119+
* @param text The original string with possible leading indentation.
120+
* @return The string with leading indentation removed from each line.
121+
*/
122+
private String trimIndent(String text) {
123+
if (text == null) {
124+
return null;
125+
}
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+
}
134+
135+
private int resolveMinIndent(String[] lines) {
136+
return Arrays.stream(lines)
137+
.filter(line -> !line.trim().isEmpty())
138+
.mapToInt(this::countLeadingSpaces)
139+
.min()
140+
.orElse(0);
141+
}
142+
143+
private int countLeadingSpaces(String line) {
144+
int count = 0;
145+
for (char ch : line.toCharArray()) {
146+
if (ch != ' ') break;
147+
count++;
148+
}
149+
return count;
150+
}
151+
109152
/**
110153
* Gets factory.
111154
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package test.org.springdoc.api.app11
2+
3+
import io.swagger.v3.oas.annotations.Operation
4+
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.RequestParam
6+
import org.springframework.web.bind.annotation.RestController
7+
8+
@RestController
9+
class ExampleController {
10+
11+
@Operation(
12+
summary = "foo api",
13+
description = """
14+
this api is foo
15+
16+
#### Custom exception case
17+
| Http Status | Error Code | Error Message | Error Data | Remark |
18+
|-------------|-------------|--------------|------------|-----------|
19+
| 403 | NO_PERMISSION |This request is only available to administrators. | | |
20+
| 400 | STORE_NOT_FOUND |Store not found. | | |
21+
"""
22+
)
23+
@GetMapping("/foo/remove-kotlin-indent")
24+
fun readFoo(@RequestParam name: String?) = FooResponse("Hello ${name ?: "world"}")
25+
26+
}
27+
28+
data class FooResponse(
29+
private val name: String,
30+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* * Copyright 2019-2023 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app11
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication
22+
import org.springframework.context.annotation.ComponentScan
23+
import org.springframework.test.context.TestPropertySource
24+
import test.org.springdoc.api.AbstractKotlinSpringDocMVCTest
25+
26+
@TestPropertySource(properties = ["springdoc.trim-kotlin-indent=true"])
27+
class SpringDocApp11Test : AbstractKotlinSpringDocMVCTest() {
28+
@SpringBootApplication
29+
@ComponentScan(basePackages = ["org.springdoc", "test.org.springdoc.api.app11"])
30+
class DemoApplication
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/foo/remove-kotlin-indent": {
15+
"get": {
16+
"tags": [
17+
"example-controller"
18+
],
19+
"summary": "foo api",
20+
"description": "\nthis api is foo\n\n#### Custom exception case\n| Http Status | Error Code | Error Message | Error Data | Remark |\n|-------------|-------------|--------------|------------|-----------|\n| 403 | NO_PERMISSION |This request is only available to administrators. | | |\n| 400 | STORE_NOT_FOUND |Store not found. | | |\n",
21+
"operationId": "readFoo",
22+
"parameters": [
23+
{
24+
"name": "name",
25+
"in": "query",
26+
"required": false,
27+
"schema": {
28+
"type": "string"
29+
}
30+
}
31+
],
32+
"responses": {
33+
"200": {
34+
"description": "OK",
35+
"content": {
36+
"*/*": {
37+
"schema": {
38+
"$ref": "#/components/schemas/FooResponse"
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},
47+
"components": {
48+
"schemas": {
49+
"FooResponse": {
50+
"type": "object",
51+
"properties": {
52+
"name": {
53+
"type": "string",
54+
"writeOnly": true
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)