Skip to content

Commit 7cb5044

Browse files
Merge pull request #12 from quarkiverse/issue-7
Fix #7 - Correct handling paths on Windows machines
2 parents 9a40c9b + 9632433 commit 7cb5044

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-22
lines changed

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecConfig.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static io.quarkiverse.openapi.generator.deployment.OpenApiGeneratorConfiguration.CONFIG_PREFIX;
44

5-
import java.nio.file.Paths;
5+
import java.nio.file.Path;
66

77
import io.quarkus.runtime.annotations.ConfigGroup;
88
import io.quarkus.runtime.annotations.ConfigItem;
@@ -11,6 +11,7 @@
1111
public class SpecConfig {
1212
public static final String API_PKG_SUFFIX = ".api";
1313
public static final String MODEL_PKG_SUFFIX = ".model";
14+
static final String BASE_PACKAGE_PROP_FORMAT = CONFIG_PREFIX + ".spec.\"%s\".base-package";
1415

1516
/**
1617
* Defines the base package name for the generated classes.
@@ -26,8 +27,9 @@ public String getModelPackage() {
2627
return String.format("%s%s", basePackage, MODEL_PKG_SUFFIX);
2728
}
2829

29-
public static String getResolvedBasePackageProperty(final String openApiFilePath) {
30-
final String fileName = Paths.get(openApiFilePath).getFileName().toString();
31-
return CONFIG_PREFIX + ".spec.\"" + fileName + "\"" + ".base-package";
30+
public static String getResolvedBasePackageProperty(final Path openApiFilePath) {
31+
final String uriFilePath = openApiFilePath.toUri().toString();
32+
final String fileName = uriFilePath.substring(uriFilePath.lastIndexOf("/") + 1);
33+
return String.format(BASE_PACKAGE_PROP_FORMAT, fileName);
3234
}
3335
}

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
4242
.filter(Files::isRegularFile)
4343
.map(Path::toString)
4444
.filter(s -> s.endsWith(this.inputExtension()))
45-
.map(this::encodeURISpaces)
46-
.forEach(openApiFilePath -> {
45+
.map(Path::of).forEach(openApiFilePath -> {
4746
final String basePackage = getRequiredIndexedProperty(
4847
getResolvedBasePackageProperty(openApiFilePath), context);
4948
final OpenApiClientGeneratorWrapper generator = new OpenApiClientGeneratorWrapper(
50-
"file:" + openApiFilePath, outDir.toString())
49+
openApiFilePath.normalize(), outDir)
5150
.withApiPackage(basePackage + API_PKG_SUFFIX)
5251
.withModelPackage(basePackage + MODEL_PKG_SUFFIX);
5352
generator.generate();
@@ -61,13 +60,6 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
6160
return false;
6261
}
6362

64-
private String encodeURISpaces(String path) {
65-
// the underlying swagger parser library handles the path as a URI, and in previous versions (the one used by the openapi-generator)
66-
// it has a bug, not handling these paths correctly. That's why we manually encode the paths here, so the file can be accessible and validated against
67-
// these libraries
68-
return path.replaceAll(" ", "%20");
69-
}
70-
7163
private String getRequiredIndexedProperty(final String propertyKey, final CodeGenContext context) {
7264
// this is how we get a required property. The configSource will handle the exception for us.
7365
return context.config().getValue(propertyKey, String.class);

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static java.lang.Boolean.TRUE;
55

66
import java.io.File;
7+
import java.nio.file.Path;
78
import java.util.List;
89

910
import org.openapitools.codegen.CodegenConstants;
@@ -25,7 +26,7 @@ public class OpenApiClientGeneratorWrapper {
2526
private final QuarkusCodegenConfigurator configurator;
2627
private final DefaultGenerator generator;
2728

28-
public OpenApiClientGeneratorWrapper(final String specFilePath, final String outputDir) {
29+
public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputDir) {
2930
// do not generate docs nor tests
3031
GlobalSettings.setProperty(CodegenConstants.API_DOCS, FALSE.toString());
3132
GlobalSettings.setProperty(CodegenConstants.API_TESTS, FALSE.toString());
@@ -41,8 +42,8 @@ public OpenApiClientGeneratorWrapper(final String specFilePath, final String out
4142
GlobalSettings.setProperty(ONCE_LOGGER, TRUE.toString());
4243

4344
this.configurator = new QuarkusCodegenConfigurator();
44-
this.configurator.setInputSpec(specFilePath);
45-
this.configurator.setOutputDir(outputDir);
45+
this.configurator.setInputSpec(specFilePath.toString());
46+
this.configurator.setOutputDir(outputDir.toString());
4647
this.generator = new DefaultGenerator();
4748
}
4849

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.quarkiverse.openapi.generator.deployment;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.nio.file.Path;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class SpecConfigTest {
10+
11+
@Test
12+
void verifySpaceEncoding() {
13+
final String resolvedBasePackageProperty = SpecConfig
14+
.getResolvedBasePackageProperty(Path.of("/home/myuser/luke/my test openapi.json"));
15+
assertEquals(resolvedBasePackageProperty,
16+
String.format(SpecConfig.BASE_PACKAGE_PROP_FORMAT, "my%20test%20openapi.json"));
17+
}
18+
19+
@Test
20+
void withSingleFileName() {
21+
final String resolvedBasePackageProperty = SpecConfig.getResolvedBasePackageProperty(Path.of("my test openapi.json"));
22+
assertEquals(resolvedBasePackageProperty,
23+
String.format(SpecConfig.BASE_PACKAGE_PROP_FORMAT, "my%20test%20openapi.json"));
24+
}
25+
26+
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
package io.quarkiverse.openapi.generator.deployment.wrapper;
22

3+
import static java.util.Objects.requireNonNull;
34
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertNotNull;
56

67
import java.io.File;
78
import java.net.URISyntaxException;
9+
import java.nio.file.Path;
810
import java.nio.file.Paths;
911
import java.util.List;
10-
import java.util.Objects;
1112

1213
import org.junit.jupiter.api.Test;
1314

1415
public class OpenApiClientGeneratorWrapperTest {
1516

1617
@Test
1718
void generatePetStore() throws URISyntaxException {
18-
final String petstoreOpenApi = Objects.requireNonNull(this.getClass().getResource("/openapi/petstore-openapi.json"))
19-
.getPath();
20-
final String targetPath = Paths.get(Objects.requireNonNull(getClass().getResource("/")).toURI()).getParent().toString()
21-
+ "/openapi-gen";
19+
final Path petstoreOpenApi = Path
20+
.of(requireNonNull(this.getClass().getResource("/openapi/petstore-openapi.json")).toURI());
21+
final Path targetPath = Paths.get(getTargetDir(), "openapi-gen");
2222
final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(petstoreOpenApi, targetPath);
2323
final List<File> generatedFiles = generatorWrapper.generate();
2424
assertNotNull(generatedFiles);
2525
assertFalse(generatedFiles.isEmpty());
2626
}
27+
28+
private String getTargetDir() throws URISyntaxException {
29+
return Paths.get(requireNonNull(getClass().getResource("/")).toURI()).getParent().toString();
30+
}
2731
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
quarkus.openapi-generator.spec."petstore.json".base-package=org.acme.openapi
22
# since the file name has a space, we use the URI representation of this space here to not break the properties file
3+
# see the RFC3986 for more info https://datatracker.ietf.org/doc/html/rfc3986
34
quarkus.openapi-generator.spec."open%20weather.yaml".base-package=org.acme.openapi.weather

0 commit comments

Comments
 (0)