org.openapitools.codegen.DefaultGenerator Java Examples
The following examples show how to use
org.openapitools.codegen.DefaultGenerator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YamlGeneratorTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void testGeneratePingOtherOutputFile() throws Exception { Map<String, Object> properties = new HashMap<>(); properties.put(OpenAPIYamlGenerator.OUTPUT_NAME, "ping.yaml"); File output = Files.createTempDirectory("test").toFile(); final CodegenConfigurator configurator = new CodegenConfigurator() .setGeneratorName("openapi-yaml") .setAdditionalProperties(properties) .setInputSpec("src/test/resources/3_0/ping.yaml") .setOutputDir(output.getAbsolutePath().replace("\\", "/")); final ClientOptInput clientOptInput = configurator.toClientOptInput(); DefaultGenerator generator = new DefaultGenerator(); List<File> files = generator.opts(clientOptInput).generate(); Assert.assertEquals(files.size(), 5); TestUtils.ensureContainsFile(files, output, "ping.yaml"); TestUtils.ensureContainsFile(files, output, "README.md"); TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore"); TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES"); TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION"); output.deleteOnExit(); }
Example #2
Source File: APIClientGenerationManager.java From carbon-apimgt with Apache License 2.0 | 6 votes |
/** * This method is used to generate SDK for a API for a given language * * @param apiName name of the API * @param specLocation location of the swagger spec for the API * @param sdkLanguage preferred SDK language * @param temporaryOutputPath temporary location where the SDK archive is saved until downloaded */ private void generateClient(String apiName, String specLocation, String sdkLanguage, String temporaryOutputPath) { APIManagerConfiguration config = getAPIManagerConfiguration(); CodegenConfigurator codegenConfigurator = new CodegenConfigurator(); codegenConfigurator.setGroupId(config.getFirstProperty(APIConstants.CLIENT_CODEGEN_GROUPID)); codegenConfigurator.setArtifactId(config.getFirstProperty(APIConstants.CLIENT_CODEGEN_ARTIFACTID) + apiName); codegenConfigurator .setModelPackage(config.getFirstProperty(APIConstants.CLIENT_CODEGEN_MODAL_PACKAGE) + apiName); codegenConfigurator.setApiPackage(config.getFirstProperty(APIConstants.CLIENT_CODEGEN_API_PACKAGE) + apiName); codegenConfigurator.setInputSpec(specLocation); codegenConfigurator.setGeneratorName(sdkLanguage); codegenConfigurator.setOutputDir(temporaryOutputPath); final ClientOptInput clientOptInput = codegenConfigurator.toClientOptInput(); new DefaultGenerator().opts(clientOptInput).generate(); }
Example #3
Source File: AsciidocSampleGeneratorTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@BeforeClass public void beforeClassGenerateTestMarkup() throws Exception { File outputTempDirectory = Files.createTempDirectory("test-asciidoc-sample-generator.").toFile(); Assert.assertTrue(specDir.exists(), "test cancel, not specDir found to use." + specDir.getPath()); Assert.assertTrue(snippetDir.exists(), "test cancel, not snippedDir found to use." + snippetDir.getPath()); final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("asciidoc") .setInputSpec("src/test/resources/3_0/asciidoc/api-docs.json") .setOutputDir(outputTempDirectory.getAbsolutePath()) .addAdditionalProperty(AsciidocDocumentationCodegen.SPEC_DIR, specDir.toString()) .addAdditionalProperty(AsciidocDocumentationCodegen.SNIPPET_DIR, snippetDir.toString()); DefaultGenerator generator = new DefaultGenerator(); List<File> files = generator.opts(configurator.toClientOptInput()).generate(); for (File file : files) { if (file.getName().equals("index.adoc")) { this.markupFileName = file.getAbsoluteFile().toString(); this.markupContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8); } } }
Example #4
Source File: AsciidocGeneratorTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void testGenerateIndexAsciidocMarkupFileWithAsciidocGenerator() throws Exception { File output = Files.createTempDirectory("test").toFile(); final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("asciidoc") .setInputSpec("src/test/resources/3_0/ping.yaml").setOutputDir(output.getAbsolutePath()) .addAdditionalProperty(AsciidocDocumentationCodegen.SNIPPET_DIR, "MY-SNIPPET-DIR") .addAdditionalProperty(AsciidocDocumentationCodegen.SPEC_DIR, "MY-SPEC-DIR"); final ClientOptInput clientOptInput = configurator.toClientOptInput(); DefaultGenerator generator = new DefaultGenerator(); generator.setGenerateMetadata(false); List<File> generatedFiles = generator.opts(clientOptInput).generate(); TestUtils.ensureContainsFile(generatedFiles, output, "index.adoc"); }
Example #5
Source File: HaskellServantCodegenTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void testGenerateRootEndpoint() throws IOException { // given File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); String outputPath = output.getAbsolutePath().replace('\\', '/'); final HaskellServantCodegen codegen = new HaskellServantCodegen(); codegen.setOutputDir(output.getAbsolutePath()); OpenAPI openAPI = new OpenAPIParser() .readLocation("src/test/resources/3_0/rootOperation.yaml", null, new ParseOptions()) .getOpenAPI(); ClientOptInput input = new ClientOptInput(); input.setOpenAPI(openAPI); input.setConfig(codegen); // when DefaultGenerator generator = new DefaultGenerator(); generator.setGenerateMetadata(false); generator.opts(input).generate(); // then TestUtils.assertFileNotContains(Paths.get(outputPath + "/lib/RootOperation/API.hs"), "\"\" :>"); }
Example #6
Source File: JavaJaxrsBaseTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void addDefaultValueDocumentationForNonContainers() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); String outputPath = output.getAbsolutePath().replace('\\', '/'); OpenAPI openAPI = new OpenAPIParser() .readLocation("src/test/resources/3_0/arrayParameter.yaml", null, new ParseOptions()).getOpenAPI(); openAPI.getComponents().getParameters().get("operationsQueryParam").setSchema(new StringSchema()._default("default")); codegen.setOutputDir(output.getAbsolutePath()); codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) .config(codegen); DefaultGenerator generator = new DefaultGenerator(); generator.opts(input).generate(); assertFileContains(Paths.get(outputPath + "/src/gen/java/org/openapitools/api/ExamplesApi.java"), "DefaultValue"); }
Example #7
Source File: JavaJaxrsBaseTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void doNotAddDefaultValueDocumentationForContainers() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); String outputPath = output.getAbsolutePath().replace('\\', '/'); OpenAPI openAPI = new OpenAPIParser() .readLocation("src/test/resources/3_0/arrayParameter.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) .config(codegen); DefaultGenerator generator = new DefaultGenerator(); generator.opts(input).generate(); assertFileNotContains(Paths.get(outputPath + "/src/gen/java/org/openapitools/api/ExamplesApi.java"), "DefaultValue"); }
Example #8
Source File: YamlGeneratorTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void testGeneratePing() throws Exception { Map<String, Object> properties = new HashMap<>(); File output = Files.createTempDirectory("test").toFile(); final CodegenConfigurator configurator = new CodegenConfigurator() .setGeneratorName("openapi-yaml") .setAdditionalProperties(properties) .setInputSpec("src/test/resources/3_0/ping.yaml") .setOutputDir(output.getAbsolutePath().replace("\\", "/")); final ClientOptInput clientOptInput = configurator.toClientOptInput(); DefaultGenerator generator = new DefaultGenerator(); List<File> files = generator.opts(clientOptInput).generate(); Assert.assertEquals(files.size(), 5); TestUtils.ensureContainsFile(files, output, "openapi/openapi.yaml"); TestUtils.ensureContainsFile(files, output, "README.md"); TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore"); TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES"); TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION"); output.deleteOnExit(); }
Example #9
Source File: JavaJaxrsBaseTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void doNotGenerateJsonAnnotationForPolymorphismIfJsonExclude() throws IOException { codegen.additionalProperties().put("jackson", false); File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); String outputPath = output.getAbsolutePath().replace('\\', '/'); OpenAPI openAPI = new OpenAPIParser() .readLocation("src/test/resources/3_0/generic.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) .config(codegen); DefaultGenerator generator = new DefaultGenerator(); generator.opts(input).generate(); String jsonTypeInfo = "@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = \"className\", visible = true)"; String jsonSubType = "@JsonSubTypes({\n" + " @JsonSubTypes.Type(value = Dog.class, name = \"Dog\"),\n" + " @JsonSubTypes.Type(value = Cat.class, name = \"Cat\"),\n" + "})"; assertFileNotContains(Paths.get(outputPath + "/src/gen/java/org/openapitools/model/Animal.java"), jsonTypeInfo, jsonSubType); }
Example #10
Source File: AsciidocGeneratorTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void testGenerateIndexAsciidocMarkupContent() throws Exception { final File output = Files.createTempDirectory("test").toFile(); output.mkdirs(); output.deleteOnExit(); final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/ping.yaml"); CodegenConfig codegenConfig = new AsciidocDocumentationCodegen(); codegenConfig.setOutputDir(output.getAbsolutePath()); ClientOptInput clientOptInput = new ClientOptInput().openAPI(openAPI).config(codegenConfig); DefaultGenerator generator = new DefaultGenerator(); List<File> files = generator.opts(clientOptInput).generate(); boolean markupFileGenerated = false; for (File file : files) { if (file.getName().equals("index.adoc")) { markupFileGenerated = true; String markupContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // check on some basic asciidoc markup content Assert.assertTrue(markupContent.contains("= ping test"), "expected = header in: " + markupContent.substring(0, 50)); Assert.assertTrue(markupContent.contains(":toc: "), "expected = :toc: " + markupContent.substring(0, 50)); } } Assert.assertTrue(markupFileGenerated, "Default api file is not generated!"); }
Example #11
Source File: AsciidocGeneratorTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void testAdditionalDirectoriesGeneratedIntoHeaderAttributes() throws Exception { File output = Files.createTempDirectory("test").toFile(); LOGGER.info("test: generating sample markup " + output.getAbsolutePath()); Map<String, Object> props = new TreeMap<String, Object>(); props.put("specDir", "spec"); final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("asciidoc") .setInputSpec("src/test/resources/3_0/ping.yaml").setOutputDir(output.getAbsolutePath()) .addAdditionalProperty(AsciidocDocumentationCodegen.SPEC_DIR, "SPEC-DIR") .addAdditionalProperty(AsciidocDocumentationCodegen.SNIPPET_DIR, "MY/SNIPPET/DIR"); DefaultGenerator generator = new DefaultGenerator(); List<File> files = generator.opts(configurator.toClientOptInput()).generate(); boolean markupFileGenerated = false; for (File file : files) { if (file.getName().equals("index.adoc")) { markupFileGenerated = true; String markupContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8); Assert.assertTrue(markupContent.contains(":specDir: SPEC-DIR"), "expected :specDir: in: " + markupContent.substring(0, 250)); Assert.assertTrue(markupContent.contains(":snippetDir: MY/SNIPPET/DIR"), "expected :snippetDir: in: " + markupContent.substring(0, 250)); } } Assert.assertTrue(markupFileGenerated, "index.adoc is not generated!"); }
Example #12
Source File: AsciidocGeneratorTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void testHeaderAttributesFlagRemovesAttributesFromMarkupHeaderSection() throws Exception { File output = Files.createTempDirectory("test").toFile(); LOGGER.info("test: generating sample markup " + output.getAbsolutePath()); Map<String, Object> props = new TreeMap<String, Object>(); props.put("specDir", "spec"); final CodegenConfigurator configurator = new CodegenConfigurator() .setGeneratorName("asciidoc") .setInputSpec("src/test/resources/3_0/ping.yaml") .setOutputDir(output.getAbsolutePath()) .addAdditionalProperty(AsciidocDocumentationCodegen.HEADER_ATTRIBUTES_FLAG, "false") // option avoids generation of attributes .addAdditionalProperty(AsciidocDocumentationCodegen.SPEC_DIR, "SPEC-DIR") .addAdditionalProperty(AsciidocDocumentationCodegen.SNIPPET_DIR, "MY/SNIPPET/DIR"); DefaultGenerator generator = new DefaultGenerator(); boolean markupFileGenerated = false; List<File> files = generator.opts(configurator.toClientOptInput()).generate(); for (File file : files) { if (file.getName().equals("index.adoc")) { markupFileGenerated = true; String markupContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8); Assert.assertFalse(markupContent.contains(":specDir: SPEC-DIR"), "not expected :specDir: in: " + markupContent.substring(0, 250)); Assert.assertFalse(markupContent.contains(":snippetDir: MY/SNIPPET/DIR"), "not expected :snippetDir: in: " + markupContent.substring(0, 250)); Assert.assertFalse(markupContent.contains(":toc:"), "not expected :toc: in: " + markupContent.substring(0, 250)); // typical attributes not found in markup. } } Assert.assertTrue(markupFileGenerated, "index.adoc is not generated!"); }
Example #13
Source File: JavaJaxrsBaseTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void generateJsonAnnotationForPolymorphism() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); String outputPath = output.getAbsolutePath().replace('\\', '/'); OpenAPI openAPI = new OpenAPIParser() .readLocation("src/test/resources/3_0/generic.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) .config(codegen); DefaultGenerator generator = new DefaultGenerator(); generator.opts(input).generate(); String jsonTypeInfo = "@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = \"className\", visible = true)"; String jsonSubType = "@JsonSubTypes({\n" + " @JsonSubTypes.Type(value = Dog.class, name = \"Dog\"),\n" + " @JsonSubTypes.Type(value = Cat.class, name = \"Cat\"),\n" + " @JsonSubTypes.Type(value = BigDog.class, name = \"BigDog\"),\n" + "})"; assertFileContains(Paths.get(outputPath + "/src/gen/java/org/openapitools/model/Animal.java"), jsonTypeInfo, jsonSubType); }
Example #14
Source File: KotlinModelCodegenTest.java From openapi-generator with Apache License 2.0 | 5 votes |
private void checkModel(AbstractKotlinCodegen codegen, boolean mutable, String... props) throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); String outputPath = output.getAbsolutePath().replace('\\', '/'); OpenAPI openAPI = new OpenAPIParser() .readLocation("src/test/resources/3_0/generic.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "models"); codegen.additionalProperties().put(AbstractKotlinCodegen.MODEL_MUTABLE, mutable); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) .config(codegen); DefaultGenerator generator = new DefaultGenerator(); generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true"); generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false"); generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false"); generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false"); generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true"); generator.opts(input).generate(); assertFileContains(Paths.get(outputPath + "/src/main/kotlin/models/Animal.kt"), props); }
Example #15
Source File: GenerateBatch.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ @Override public void run() { String name = ""; try { GlobalSettings.reset(); ClientOptInput opts = configurator.toClientOptInput(); CodegenConfig config = opts.getConfig(); name = config.getName(); Path target = Paths.get(config.getOutputDir()); Path updated = rootDir.resolve(target); config.setOutputDir(updated.toString()); System.out.printf(Locale.ROOT, "[%s] Generating %s (outputs to %s)…%n", Thread.currentThread().getName(), name, updated.toString()); DefaultGenerator defaultGenerator = new DefaultGenerator(); defaultGenerator.opts(opts); defaultGenerator.generate(); System.out.printf(Locale.ROOT, "[%s] Finished generating %s…%n", Thread.currentThread().getName(), name); successes.incrementAndGet(); } catch (Throwable e) { failures.incrementAndGet(); String failedOn = name; if (StringUtils.isEmpty(failedOn)) { failedOn = "unspecified"; } System.err.printf(Locale.ROOT, "[%s] Generation failed for %s: (%s) %s%n", Thread.currentThread().getName(), failedOn, e.getClass().getSimpleName(), e.getMessage()); e.printStackTrace(System.err); if (exitOnError) { System.exit(1); } } finally { GlobalSettings.reset(); } }
Example #16
Source File: GenerateTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@BeforeMethod public void beforeEachTest() throws IOException { outputDirectory = Files.createTempDirectory("GenerateTest"); generator = mock(DefaultGenerator.class); when(generator.generate()).thenReturn(new ArrayList<>()); configurator = mock(CodegenConfigurator.class, mockSettings); }
Example #17
Source File: MyclientcodegenGeneratorTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void launchCodeGenerator() { // to understand how the 'openapi-generator-cli' module is using 'CodegenConfigurator', have a look at the 'Generate' class: // https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java final CodegenConfigurator configurator = new CodegenConfigurator() .setGeneratorName("myClientCodegen") // use this codegen library .setInputSpec("../../../modules/openapi-generator/src/test/resources/2_0/petstore.yaml") // sample OpenAPI file // .setInputSpec("https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml") // or from the server .setOutputDir("out/myClientCodegen"); // output directory final ClientOptInput clientOptInput = configurator.toClientOptInput(); DefaultGenerator generator = new DefaultGenerator(); generator.opts(clientOptInput).generate(); }
Example #18
Source File: ApiGenerator.java From teiid-spring-boot with Apache License 2.0 | 5 votes |
protected void generate(MustacheFactory mf, File javaSrcDir, Database database, HashMap<String, String> parentMap) throws Exception { log.info("Starting to Generate the Java classes for OpenAPI document: " + this.openApiFile.getCanonicalPath()); String packageName = parentMap.get("packageName"); String outputDir = this.outputDirectory.getAbsolutePath(); CodegenConfigurator configurator = new CodegenConfigurator(); configurator.setPackageName(packageName); configurator.setApiPackage(packageName); configurator.addDynamicProperty("configPackage", packageName); configurator.addDynamicProperty("basePackage", packageName); configurator.setModelPackage(packageName); //configurator.addSystemProperty("models", ""); //configurator.addSystemProperty("modelDocs", "false"); //configurator.addSystemProperty("modelTests", "false"); configurator.setInputSpec(this.openApiFile.getAbsolutePath()); configurator.setGeneratorName("org.teiid.maven.TeiidCodegen"); configurator.setOutputDir(outputDir); configurator.setLibrary("spring-boot"); configurator.addDynamicProperty("delegatePattern", "true"); configurator.setIgnoreFileOverride(null); final ClientOptInput input = configurator.toClientOptInput(); new DefaultGenerator().opts(input).generate(); log.info("Generated the Java classes for OpenAPI document: " + this.openApiFile.getCanonicalPath()); }