Skip to content

Commit

Permalink
add convert cli
Browse files Browse the repository at this point in the history
  • Loading branch information
slamdev committed Dec 8, 2021
1 parent a5927dc commit cd9c246
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import picocli.CommandLine;

@CommandLine.Command(subcommands = {Generate.class, Validate.class})
@CommandLine.Command(subcommands = {Generate.class, Validate.class, Convert.class})
public class Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.slamdev.openapispringgenerator.cli;

import com.github.slamdev.openapispringgenerator.lib.generator.OpenAPIYamlCodegen;
import io.swagger.codegen.v3.ClientOptInput;
import io.swagger.codegen.v3.DefaultGenerator;
import io.swagger.codegen.v3.config.CodegenConfigurator;
import io.swagger.codegen.v3.generators.openapi.OpenAPIGenerator;
import picocli.CommandLine;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@CommandLine.Command(name = "convert", mixinStandardHelpOptions = true, description = "convert to OpenAPI v3")
public class Convert implements Runnable {

@CommandLine.Option(names = {"-f", "--input-file"}, required = true, description = "path to a spec file")
private Path specFile;

@CommandLine.Option(names = {"-o", "--output-file"}, required = true, description = "path to store the generated file")
private Path outputFile;

@Override
public void run() {
Path abs = outputFile.toAbsolutePath();
CodegenConfigurator configurator = new CodegenConfigurator();
configurator.setLang(OpenAPIYamlCodegen.class.getName());
configurator.setOutputDir(abs.getParent().toString());
configurator.addAdditionalProperty(OpenAPIGenerator.OUTPUT_NAME, abs.getFileName().toString());
configurator.addAdditionalProperty(OpenAPIGenerator.FLATTEN_SPEC, true);
try {
configurator.setInputSpec(new String(Files.readAllBytes(specFile)));
} catch (IOException e) {
throw new IllegalStateException(e);
}
ClientOptInput input = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
generator.opts(input);
generator.setGenerateSwaggerMetadata(false);
generator.generate();
}
}
4 changes: 2 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ dependencies {
// override to support https://github.com/jknack/handlebars.java/pull/893/files
runtimeOnly 'com.github.jknack:handlebars:4.3.0'
// TODO upgrade to support inheritance
api('io.swagger.codegen.v3:swagger-codegen-generators:1.0.29') {
api('io.swagger.codegen.v3:swagger-codegen-generators:1.0.30') {
exclude group: 'io.swagger', module: 'swagger-codegen'
exclude module: 'guava'
exclude module: 'logback-classic'
exclude module: 'logback-core'
}
api('org.openapitools:openapi-generator:5.2.1') {
api('org.openapitools:openapi-generator:5.3.0') {
exclude module: 'guava'
exclude module: 'slf4j-simple'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.slamdev.openapispringgenerator.lib.generator;

import io.swagger.codegen.v3.generators.openapi.OpenAPIYamlGenerator;

public class OpenAPIYamlCodegen extends OpenAPIYamlGenerator {

@Override
public void processOpts() {
super.processOpts();
supportingFiles.clear();
}
}

0 comments on commit cd9c246

Please sign in to comment.