Skip to content

Commit cd9c246

Browse files
committed
add convert cli
1 parent a5927dc commit cd9c246

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

cli/src/main/java/com/github/slamdev/openapispringgenerator/cli/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import picocli.CommandLine;
44

5-
@CommandLine.Command(subcommands = {Generate.class, Validate.class})
5+
@CommandLine.Command(subcommands = {Generate.class, Validate.class, Convert.class})
66
public class Application {
77

88
public static void main(String[] args) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.github.slamdev.openapispringgenerator.cli;
2+
3+
import com.github.slamdev.openapispringgenerator.lib.generator.OpenAPIYamlCodegen;
4+
import io.swagger.codegen.v3.ClientOptInput;
5+
import io.swagger.codegen.v3.DefaultGenerator;
6+
import io.swagger.codegen.v3.config.CodegenConfigurator;
7+
import io.swagger.codegen.v3.generators.openapi.OpenAPIGenerator;
8+
import picocli.CommandLine;
9+
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
14+
@CommandLine.Command(name = "convert", mixinStandardHelpOptions = true, description = "convert to OpenAPI v3")
15+
public class Convert implements Runnable {
16+
17+
@CommandLine.Option(names = {"-f", "--input-file"}, required = true, description = "path to a spec file")
18+
private Path specFile;
19+
20+
@CommandLine.Option(names = {"-o", "--output-file"}, required = true, description = "path to store the generated file")
21+
private Path outputFile;
22+
23+
@Override
24+
public void run() {
25+
Path abs = outputFile.toAbsolutePath();
26+
CodegenConfigurator configurator = new CodegenConfigurator();
27+
configurator.setLang(OpenAPIYamlCodegen.class.getName());
28+
configurator.setOutputDir(abs.getParent().toString());
29+
configurator.addAdditionalProperty(OpenAPIGenerator.OUTPUT_NAME, abs.getFileName().toString());
30+
configurator.addAdditionalProperty(OpenAPIGenerator.FLATTEN_SPEC, true);
31+
try {
32+
configurator.setInputSpec(new String(Files.readAllBytes(specFile)));
33+
} catch (IOException e) {
34+
throw new IllegalStateException(e);
35+
}
36+
ClientOptInput input = configurator.toClientOptInput();
37+
DefaultGenerator generator = new DefaultGenerator();
38+
generator.opts(input);
39+
generator.setGenerateSwaggerMetadata(false);
40+
generator.generate();
41+
}
42+
}

lib/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ dependencies {
1010
// override to support https://github.com/jknack/handlebars.java/pull/893/files
1111
runtimeOnly 'com.github.jknack:handlebars:4.3.0'
1212
// TODO upgrade to support inheritance
13-
api('io.swagger.codegen.v3:swagger-codegen-generators:1.0.29') {
13+
api('io.swagger.codegen.v3:swagger-codegen-generators:1.0.30') {
1414
exclude group: 'io.swagger', module: 'swagger-codegen'
1515
exclude module: 'guava'
1616
exclude module: 'logback-classic'
1717
exclude module: 'logback-core'
1818
}
19-
api('org.openapitools:openapi-generator:5.2.1') {
19+
api('org.openapitools:openapi-generator:5.3.0') {
2020
exclude module: 'guava'
2121
exclude module: 'slf4j-simple'
2222
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.slamdev.openapispringgenerator.lib.generator;
2+
3+
import io.swagger.codegen.v3.generators.openapi.OpenAPIYamlGenerator;
4+
5+
public class OpenAPIYamlCodegen extends OpenAPIYamlGenerator {
6+
7+
@Override
8+
public void processOpts() {
9+
super.processOpts();
10+
supportingFiles.clear();
11+
}
12+
}

0 commit comments

Comments
 (0)