org.openapitools.codegen.ClientOptInput Java Examples

The following examples show how to use org.openapitools.codegen.ClientOptInput. 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: JavaJaxrsBaseTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: APIClientGenerationManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * 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: AsciidocGeneratorTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: HaskellServantCodegenTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: JavaJaxrsBaseTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: YamlGeneratorTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: YamlGeneratorTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@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 #8
Source File: JavaJaxrsBaseTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: JavaJaxrsBaseTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@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 vote down vote up
@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: KotlinModelCodegenTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: GenerateBatch.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #13
Source File: MyclientcodegenGeneratorTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@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 #14
Source File: ApiGenerator.java    From teiid-spring-boot with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #15
Source File: MultiContractGenerator.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
@Override
public List<File> generate() {

  if (optsList == null || optsList.size() == 0) {
    return null;
  }

  List<File> fileList = new ArrayList<>();
  List<Map<String, Object>> modules = new ArrayList<>();

  Set<Object> moduleSet = new HashSet<>();
  for (ClientOptInput opts : optsList) {
    moduleSet.add(opts.getConfig().additionalProperties().get(GeneratorExternalConfigConstant.PROVIDER_PROJECT_NAME));
    moduleSet.add(opts.getConfig().additionalProperties().get(GeneratorExternalConfigConstant.CONSUMER_PROJECT_NAME));
    moduleSet.add(opts.getConfig().additionalProperties().get(GeneratorExternalConfigConstant.MODEL_PROJECT_NAME));
    this.opts(opts);
    fileList.addAll(super.generate());
  }

  moduleSet.forEach(module -> {
    modules.add(Collections.singletonMap("module", module));
  });

  if (ServiceType.ALL.getValue().equals(
      Optional.ofNullable(opts.getConfig().additionalProperties().get(ProjectMetaConstant.SERVICE_TYPE))
          .orElse(ServiceType.ALL.getValue()))) {
    generateParentProject(fileList, modules);
  }

  return fileList;
}
 
Example #16
Source File: JavaJerseyServerCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddOperationToGroupUseTagsTrue() throws Exception {
    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/tags.yaml");
    ((JavaJerseyServerCodegen) codegen).setUseTags(true);
    codegen.setOutputDir(output.getAbsolutePath());

    ClientOptInput input = new ClientOptInput();
    input.setOpenAPI(openAPI);
    input.setConfig(codegen);

    MockDefaultGenerator generator = new MockDefaultGenerator();
    generator.opts(input).generate();

    WrittenTemplateBasedFile tag1File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Tag1Api.java");
    Assert.assertEquals(tag1File.getTemplateData().get("baseName"), "Tag1");
    Assert.assertEquals(tag1File.getTemplateData().get("commonPath"), null);
    List<CodegenOperation> tag1List = getOperationsList(tag1File.getTemplateData());
    Assert.assertEquals(tag1List.size(), 1);
    Assert.assertEquals(tag1List.get(0).path, "/group1/op1");
    Assert.assertEquals(tag1List.get(0).baseName, null);
    Assert.assertEquals(tag1List.get(0).subresourceOperation, true);

    WrittenTemplateBasedFile tag2File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Tag2Api.java");
    Assert.assertEquals(tag2File.getTemplateData().get("baseName"), "Tag2");
    Assert.assertEquals(tag2File.getTemplateData().get("commonPath"), null);
    List<CodegenOperation> tag2List = getOperationsList(tag2File.getTemplateData());
    Assert.assertEquals(tag2List.size(), 2);
    Assert.assertEquals(tag2List.get(0).path, "/group1/op2");
    Assert.assertEquals(tag2List.get(0).baseName, null);
    Assert.assertEquals(tag2List.get(0).subresourceOperation, true);
    Assert.assertEquals(tag2List.get(1).path, "/group2/op3");
    Assert.assertEquals(tag2List.get(1).baseName, null);
    Assert.assertEquals(tag2List.get(1).subresourceOperation, true);

    WrittenTemplateBasedFile defaultFile = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/DefaultApi.java");
    Assert.assertEquals(defaultFile.getTemplateData().get("baseName"), "Default");
    Assert.assertEquals(defaultFile.getTemplateData().get("commonPath"), null);
    List<CodegenOperation> defaultList = getOperationsList(defaultFile.getTemplateData());
    Assert.assertEquals(defaultList.size(), 1);
    Assert.assertEquals(defaultList.get(0).path, "/group3/op4");
    Assert.assertEquals(defaultList.get(0).baseName, null);
    Assert.assertEquals(defaultList.get(0).subresourceOperation, true);

    WrittenTemplateBasedFile group4File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group4Api.java");
    Assert.assertEquals(group4File.getTemplateData().get("baseName"), "Group4");
    Assert.assertEquals(group4File.getTemplateData().get("commonPath"), "group4");
    List<CodegenOperation> group4List = getOperationsList(group4File.getTemplateData());
    Assert.assertEquals(group4List.size(), 2);
    Assert.assertEquals(group4List.get(0).path, "/op5");
    Assert.assertEquals(group4List.get(0).baseName, "group4");
    Assert.assertEquals(group4List.get(0).subresourceOperation, true);
    Assert.assertEquals(group4List.get(1).path, "/op6");
    Assert.assertEquals(group4List.get(1).baseName, "group4");
    Assert.assertEquals(group4List.get(1).subresourceOperation, true);

    WrittenTemplateBasedFile group5File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group5Api.java");
    Assert.assertEquals(group5File.getTemplateData().get("baseName"), "Group5");
    Assert.assertEquals(group5File.getTemplateData().get("commonPath"), null);
    List<CodegenOperation> group5List = getOperationsList(group5File.getTemplateData());
    Assert.assertEquals(group5List.size(), 2);
    Assert.assertEquals(group5List.get(0).path, "/group5/op7");
    Assert.assertEquals(group5List.get(0).baseName, null);
    Assert.assertEquals(group5List.get(0).subresourceOperation, true);
    Assert.assertEquals(group5List.get(1).path, "/group6/op8");
    Assert.assertEquals(group5List.get(1).baseName, null);
    Assert.assertEquals(group5List.get(1).subresourceOperation, true);
}
 
Example #17
Source File: JavaJerseyServerCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddOperationToGroupUseTagsFalse() throws Exception {
    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/tags.yaml");
    ((JavaJerseyServerCodegen) codegen).setUseTags(false);
    codegen.setOutputDir(output.getAbsolutePath());

    ClientOptInput input = new ClientOptInput();
    input.setOpenAPI(openAPI);
    input.setConfig(codegen);

    MockDefaultGenerator generator = new MockDefaultGenerator();
    generator.opts(input).generate();

    WrittenTemplateBasedFile group1File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group1Api.java");
    Assert.assertEquals(group1File.getTemplateData().get("baseName"), "group1");
    Assert.assertEquals(group1File.getTemplateData().get("commonPath"), "group1");
    List<CodegenOperation> group1 = getOperationsList(group1File.getTemplateData());
    Assert.assertEquals(group1.size(), 2);
    Assert.assertEquals(group1.get(0).path, "/op1");
    Assert.assertEquals(group1.get(0).baseName, "group1");
    Assert.assertEquals(group1.get(0).subresourceOperation, true);
    Assert.assertEquals(group1.get(1).path, "/op2");
    Assert.assertEquals(group1.get(1).baseName, "group1");
    Assert.assertEquals(group1.get(1).subresourceOperation, true);

    WrittenTemplateBasedFile group2File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group2Api.java");
    Assert.assertEquals(group2File.getTemplateData().get("baseName"), "group2");
    Assert.assertEquals(group2File.getTemplateData().get("commonPath"), "group2");
    List<CodegenOperation> group2 = getOperationsList(group2File.getTemplateData());
    Assert.assertEquals(group2.size(), 1);
    Assert.assertEquals(group2.get(0).path, "/op3");
    Assert.assertEquals(group2.get(0).baseName, "group2");
    Assert.assertEquals(group2.get(0).subresourceOperation, true);

    WrittenTemplateBasedFile group3File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group3Api.java");
    Assert.assertEquals(group3File.getTemplateData().get("baseName"), "group3");
    Assert.assertEquals(group3File.getTemplateData().get("commonPath"), "group3");
    List<CodegenOperation> group3 = getOperationsList(group3File.getTemplateData());
    Assert.assertEquals(group3.size(), 1);
    Assert.assertEquals(group3.get(0).path, "/op4");
    Assert.assertEquals(group3.get(0).baseName, "group3");
    Assert.assertEquals(group3.get(0).subresourceOperation, true);

    WrittenTemplateBasedFile group4File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group4Api.java");
    Assert.assertEquals(group4File.getTemplateData().get("baseName"), "group4");
    Assert.assertEquals(group4File.getTemplateData().get("commonPath"), "group4");
    List<CodegenOperation> group4 = getOperationsList(group4File.getTemplateData());
    Assert.assertEquals(group4.size(), 2);
    Assert.assertEquals(group4.get(0).path, "/op5");
    Assert.assertEquals(group4.get(0).baseName, "group4");
    Assert.assertEquals(group4.get(0).subresourceOperation, true);
    Assert.assertEquals(group4.get(1).path, "/op6");
    Assert.assertEquals(group4.get(1).baseName, "group4");
    Assert.assertEquals(group4.get(1).subresourceOperation, true);

    WrittenTemplateBasedFile group5File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group5Api.java");
    Assert.assertEquals(group5File.getTemplateData().get("baseName"), "group5");
    Assert.assertEquals(group5File.getTemplateData().get("commonPath"), "group5");
    List<CodegenOperation> group5 = getOperationsList(group5File.getTemplateData());
    Assert.assertEquals(group5.size(), 1);
    Assert.assertEquals(group5.get(0).path, "/op7");
    Assert.assertEquals(group5.get(0).baseName, "group5");
    Assert.assertEquals(group5.get(0).subresourceOperation, true);

    WrittenTemplateBasedFile group6File = TestUtils.getTemplateBasedFile(generator, output, "src/gen/java/org/openapitools/api/Group6Api.java");
    Assert.assertEquals(group6File.getTemplateData().get("baseName"), "group6");
    Assert.assertEquals(group6File.getTemplateData().get("commonPath"), "group6");
    List<CodegenOperation> group6 = getOperationsList(group6File.getTemplateData());
    Assert.assertEquals(group6.size(), 1);
    Assert.assertEquals(group6.get(0).path, "/op8");
    Assert.assertEquals(group6.get(0).baseName, "group6");
    Assert.assertEquals(group6.get(0).subresourceOperation, true);
}
 
Example #18
Source File: MultiContractGenerator.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
public Generator addOpts(ClientOptInput opts) {
  optsList.add(opts);
  return this;
}
 
Example #19
Source File: CodegenConfiguratorTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSetConfiglProperties() throws IOException {
    // This tests that properties we set on CodegenConfigurator make it down into generator properties,
    // limiting to those managed in DefaultCodegen.
    Map<String, Object> properties = new HashMap<String, Object>() {{
       put("foo", "bar");
       put("baz", "quux");
       put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, true);
       put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, true);
       put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, false);
       put(CodegenConstants.ENSURE_UNIQUE_PARAMS, true);
       put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, true);
       put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, false);
       put(CodegenConstants.DOCEXTENSION, "D");
       put(CodegenConstants.ENABLE_POST_PROCESS_FILE, false);
       put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
    }};

    File output = Files.createTempDirectory("test").toFile();
    File template = Files.createTempDirectory("test").toFile();
    String outDir = Paths.get(output.toURI()).toAbsolutePath().toString();
    String templateDir = Paths.get(template.toURI()).toAbsolutePath().toString();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("java")
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .addImportMapping("one", "two")
            .addInstantiationType("three", "four")
            .addLanguageSpecificPrimitive("five")
            .addGlobalProperty("six", "seven")
            .addTypeMapping("eight", "nine")
            .setApiPackage("test-api")
            .setArtifactId("test-artifactId")
            .setArtifactVersion("test-artifactVersion")
            .setAuth("test-auth")
            .setGitRepoId("git")
            .setGitUserId("user")
            .setGitHost("test.com")
            .setGroupId("group")
            .setHttpUserAgent("agent")
            .setApiNameSuffix("api-suffix")
            .setModelNamePrefix("model-prefix")
            .setModelNameSuffix("model-suffix")
            .setModelPackage("model-package")
            .setPackageName("package-name")
            .setReleaseNote("release-note")
            .setTemplateDir(templateDir)
            .setOutputDir(outDir);

    final ClientOptInput clientOptInput = configurator.toClientOptInput();

    CodegenConfig config = clientOptInput.getConfig();
    config.processOpts();

    Map<String, Object> props = config.additionalProperties();

    // This verifies that things we expect to make it into the template will, as a result of this CodegenConfigurator.
    want(props, CodegenConstants.MODEL_PACKAGE, "model_package"); // * mutated by codegen
    want(props, CodegenConstants.API_PACKAGE, "test_api"); // * mutated by codegen
    want(props, CodegenConstants.HIDE_GENERATION_TIMESTAMP, true);
    want(props, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, true);
    want(props, CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, false);
    want(props, CodegenConstants.ENSURE_UNIQUE_PARAMS, true);
    want(props, CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, true);
    want(props, CodegenConstants.API_NAME_SUFFIX, "api-suffix");
    want(props, CodegenConstants.MODEL_NAME_PREFIX, "model-prefix");
    want(props, CodegenConstants.MODEL_NAME_SUFFIX, "model-suffix");
    want(props, CodegenConstants.REMOVE_OPERATION_ID_PREFIX, false);
    want(props, CodegenConstants.DOCEXTENSION, "D");
    want(props, CodegenConstants.ENABLE_POST_PROCESS_FILE, false);
    want(props, CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
    want(props, CodegenConstants.TEMPLATE_DIR, templateDir);
    want(props, CodegenConstants.GIT_REPO_ID, "git");
    want(props, CodegenConstants.GIT_USER_ID, "user");
    want(props, CodegenConstants.GIT_HOST, "test.com");
    want(props, CodegenConstants.GROUP_ID, "group");
    want(props, CodegenConstants.ARTIFACT_ID, "test-artifactId");
    want(props, CodegenConstants.ARTIFACT_VERSION, "test-artifactVersion");
    want(props, CodegenConstants.HTTP_USER_AGENT, "agent");
    want(props, CodegenConstants.RELEASE_NOTE, "release-note");
    want(props, CodegenConstants.PACKAGE_NAME, "package-name");

    // test custom properties
    want(props, "foo", "bar");
    want(props, "baz", "quux");
}