24
24
25
25
package org .springdoc .core .utils ;
26
26
27
+ import java .util .Arrays ;
27
28
import java .util .HashMap ;
28
29
import java .util .Locale ;
29
30
import java .util .Map ;
30
31
31
32
import io .swagger .v3 .oas .models .SpecVersion ;
33
+ import org .apache .commons .lang3 .StringUtils ;
32
34
import org .slf4j .Logger ;
33
35
import org .slf4j .LoggerFactory ;
34
36
import org .springdoc .core .properties .SpringDocConfigProperties ;
@@ -97,6 +99,9 @@ public String resolve(String parameterProperty, Locale locale) {
97
99
}
98
100
if (parameterProperty .equals (result ))
99
101
try {
102
+ if (springDocConfigProperties .isTrimKotlinIndent ()) {
103
+ parameterProperty = trimIndent (parameterProperty );
104
+ }
100
105
result = factory .resolveEmbeddedValue (parameterProperty );
101
106
}
102
107
catch (IllegalArgumentException ex ) {
@@ -106,6 +111,44 @@ public String resolve(String parameterProperty, Locale locale) {
106
111
return result ;
107
112
}
108
113
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
+
109
152
/**
110
153
* Gets factory.
111
154
*
0 commit comments