org.openapitools.codegen.CodegenModel Java Examples

The following examples show how to use org.openapitools.codegen.CodegenModel. 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: TypeScriptNodeModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a map model")
public void mapModelTest() {
    final Schema schema = new Schema()
            .description("a map model")
            .additionalProperties(new Schema().$ref("#/definitions/Children"));
    final DefaultCodegen codegen = new TypeScriptNodeClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a map model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #2
Source File: TypeScriptFetchModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a map model")
public void mapModelTest() {
    final Schema model = new Schema()
            .description("a map model")
            .additionalProperties(new Schema().$ref("#/definitions/Children"));
    final DefaultCodegen codegen = new TypeScriptFetchClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a map model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #3
Source File: GoModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a map model")
public void mapModelTest() {
    final Schema model = new Schema()
            .additionalProperties(new Schema().$ref("#/definitions/Children"))
            .description("a map model");
    final DefaultCodegen codegen = new GoClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a map model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #4
Source File: TypeScriptAngularModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex property")
public void complexPropertyTest() {
    final Schema schema = new Schema()
            .description("a sample model")
            .addProperties("children", new Schema().$ref("#/definitions/Children"));
    final DefaultCodegen codegen = new TypeScriptAngularClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "children");
    Assert.assertEquals(property1.dataType, "Children");
    Assert.assertEquals(property1.name, "children");
    Assert.assertEquals(property1.defaultValue, "undefined");
    Assert.assertEquals(property1.baseType, "Children");
    Assert.assertFalse(property1.required);
}
 
Example #5
Source File: AbstractKotlinCodegen.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
    objs = super.postProcessModelsEnum(objs);
    List<Object> models = (List<Object>) objs.get("models");
    for (Object _mo : models) {
        Map<String, Object> mo = (Map<String, Object>) _mo;
        CodegenModel cm = (CodegenModel) mo.get("model");
        if (cm.getDiscriminator() != null) {
            cm.vendorExtensions.put("x-has-data-class-body", true);
            break;
        }

        for (CodegenProperty var : cm.vars) {
            if (var.isEnum || isSerializableModel()) {
                cm.vendorExtensions.put("x-has-data-class-body", true);
                break;
            }
        }
    }
    return postProcessModelsEnum(objs);
}
 
Example #6
Source File: JavaCXFExtServerCodegen.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
    objs = super.postProcessAllModels(objs);

    // When populating operation bodies we need to import enum types, which requires the class that defines them.
    if (generateOperationBody) {
        for (Object value : objs.values()) {
            @SuppressWarnings("unchecked")
            Map<String, Object> inner = (Map<String, Object>) value;
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models");
            for (Map<String, Object> mo : models)
                postProcessModel((CodegenModel) mo.get("model"));
        }
    }

    return objs;
}
 
Example #7
Source File: KotlinClientCodegenModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex property")
public void complexPropertyTest() {
    final Schema schema = getComplexSchema();
    final DefaultCodegen codegen = new KotlinClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "child");
    Assert.assertEquals(property1.dataType, "Child");
    Assert.assertEquals(property1.name, "child");
    Assert.assertEquals(property1.baseType, "Child");
    Assert.assertFalse(property1.required);
    Assert.assertFalse(property1.isContainer);
}
 
Example #8
Source File: TypeScriptAngularJsModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex list property")
public void complexListPropertyTest() {
    final Schema schema = new Schema()
            .description("a sample model")
            .addProperties("children", new ArraySchema()
                    .items(new Schema().$ref("#/definitions/Children")));
    final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "children");
    Assert.assertEquals(property1.complexType, "models.Children");
    Assert.assertEquals(property1.dataType, "Array<models.Children>");
    Assert.assertEquals(property1.name, "children");
    Assert.assertEquals(property1.baseType, "Array");
    Assert.assertFalse(property1.required);
}
 
Example #9
Source File: CSharpModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with array property to default List<T>")
public void arrayPropertyTest() {
    final Schema schema = getArrayTestSchema();

    final DefaultCodegen codegen = new CSharpClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel generated = codegen.fromModel("sample", schema);

    Assert.assertEquals(generated.name, "sample");
    Assert.assertEquals(generated.classname, "Sample");
    Assert.assertEquals(generated.description, "a sample model");
    Assert.assertEquals(generated.vars.size(), 2);

    final CodegenProperty property = generated.vars.get(1);
    Assert.assertEquals(property.baseName, "examples");
    Assert.assertEquals(property.getter, "getExamples");
    Assert.assertEquals(property.setter, "setExamples");
    Assert.assertEquals(property.dataType, "List<string>");
    Assert.assertEquals(property.name, "Examples");
    Assert.assertNull(property.defaultValue);
    Assert.assertEquals(property.baseType, "List");
    Assert.assertEquals(property.containerType, "array");
    Assert.assertFalse(property.required);
    Assert.assertTrue(property.isContainer);
}
 
Example #10
Source File: JavaInheritanceTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a composed model with parent")
public void javaInheritanceTest() {
    final Schema parentModel = new Schema().name("Base");

    final Schema schema = new ComposedSchema()
            .addAllOfItem(new Schema().$ref("Base"))
            .name("composed");

    OpenAPI openAPI = TestUtils.createOpenAPI();
    openAPI.setComponents(new Components()
            .addSchemas(parentModel.getName(),parentModel)
            .addSchemas(schema.getName(), schema)
    );

    final JavaClientCodegen codegen = new JavaClientCodegen();
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.parent, "Base");
    Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
}
 
Example #11
Source File: JavaInheritanceTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a composed model with discriminator")
public void javaInheritanceWithDiscriminatorTest() {
    final Schema base = new Schema().name("Base");
    Discriminator discriminator = new Discriminator().mapping("name", StringUtils.EMPTY);
    discriminator.setPropertyName("model_type");
    base.setDiscriminator(discriminator);

    final Schema schema = new ComposedSchema()
            .addAllOfItem(new Schema().$ref("Base"));

    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("Base", base);

    final JavaClientCodegen codegen = new JavaClientCodegen();
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.parent, "Base");
    Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
}
 
Example #12
Source File: PlantumlDocumentationCodegenTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void listFieldTest() {
    final Schema model = new Schema()
            .description("a sample model")
            .addProperties("tags", new ArraySchema().items(new StringSchema()));
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    plantumlDocumentationCodegen.setOpenAPI(openAPI);
    final CodegenModel cm = plantumlDocumentationCodegen.fromModel("sample", model);

    Map<String, Object> objs = createObjectsMapFor(cm);

    plantumlDocumentationCodegen.postProcessSupportingFileData(objs);

    List<Object> entityList = getList(objs, "entities");

    Map<String, Object> sampleEntity = getEntityFromList("Sample", entityList);
    Map<String, Object> tagsField = getFieldFromEntity("tags", sampleEntity);
    Assert.assertEquals((String)tagsField.get("dataType"), "List<String>");
}
 
Example #13
Source File: TypeScriptAngularModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "import a typemapping")
public void importTypeMappingModelTest() {
    final Schema schema = new ArraySchema()
            .items(new Schema().$ref("Children"))
            .description("a typemapping array model");
    final DefaultCodegen codegen = new TypeScriptAngularClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    codegen.typeMapping().put("Children", "Test");
    codegen.importMapping().put("Test", "@myTest/package");
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a typemapping array model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Test")).size(), 1);
}
 
Example #14
Source File: TypeScriptAngularModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a map model")
public void mapModelTest() {
    final Schema schema = new Schema()
            .description("a map model")
            .additionalProperties(new Schema().$ref("#/definitions/Children"));
    final DefaultCodegen codegen = new TypeScriptAngularClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a map model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(cm.additionalPropertiesType, "Children");
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #15
Source File: CSharpModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with array property to Collection<T>")
public void arrayPropertyCollectionOptionTest() {
    final Schema schema = getArrayTestSchema();

    final CSharpClientCodegen codegen = new CSharpClientCodegen();
    codegen.setUseCollection(true);

    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel generated = codegen.fromModel("sample", schema);

    Assert.assertEquals(generated.name, "sample");
    Assert.assertEquals(generated.vars.size(), 2);

    final CodegenProperty property = generated.vars.get(1);
    Assert.assertEquals(property.baseName, "examples");
    Assert.assertEquals(property.name, "Examples");
    Assert.assertEquals(property.defaultValue, null);
    Assert.assertEquals(property.dataType, "Collection<string>");
    Assert.assertEquals(property.baseType, "Collection");
    Assert.assertEquals(property.containerType, "array");
    Assert.assertFalse(property.required);
    Assert.assertTrue(property.isContainer);
}
 
Example #16
Source File: TypeScriptAngularModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert an any of with array oneof model")
public void objectPropertyAnyOfWithArrayOneOfModelTest() {
    final Schema schema = new ObjectSchema().addProperties("value",
            new ComposedSchema().addAnyOfItem(new StringSchema()).addAnyOfItem(new ArraySchema()
                    .items(new ComposedSchema()
                            .addOneOfItem(new StringSchema())
                            .addOneOfItem(new IntegerSchema().format("int64")))))
            .description("an any of with array oneof model");
    final DefaultCodegen codegen = new TypeScriptAngularClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    String s = codegen.getSchemaType((Schema)schema.getProperties().get("value"));


    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "an any of with array oneof model");
    Assert.assertEquals(cm.vars.size(), 1);
    Assert.assertEquals(s, "string | Array<string | number>");
}
 
Example #17
Source File: Swift4ModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a java model with an string enum and a default value")
public void convertStringDefaultValueTest() {
    final StringSchema enumSchema = new StringSchema();
    enumSchema.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
    enumSchema.setDefault("VALUE2");
    final Schema model = new Schema().type("object").addProperties("name", enumSchema);

    final DefaultCodegen codegen = new Swift4Codegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty enumVar = cm.vars.get(0);
    Assert.assertEquals(enumVar.baseName, "name");
    Assert.assertEquals(enumVar.dataType, "String");
    Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
    Assert.assertEquals(enumVar.name, "name");
    Assert.assertEquals(enumVar.defaultValue, ".value2");
    Assert.assertEquals(enumVar.baseType, "String");
    Assert.assertTrue(enumVar.isEnum);
}
 
Example #18
Source File: Swift4ModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a java model with an reserved word string enum and a default value")
public void convertReservedWordStringDefaultValueTest() {
    final StringSchema enumSchema = new StringSchema();
    enumSchema.setEnum(Arrays.asList("1st", "2nd", "3rd"));
    enumSchema.setDefault("2nd");
    final Schema model = new Schema().type("object").addProperties("name", enumSchema);

    final DefaultCodegen codegen = new Swift4Codegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty enumVar = cm.vars.get(0);
    Assert.assertEquals(enumVar.baseName, "name");
    Assert.assertEquals(enumVar.dataType, "String");
    Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
    Assert.assertEquals(enumVar.name, "name");
    Assert.assertEquals(enumVar.defaultValue, "._2nd");
    Assert.assertEquals(enumVar.baseType, "String");
    Assert.assertTrue(enumVar.isEnum);
}
 
Example #19
Source File: TypeScriptAngularModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex list property")
public void complexListPropertyTest() {
    final Schema schema = new Schema()
            .description("a sample model")
            .addProperties("children", new ArraySchema()
                    .items(new Schema().$ref("#/definitions/Children")));
    final DefaultCodegen codegen = new TypeScriptAngularClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "children");
    Assert.assertEquals(property1.complexType, "Children");
    Assert.assertEquals(property1.dataType, "Array<Children>");
    Assert.assertEquals(property1.name, "children");
    Assert.assertEquals(property1.baseType, "Array");
    Assert.assertFalse(property1.required);
}
 
Example #20
Source File: Swift4ModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a java model with an number enum and a default value")
public void convertNumberDefaultValueTest() {
    final NumberSchema enumSchema = new NumberSchema();
    enumSchema.setEnum(Arrays.asList(new BigDecimal(10), new BigDecimal(100), new BigDecimal(1000)));
    enumSchema.setDefault(new BigDecimal((100)));
    final Schema model = new Schema().type("object").addProperties("name", enumSchema);

    final DefaultCodegen codegen = new Swift4Codegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty enumVar = cm.vars.get(0);
    Assert.assertEquals(enumVar.baseName, "name");
    Assert.assertEquals(enumVar.dataType, "Double");
    Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
    Assert.assertEquals(enumVar.name, "name");
    Assert.assertEquals(enumVar.defaultValue, "._100");
    Assert.assertEquals(enumVar.baseType, "Double");
    Assert.assertTrue(enumVar.isEnum);
}
 
Example #21
Source File: CSharpModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a map model")
public void mapModelTest() {
    final Schema schema = new Schema()
            .description("a map model")
            .additionalProperties(new Schema().$ref("#/components/schemas/Children"));
    final DefaultCodegen codegen = new CSharpClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a map model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.parent, "Dictionary<String, Children>");
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #22
Source File: CsharpModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "support empty suffixes for enums")
public void useEmptyEnumSuffixes() {
    final AspNetCoreServerCodegen codegen = new AspNetCoreServerCodegen();
    codegen.setEnumNameSuffix("");
    codegen.setEnumValueSuffix("");

    OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
    codegen.setOpenAPI(openAPI);

    final Schema petSchema = openAPI.getComponents().getSchemas().get("Pet");
    final CodegenModel cm = codegen.fromModel("Pet", petSchema);
    final CodegenProperty statusProperty = cm.vars.get(5);
    Assert.assertEquals(statusProperty.name, "Status");
    Assert.assertTrue(statusProperty.isEnum);
    Assert.assertEquals(statusProperty.datatypeWithEnum, "Status");

    Assert.assertEquals(codegen.toEnumVarName("Aaaa", ""), "Aaaa");
}
 
Example #23
Source File: CSharpModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert an array model")
public void arrayModelTest() {
    final Schema schema = new ArraySchema()
            .items(new Schema().$ref("#/components/schemas/Children"))
            .description("an array model");
    final DefaultCodegen codegen = new CSharpClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "an array model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.parent, "List<Children>");
    Assert.assertEquals(cm.imports.size(), 1);
    Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #24
Source File: DartDioModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex list property")
public void complexListProperty() {
    final Schema model = new Schema()
            .description("a sample model")
            .addProperties("children", new ArraySchema()
                    .items(new Schema().$ref("#/definitions/Children")));
    final DefaultCodegen codegen = new DartDioClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "children");
    Assert.assertEquals(property1.dataType, "BuiltList<Children>");
    Assert.assertEquals(property1.name, "children");
    Assert.assertEquals(property1.baseType, "BuiltList");
    Assert.assertEquals(property1.containerType, "array");
    Assert.assertFalse(property1.required);
    Assert.assertTrue(property1.isContainer);
}
 
Example #25
Source File: KotlinClientCodegenModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a simple model: threetenbp")
public void selectDateLibraryAsThreetenbp() {
    final Schema schema = getSimpleSchema();
    final KotlinClientCodegen codegen = new KotlinClientCodegen();
    codegen.setDateLibrary(KotlinClientCodegen.DateLibrary.THREETENBP.value);
    codegen.processOpts();

    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    final CodegenProperty property3 = cm.vars.get(2);
    Assert.assertEquals(property3.baseName, "createdAt");
    Assert.assertEquals(property3.dataType, "org.threeten.bp.OffsetDateTime");
    Assert.assertEquals(property3.name, "createdAt");
    Assert.assertEquals(property3.defaultValue, null);
    Assert.assertEquals(property3.baseType, "org.threeten.bp.OffsetDateTime");
    Assert.assertFalse(property3.hasMore);
    Assert.assertFalse(property3.required);
    Assert.assertFalse(property3.isContainer);
}
 
Example #26
Source File: DartDioModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a map model")
public void mapModelTest() {
    final Schema model = new Schema()
            .description("a map model")
            .additionalProperties(new Schema().$ref("#/definitions/Children"));
    final DefaultCodegen codegen = new DartDioClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a map model");
    Assert.assertEquals(cm.vars.size(), 0);
    // {{imports}} is not used in template
    //Assert.assertEquals(cm.imports.size(), 2);
    //Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
 
Example #27
Source File: Swift5ModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a java model with an reserved word string enum and a default value")
public void convertReservedWordStringDefaultValueTest() {
    final StringSchema enumSchema = new StringSchema();
    enumSchema.setEnum(Arrays.asList("1st", "2nd", "3rd"));
    enumSchema.setDefault("2nd");
    final Schema model = new Schema().type("object").addProperties("name", enumSchema);

    final DefaultCodegen codegen = new Swift5ClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty enumVar = cm.vars.get(0);
    Assert.assertEquals(enumVar.baseName, "name");
    Assert.assertEquals(enumVar.dataType, "String");
    Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
    Assert.assertEquals(enumVar.name, "name");
    Assert.assertEquals(enumVar.defaultValue, "._2nd");
    Assert.assertEquals(enumVar.baseType, "String");
    Assert.assertTrue(enumVar.isEnum);
}
 
Example #28
Source File: GoModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex property")
public void complexPropertyTest() {
    final Schema model = new Schema()
            .description("a sample model")
            .addProperties("children", new Schema().$ref("#/definitions/Children"));
    final DefaultCodegen codegen = new GoClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "children");
    Assert.assertEquals(property1.dataType, "Children");
    Assert.assertEquals(property1.name, "Children");
    Assert.assertEquals(property1.baseType, "Children");
    Assert.assertFalse(property1.required);
}
 
Example #29
Source File: GoModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a model with complex list property")
public void complexListProperty() {
    final Schema model = new Schema()
            .description("a sample model")
            .addProperties("children", new ArraySchema()
                    .items(new Schema().$ref("#/definitions/Children")));
    final DefaultCodegen codegen = new GoClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "children");
    Assert.assertEquals(property1.dataType, "[]Children");
    Assert.assertEquals(property1.name, "Children");
    Assert.assertEquals(property1.baseType, "array");
    Assert.assertEquals(property1.containerType, "array");
    Assert.assertFalse(property1.required);
    Assert.assertTrue(property1.isContainer);
}
 
Example #30
Source File: GoModelTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert an array model")
public void arrayModelTest() {
    final Schema model = new ArraySchema()
            .items(new Schema().$ref("#/definitions/Children"))
            .description("an array model");
    final DefaultCodegen codegen = new GoClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "an array model");
    Assert.assertEquals(cm.vars.size(), 0);
    Assert.assertEquals(cm.imports.size(), 1);
}