From ec4bdbe1900837e53c000cd1abd6c8cb14a2171a Mon Sep 17 00:00:00 2001 From: nirbhay mishra Date: Sun, 20 Nov 2022 18:24:35 +0530 Subject: [PATCH 1/2] Handling Unicode in Project Generation stripAccents Fix for Bug : https://github.com/spring-io/initializr/issues/1328 --- .../initializr/web/project/ProjectRequest.java | 15 +++++++++++---- ...nerationControllerArchiveIntegrationTests.java | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java b/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java index a8a94dd35b..d63cc78b3b 100644 --- a/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java +++ b/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java @@ -66,7 +66,7 @@ public void setDependencies(List dependencies) { } public String getName() { - return this.name; + return cleanInputValue(this.name); } public void setName(String name) { @@ -90,7 +90,7 @@ public void setDescription(String description) { } public String getGroupId() { - return this.groupId; + return cleanInputValue(this.groupId); } public void setGroupId(String groupId) { @@ -98,7 +98,7 @@ public void setGroupId(String groupId) { } public String getArtifactId() { - return this.artifactId; + return cleanInputValue(this.artifactId); } public void setArtifactId(String artifactId) { @@ -147,7 +147,7 @@ public void setLanguage(String language) { public String getPackageName() { if (StringUtils.hasText(this.packageName)) { - return this.packageName; + return cleanInputValue(this.packageName); } if (StringUtils.hasText(this.groupId) && StringUtils.hasText(this.artifactId)) { return getGroupId() + "." + getArtifactId(); @@ -175,4 +175,11 @@ public void setBaseDir(String baseDir) { this.baseDir = baseDir; } + private String cleanInputValue(String inputString) { + if (StringUtils.hasText(inputString)) { + return org.apache.commons.lang3.StringUtils.stripAccents(inputString); + } + return inputString; + } + } diff --git a/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerArchiveIntegrationTests.java b/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerArchiveIntegrationTests.java index aee397621f..2d4be9612d 100644 --- a/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerArchiveIntegrationTests.java +++ b/initializr-web/src/test/java/io/spring/initializr/web/controller/ProjectGenerationControllerArchiveIntegrationTests.java @@ -36,4 +36,10 @@ void baseDirectorySeparatedBySpace() { assertDefaultProject(project.resolveModule("demo trial")); } + @Test + void projectNameWithAccentRemoved() { + ProjectStructure project = downloadZip("/starter.zip?artifactId=demo&baseDir=demo trial&name=Demö"); + assertDefaultProject(project.resolveModule("demo trial")); + } + } From e7ae9ea28aaf55915ba0dd825e56edd075562145 Mon Sep 17 00:00:00 2001 From: nirbhay mishra Date: Wed, 23 Nov 2022 22:47:59 +0530 Subject: [PATCH 2/2] Using DefaultProjectRequestDescriptionConverter in place of ProjectRequest --- ...ltProjectRequestToDescriptionConverter.java | 18 ++++++++++++++---- .../initializr/web/project/ProjectRequest.java | 15 ++++----------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java b/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java index 32056130f0..078239b282 100644 --- a/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java +++ b/initializr-web/src/main/java/io/spring/initializr/web/project/DefaultProjectRequestToDescriptionConverter.java @@ -16,6 +16,7 @@ package io.spring.initializr.web.project; +import java.text.Normalizer; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -34,6 +35,7 @@ import io.spring.initializr.metadata.support.MetadataBuildItemMapper; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * A default {@link ProjectRequestToDescriptionConverter} implementation that uses the @@ -44,6 +46,7 @@ * @author Madhura Bhave * @author HaiTao Zhang * @author Stephane Nicoll + * @author Nirbhay Mishra */ public class DefaultProjectRequestToDescriptionConverter implements ProjectRequestToDescriptionConverter { @@ -82,14 +85,14 @@ public void convert(ProjectRequest request, MutableProjectDescription descriptio validateDependencyRange(platformVersion, resolvedDependencies); description.setApplicationName(request.getApplicationName()); - description.setArtifactId(request.getArtifactId()); + description.setArtifactId(cleanInputValue(request.getArtifactId())); description.setBaseDirectory(request.getBaseDir()); description.setBuildSystem(getBuildSystem(request, metadata)); description.setDescription(request.getDescription()); - description.setGroupId(request.getGroupId()); + description.setGroupId(cleanInputValue(request.getGroupId())); description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion())); - description.setName(request.getName()); - description.setPackageName(request.getPackageName()); + description.setName(cleanInputValue(request.getName())); + description.setPackageName(cleanInputValue(request.getPackageName())); description.setPackaging(Packaging.forId(request.getPackaging())); description.setPlatformVersion(platformVersion); description.setVersion(request.getVersion()); @@ -188,4 +191,11 @@ private List getResolvedDependencies(ProjectRequest request, Version }).collect(Collectors.toList()); } + private String cleanInputValue(String inputString) { + if (StringUtils.hasText(inputString)) { + return Normalizer.normalize(inputString, Normalizer.Form.NFKD).replaceAll("\\p{M}", ""); + } + return inputString; + } + } diff --git a/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java b/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java index d63cc78b3b..a8a94dd35b 100644 --- a/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java +++ b/initializr-web/src/main/java/io/spring/initializr/web/project/ProjectRequest.java @@ -66,7 +66,7 @@ public void setDependencies(List dependencies) { } public String getName() { - return cleanInputValue(this.name); + return this.name; } public void setName(String name) { @@ -90,7 +90,7 @@ public void setDescription(String description) { } public String getGroupId() { - return cleanInputValue(this.groupId); + return this.groupId; } public void setGroupId(String groupId) { @@ -98,7 +98,7 @@ public void setGroupId(String groupId) { } public String getArtifactId() { - return cleanInputValue(this.artifactId); + return this.artifactId; } public void setArtifactId(String artifactId) { @@ -147,7 +147,7 @@ public void setLanguage(String language) { public String getPackageName() { if (StringUtils.hasText(this.packageName)) { - return cleanInputValue(this.packageName); + return this.packageName; } if (StringUtils.hasText(this.groupId) && StringUtils.hasText(this.artifactId)) { return getGroupId() + "." + getArtifactId(); @@ -175,11 +175,4 @@ public void setBaseDir(String baseDir) { this.baseDir = baseDir; } - private String cleanInputValue(String inputString) { - if (StringUtils.hasText(inputString)) { - return org.apache.commons.lang3.StringUtils.stripAccents(inputString); - } - return inputString; - } - }