From 665d72356b6ed57c10e627e883d74c736127b8a9 Mon Sep 17 00:00:00 2001 From: ryan-dia Date: Thu, 6 Feb 2025 22:59:34 +0900 Subject: [PATCH 1/2] Refactor trimIndent method: streamline indentation removal and handle exceptions gracefully --- .../core/utils/PropertyResolverUtils.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java index 3178f1f6f..6eff3e174 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java @@ -32,7 +32,8 @@ import java.util.Map; import io.swagger.v3.oas.models.SpecVersion; -import org.apache.commons.lang3.StringUtils; +import java.util.stream.Collectors; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springdoc.core.properties.SpringDocConfigProperties; @@ -119,18 +120,17 @@ public String resolve(String parameterProperty, Locale locale) { * @return The string with leading indentation removed from each line. */ public String trimIndent(String text) { + if (text == null) { + return null; + } + final String newLine = "\n"; + String[] lines = text.split("\\r?\\n"); + int minIndent = resolveMinIndent(lines); try { - if (text == null) { - return null; - } - final String newLine = "\n"; - String[] lines = text.split(newLine); - int minIndent = resolveMinIndent(lines); return Arrays.stream(lines) - .map(line -> line.substring(Math.min(line.length(), minIndent))) - .reduce((a, b) -> a + newLine + b) - .orElse(StringUtils.EMPTY); - } catch (Exception ex){ + .map(line -> line.substring(Math.min(line.length(), minIndent))) + .collect(Collectors.joining(newLine)); + } catch (Exception ex) { LOGGER.warn(ex.getMessage()); return text; } @@ -239,4 +239,4 @@ public Map resolveExtensions(Locale locale, Map else return extensions; } -} \ No newline at end of file +} From f72a7fb2496454102bea366b9c1855764dc26790 Mon Sep 17 00:00:00 2001 From: ryan-dia Date: Thu, 6 Feb 2025 23:40:45 +0900 Subject: [PATCH 2/2] Improve documentation for trimIndent: add details on null input and return values --- .../org/springdoc/core/utils/PropertyResolverUtils.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java index 6eff3e174..a98d7a5e3 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/PropertyResolverUtils.java @@ -115,9 +115,11 @@ public String resolve(String parameterProperty, Locale locale) { * Returns a string where all leading indentation has been removed from each line. * It detects the smallest common indentation of all the lines in the input string, * and removes it. + * If the input text is {@code null}, the method returns {@code null}. * - * @param text The original string with possible leading indentation. - * @return The string with leading indentation removed from each line. + * @param text The original string with possible leading indentation. + * @return The string with the smallest common leading indentation removed from each line, + * or {@code null} if the input text is {@code null}. */ public String trimIndent(String text) { if (text == null) {