Skip to content

Commit 0e94d24

Browse files
committed
implement feature for springdoc.trim-kotlin-indent
1 parent 18edc27 commit 0e94d24

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-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
*

0 commit comments

Comments
 (0)