Skip to content

Commit 11ccea7

Browse files
committed
Generating sources from an InputStream
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent a63e07e commit 11ccea7

File tree

14 files changed

+108
-35
lines changed

14 files changed

+108
-35
lines changed

deployment/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
<dependency>
101101
<groupId>io.quarkiverse.openapi.generator</groupId>
102102
<artifactId>quarkus-openapi-generator-test-utils</artifactId>
103-
<version>${project.version}</version>
104103
<scope>test</scope>
105104
</dependency>
106105

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkiverse.openapi.generator.deployment.codegen;
22

3+
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.getResolvedBasePackageProperty;
4+
35
import java.io.IOException;
46
import java.nio.file.Files;
57
import java.nio.file.Path;
@@ -11,8 +13,6 @@
1113
import io.quarkus.deployment.CodeGenContext;
1214
import io.quarkus.deployment.CodeGenProvider;
1315

14-
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.getResolvedBasePackageProperty;
15-
1616
/**
1717
* Code generation for OpenApi Client. Generates Java classes from OpenApi spec files located in src/main/openapi or
1818
* src/test/openapi
@@ -56,8 +56,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
5656
protected void generate(CodeGenContext context, final Path openApiFilePath, final Path outDir) {
5757
final OpenApiClientGeneratorWrapper generator = new OpenApiClientGeneratorWrapper(
5858
openApiFilePath.normalize(), outDir)
59-
.withCircuitBreakerConfiguration(CircuitBreakerConfigurationParser.parse(
60-
context.config()));
59+
.withCircuitBreakerConfiguration(CircuitBreakerConfigurationParser.parse(
60+
context.config()));
6161
context.config()
6262
.getOptionalValue(getResolvedBasePackageProperty(openApiFilePath), String.class)
6363
.ifPresent(generator::withBasePackage);

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020

21-
import io.quarkiverse.openapi.generator.OpenApiSpecInputProvider;
21+
import io.quarkiverse.openapi.generator.codegen.OpenApiSpecInputProvider;
22+
import io.quarkiverse.openapi.generator.codegen.SpecInputModel;
2223
import io.quarkus.bootstrap.prebuild.CodeGenException;
2324
import io.quarkus.deployment.CodeGenContext;
2425

@@ -60,18 +61,20 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
6061
boolean generated = false;
6162

6263
for (final OpenApiSpecInputProvider provider : this.providers) {
63-
for (final InputStream is : provider.read()) {
64+
for (SpecInputModel inputModel : provider.read()) {
65+
if (inputModel == null) {
66+
throw new CodeGenException("SpecInputModel from provider " + provider + " is null");
67+
}
6468
// TODO: in the future, we can use the checksum to not generate the stub files again
65-
final String checksum = generateChecksumCRC32(is);
66-
final Path openApiFilePath = Paths.get(outDir.toString(), checksum + ".yaml");
67-
68-
try (ReadableByteChannel channel = Channels.newChannel(is);
69+
final Path openApiFilePath = Paths.get(outDir.toString(), inputModel.getFileName());
70+
try (ReadableByteChannel channel = Channels.newChannel(inputModel.getInputStream());
6971
FileOutputStream output = new FileOutputStream(openApiFilePath.toString())) {
7072
output.getChannel().transferFrom(channel, 0, Integer.MAX_VALUE);
7173
this.generate(context, openApiFilePath, outDir);
7274
generated = true;
7375
} catch (IOException e) {
74-
throw new UncheckedIOException("Fail to save openapi file", e);
76+
throw new UncheckedIOException("Failed to save InputStream from provider " + provider + " into location ",
77+
e);
7578
}
7679
}
7780
}

integration-tests/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
<artifactId>microprofile-fault-tolerance-api</artifactId>
3737
</dependency>
3838

39+
<!-- exceptionally here, we are using the test utils with default scope because of the ClassPathOpenApiSpecInputProvider -->
3940
<dependency>
4041
<groupId>io.quarkiverse.openapi.generator</groupId>
4142
<artifactId>quarkus-openapi-generator-test-utils</artifactId>
42-
<version>${project.version}</version>
43-
<scope>test</scope>
43+
<scope>compile</scope>
4444
</dependency>
4545
<dependency>
4646
<groupId>io.quarkus</groupId>

integration-tests/src/main/java/io/quarkiverse/openapi/generator/it/providers/ClassPathOpenApiSpecInputProvider.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

integration-tests/src/main/resources/META-INF/services/io.quarkiverse.openapi.generator.OpenApiSpecInputProvider

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.quarkiverse.openapi.generator.testutils.codegen.ClassPathPetstoreOpenApiSpecInputProvider

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<artifactId>microprofile-fault-tolerance-api</artifactId>
4242
<version>${version.org.eclipse.microprofile.fault-tolerance}</version>
4343
</dependency>
44+
<dependency>
45+
<groupId>io.quarkiverse.openapi.generator</groupId>
46+
<artifactId>quarkus-openapi-generator</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
4449
<!-- Test -->
4550
<dependency>
4651
<groupId>com.github.javaparser</groupId>
@@ -54,6 +59,12 @@
5459
<version>${version.org.assertj}</version>
5560
<scope>test</scope>
5661
</dependency>
62+
<dependency>
63+
<groupId>io.quarkiverse.openapi.generator</groupId>
64+
<artifactId>quarkus-openapi-generator-test-utils</artifactId>
65+
<version>${project.version}</version>
66+
<scope>test</scope>
67+
</dependency>
5768
</dependencies>
5869
</dependencyManagement>
5970
<build>

runtime/src/main/java/io/quarkiverse/openapi/generator/OpenApiSpecInputProvider.java renamed to runtime/src/main/java/io/quarkiverse/openapi/generator/codegen/OpenApiSpecInputProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.quarkiverse.openapi.generator;
1+
package io.quarkiverse.openapi.generator.codegen;
22

33
import java.io.InputStream;
44
import java.util.List;
@@ -10,9 +10,9 @@ public interface OpenApiSpecInputProvider {
1010

1111
/**
1212
* Fetch OpenAPI specification files from a given source.
13-
*
13+
*
1414
* @return a list of spec files in {@link InputStream} format.
1515
*/
16-
List<InputStream> read();
16+
List<SpecInputModel> read();
1717

1818
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.quarkiverse.openapi.generator.codegen;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.io.InputStream;
6+
import java.util.Objects;
7+
8+
public class SpecInputModel {
9+
10+
private final InputStream inputStream;
11+
private final String filename;
12+
13+
public SpecInputModel(final String filename, final InputStream inputStream) {
14+
requireNonNull(inputStream, "InputStream can't be null");
15+
requireNonNull(filename, "File name can't be null");
16+
this.inputStream = inputStream;
17+
this.filename = filename;
18+
}
19+
20+
public String getFileName() {
21+
return filename;
22+
}
23+
24+
public InputStream getInputStream() {
25+
return inputStream;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "SpecInputModel{" +
31+
"name='" + filename + '\'' +
32+
'}';
33+
}
34+
35+
@Override
36+
public boolean equals(Object o) {
37+
if (this == o) {
38+
return true;
39+
}
40+
if (o == null || getClass() != o.getClass()) {
41+
return false;
42+
}
43+
SpecInputModel that = (SpecInputModel) o;
44+
return inputStream.equals(that.inputStream) && filename.equals(that.filename);
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return Objects.hash(inputStream, filename);
50+
}
51+
}

0 commit comments

Comments
 (0)