forked from loafle/openapi-generator-original
Add more tests (c#, swift4) (#330)
* add more tests to csharp client generator * add swift4 tests * update wordings
This commit is contained in:
parent
2103fadab8
commit
86f9686f29
@ -0,0 +1,327 @@
|
||||
package org.openapitools.codegen.csharp;
|
||||
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.languages.CSharpClientCodegen;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.DateTimeSchema;
|
||||
import io.swagger.v3.oas.models.media.IntegerSchema;
|
||||
import io.swagger.v3.oas.models.media.MapSchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
import io.swagger.v3.parser.util.SchemaTypeUtil;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
public class CSharpModelTest {
|
||||
|
||||
@Test(description = "convert a model with array property to default List<T>")
|
||||
public void arrayPropertyTest() {
|
||||
final Schema schema = getArrayTestSchema();
|
||||
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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);
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with array property to Collection<T>")
|
||||
public void arrayPropertyICollectionOptionTest() {
|
||||
final Schema schema = getArrayTestSchema();
|
||||
|
||||
final CSharpClientCodegen codegen = new CSharpClientCodegen();
|
||||
codegen.setUseCollection(true);
|
||||
codegen.setReturnICollection(true);
|
||||
|
||||
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.datatype, "Collection<string>",
|
||||
"returnICollection option should not modify property datatype");
|
||||
Assert.assertNull(property.defaultValue);
|
||||
Assert.assertEquals(property.baseType, "Collection",
|
||||
"returnICollection option should not modify property baseType");
|
||||
Assert.assertEquals(property.containerType, "array");
|
||||
Assert.assertFalse(property.required);
|
||||
Assert.assertTrue(property.isContainer);
|
||||
}
|
||||
|
||||
private Schema getArrayTestSchema() {
|
||||
return new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
|
||||
.addProperties("examples", new ArraySchema().items(new StringSchema()))
|
||||
.addRequiredItem("id");
|
||||
}
|
||||
|
||||
@Test(description = "convert a simple model")
|
||||
public void simpleModelTest() {
|
||||
final Schema model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
|
||||
.addProperties("name", new StringSchema())
|
||||
.addProperties("createdAt", new DateTimeSchema())
|
||||
.addRequiredItem("id")
|
||||
.addRequiredItem("name");
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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(), 3);
|
||||
|
||||
final CodegenProperty property1 = cm.vars.get(0);
|
||||
Assert.assertEquals(property1.baseName, "id");
|
||||
Assert.assertEquals(property1.datatype, "long?");
|
||||
Assert.assertEquals(property1.name, "Id");
|
||||
Assert.assertNull(property1.defaultValue);
|
||||
Assert.assertEquals(property1.baseType, "long?");
|
||||
Assert.assertTrue(property1.hasMore);
|
||||
Assert.assertTrue(property1.required);
|
||||
Assert.assertTrue(property1.isPrimitiveType);
|
||||
Assert.assertTrue(property1.isNotContainer);
|
||||
|
||||
final CodegenProperty property2 = cm.vars.get(1);
|
||||
Assert.assertEquals(property2.baseName, "name");
|
||||
Assert.assertEquals(property2.datatype, "string");
|
||||
Assert.assertEquals(property2.name, "Name");
|
||||
Assert.assertNull(property2.defaultValue);
|
||||
Assert.assertEquals(property2.baseType, "string");
|
||||
Assert.assertTrue(property2.hasMore);
|
||||
Assert.assertTrue(property2.required);
|
||||
Assert.assertTrue(property2.isPrimitiveType);
|
||||
Assert.assertTrue(property2.isNotContainer);
|
||||
|
||||
final CodegenProperty property3 = cm.vars.get(2);
|
||||
Assert.assertEquals(property3.baseName, "createdAt");
|
||||
Assert.assertEquals(property3.datatype, "DateTime?");
|
||||
Assert.assertEquals(property3.name, "CreatedAt");
|
||||
Assert.assertNull(property3.defaultValue);
|
||||
Assert.assertEquals(property3.baseType, "DateTime?");
|
||||
Assert.assertFalse(property3.hasMore);
|
||||
Assert.assertFalse(property3.required);
|
||||
Assert.assertTrue(property3.isNotContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with list property")
|
||||
public void listPropertyTest() {
|
||||
final Schema model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
|
||||
.addProperties("urls", new ArraySchema()
|
||||
.items(new StringSchema()))
|
||||
.addRequiredItem("id");
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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(), 2);
|
||||
|
||||
final CodegenProperty property1 = cm.vars.get(0);
|
||||
Assert.assertEquals(property1.baseName, "id");
|
||||
Assert.assertEquals(property1.datatype, "long?");
|
||||
Assert.assertEquals(property1.name, "Id");
|
||||
Assert.assertNull(property1.defaultValue);
|
||||
Assert.assertEquals(property1.baseType, "long?");
|
||||
Assert.assertTrue(property1.hasMore);
|
||||
Assert.assertTrue(property1.required);
|
||||
Assert.assertTrue(property1.isPrimitiveType);
|
||||
Assert.assertTrue(property1.isNotContainer);
|
||||
|
||||
final CodegenProperty property2 = cm.vars.get(1);
|
||||
Assert.assertEquals(property2.baseName, "urls");
|
||||
Assert.assertEquals(property2.datatype, "List<string>");
|
||||
Assert.assertEquals(property2.name, "Urls");
|
||||
Assert.assertNull(property2.defaultValue);
|
||||
Assert.assertEquals(property2.baseType, "List");
|
||||
Assert.assertFalse(property2.hasMore);
|
||||
Assert.assertEquals(property2.containerType, "array");
|
||||
Assert.assertFalse(property2.required);
|
||||
Assert.assertTrue(property2.isPrimitiveType);
|
||||
Assert.assertTrue(property2.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with a map property")
|
||||
public void mapPropertyTest() {
|
||||
final Schema schema = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("translations", new MapSchema()
|
||||
.additionalProperties(new StringSchema()))
|
||||
.addRequiredItem("id");
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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, "translations");
|
||||
Assert.assertEquals(property1.datatype, "Dictionary<string, string>");
|
||||
Assert.assertEquals(property1.name, "Translations");
|
||||
Assert.assertEquals(property1.baseType, "Dictionary");
|
||||
Assert.assertEquals(property1.containerType, "map");
|
||||
Assert.assertFalse(property1.required);
|
||||
Assert.assertTrue(property1.isContainer);
|
||||
Assert.assertTrue(property1.isPrimitiveType);
|
||||
}
|
||||
|
||||
@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("#/components/schemas/Children"));
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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.baseType, "Children");
|
||||
Assert.assertFalse(property1.required);
|
||||
Assert.assertTrue(property1.isNotContainer);
|
||||
}
|
||||
|
||||
@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("#/components/schemas/Children")));
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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, "List<Children>");
|
||||
Assert.assertEquals(property1.name, "Children");
|
||||
Assert.assertEquals(property1.baseType, "List");
|
||||
Assert.assertEquals(property1.containerType, "array");
|
||||
Assert.assertFalse(property1.required);
|
||||
Assert.assertTrue(property1.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with complex map property")
|
||||
public void complexMapPropertyTest() {
|
||||
final Schema schema = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("children", new MapSchema()
|
||||
.additionalProperties(new Schema().$ref("#/components/schemas/Children")));
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
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);
|
||||
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
|
||||
|
||||
final CodegenProperty property1 = cm.vars.get(0);
|
||||
Assert.assertEquals(property1.baseName, "children");
|
||||
Assert.assertEquals(property1.complexType, "Children");
|
||||
Assert.assertEquals(property1.datatype, "Dictionary<string, Children>");
|
||||
Assert.assertEquals(property1.name, "Children");
|
||||
Assert.assertEquals(property1.baseType, "Dictionary");
|
||||
Assert.assertEquals(property1.containerType, "map");
|
||||
Assert.assertFalse(property1.required);
|
||||
Assert.assertTrue(property1.isContainer);
|
||||
Assert.assertFalse(property1.isNotContainer);
|
||||
}
|
||||
|
||||
@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();
|
||||
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);
|
||||
}
|
||||
|
||||
@Test(description = "convert an 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();
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package org.openapitools.codegen.csharp;
|
||||
|
||||
import io.swagger.v3.oas.models.media.ComposedSchema;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.languages.CSharpClientCodegen;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CsharpModelEnumTest {
|
||||
// TODO there's no parent/child method in ComposeSchema so we will need to revise the code
|
||||
// before we can reeanble the test case below
|
||||
@Test(description = "not override identical parent enums", enabled = false)
|
||||
public void overrideEnumTest() {
|
||||
final StringSchema identicalEnumProperty = new StringSchema();
|
||||
identicalEnumProperty.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
|
||||
|
||||
final StringSchema subEnumProperty = new StringSchema();
|
||||
subEnumProperty.setEnum(Arrays.asList("SUB1", "SUB2", "SUB3"));
|
||||
|
||||
// Add one enum property to the parent
|
||||
final Map<String, Schema> parentProperties = new HashMap<String, Schema>();
|
||||
parentProperties.put("sharedThing", identicalEnumProperty);
|
||||
|
||||
// Add TWO enums to the subType model; one of which is identical to the one in parent class
|
||||
final Map<String, Schema> subProperties = new HashMap<String, Schema>();
|
||||
subProperties.put("sharedThing", identicalEnumProperty);
|
||||
subProperties.put("unsharedThing", identicalEnumProperty);
|
||||
|
||||
final Schema parentModel = new Schema()
|
||||
.description("parentModel");
|
||||
parentModel.setProperties(parentProperties);
|
||||
parentModel.name("parentModel");
|
||||
|
||||
final Schema subModel = new Schema()
|
||||
.description("subModel");
|
||||
subModel.setProperties(subProperties);
|
||||
subModel.name("subModel");
|
||||
|
||||
/* TODO revise the following as there's parent/child method
|
||||
final ComposedSchema model = new ComposedSchema().
|
||||
.parent(new RefModel(parentModel.getName()))
|
||||
.child(subModel)
|
||||
.interfaces(new ArrayList<RefModel>());
|
||||
*/
|
||||
final DefaultCodegen codegen = new CSharpClientCodegen();
|
||||
final Map<String, Schema> allModels = new HashMap<>();
|
||||
allModels.put("ParentModel", parentModel);
|
||||
allModels.put("SubModel", subModel);
|
||||
|
||||
/*
|
||||
final CodegenModel cm = codegen.fromModel("sample", model, allModels);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.parent, "ParentModel");
|
||||
Assert.assertTrue(cm.imports.contains("ParentModel"));
|
||||
|
||||
// Assert that only the unshared/uninherited enum remains
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
final CodegenProperty enumVar = cm.vars.get(0);
|
||||
Assert.assertEquals(enumVar.baseName, "unsharedThing");
|
||||
Assert.assertEquals(enumVar.datatype, "string");
|
||||
Assert.assertEquals(enumVar.datatypeWithEnum, "UnsharedThingEnum");
|
||||
Assert.assertTrue(enumVar.isEnum);
|
||||
*/
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package org.openapitools.codegen.options;
|
||||
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.Swift4Codegen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Swift4OptionsProvider implements OptionsProvider {
|
||||
public static final String SORT_PARAMS_VALUE = "false";
|
||||
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
|
||||
public static final String PROJECT_NAME_VALUE = "Swagger";
|
||||
public static final String RESPONSE_AS_VALUE = "test";
|
||||
public static final String UNWRAP_REQUIRED_VALUE = "true";
|
||||
public static final String OBJC_COMPATIBLE_VALUE = "false";
|
||||
public static final String LENIENT_TYPE_CAST_VALUE = "false";
|
||||
public static final String POD_SOURCE_VALUE = "{ :git => 'git@github.com:swagger-api/swagger-mustache.git'," +
|
||||
" :tag => 'v1.0.0-SNAPSHOT' }";
|
||||
public static final String POD_VERSION_VALUE = "v1.0.0-SNAPSHOT";
|
||||
public static final String POD_AUTHORS_VALUE = "podAuthors";
|
||||
public static final String POD_SOCIAL_MEDIA_URL_VALUE = "podSocialMediaURL";
|
||||
public static final String POD_DOCSET_URL_VALUE = "podDocsetURL";
|
||||
public static final String POD_LICENSE_VALUE = "'Apache License, Version 2.0'";
|
||||
public static final String POD_HOMEPAGE_VALUE = "podHomepage";
|
||||
public static final String POD_SUMMARY_VALUE = "podSummary";
|
||||
public static final String POD_DESCRIPTION_VALUE = "podDescription";
|
||||
public static final String POD_SCREENSHOTS_VALUE = "podScreenshots";
|
||||
public static final String POD_DOCUMENTATION_URL_VALUE = "podDocumentationURL";
|
||||
public static final String SWIFT_USE_API_NAMESPACE_VALUE = "swiftUseApiNamespace";
|
||||
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
|
||||
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return "swift4";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> createOptions() {
|
||||
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
|
||||
return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
|
||||
.put(Swift4Codegen.PROJECT_NAME, PROJECT_NAME_VALUE)
|
||||
.put(Swift4Codegen.RESPONSE_AS, RESPONSE_AS_VALUE)
|
||||
.put(Swift4Codegen.UNWRAP_REQUIRED, UNWRAP_REQUIRED_VALUE)
|
||||
.put(Swift4Codegen.OBJC_COMPATIBLE, OBJC_COMPATIBLE_VALUE)
|
||||
.put(Swift4Codegen.LENIENT_TYPE_CAST, LENIENT_TYPE_CAST_VALUE)
|
||||
.put(Swift4Codegen.POD_SOURCE, POD_SOURCE_VALUE)
|
||||
.put(CodegenConstants.POD_VERSION, POD_VERSION_VALUE)
|
||||
.put(Swift4Codegen.POD_AUTHORS, POD_AUTHORS_VALUE)
|
||||
.put(Swift4Codegen.POD_SOCIAL_MEDIA_URL, POD_SOCIAL_MEDIA_URL_VALUE)
|
||||
.put(Swift4Codegen.POD_DOCSET_URL, POD_DOCSET_URL_VALUE)
|
||||
.put(Swift4Codegen.POD_LICENSE, POD_LICENSE_VALUE)
|
||||
.put(Swift4Codegen.POD_HOMEPAGE, POD_HOMEPAGE_VALUE)
|
||||
.put(Swift4Codegen.POD_SUMMARY, POD_SUMMARY_VALUE)
|
||||
.put(Swift4Codegen.POD_DESCRIPTION, POD_DESCRIPTION_VALUE)
|
||||
.put(Swift4Codegen.POD_SCREENSHOTS, POD_SCREENSHOTS_VALUE)
|
||||
.put(Swift4Codegen.POD_DOCUMENTATION_URL, POD_DOCUMENTATION_URL_VALUE)
|
||||
.put(Swift4Codegen.SWIFT_USE_API_NAMESPACE, SWIFT_USE_API_NAMESPACE_VALUE)
|
||||
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
|
||||
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
|
||||
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package org.openapitools.codegen.swift4;
|
||||
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.CodegenOperation;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.languages.Swift4Codegen;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.parser.OpenAPIV3Parser;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
|
||||
public class Swift4CodegenTest {
|
||||
|
||||
Swift4Codegen swiftCodegen = new Swift4Codegen();
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testCapitalizedReservedWord() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("AS", null), "_as");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testReservedWord() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("Public", null), "_public");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void shouldNotBreakNonReservedWord() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("Error", null), "error");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void shouldNotBreakCorrectName() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("EntryName", null), "entryName");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testSingleWordAllCaps() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("VALUE", null), "value");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testSingleWordLowercase() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("value", null), "value");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testCapitalsWithUnderscore() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY_NAME", null), "entryName");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testCapitalsWithDash() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY-NAME", null), "entryName");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testCapitalsWithSpace() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY NAME", null), "entryName");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testLowercaseWithUnderscore() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("entry_name", null), "entryName");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testStartingWithNumber() throws Exception {
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName", null), "_123entryName");
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("123Entry_name", null), "_123entryName");
|
||||
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName123", null), "_123entryName123");
|
||||
}
|
||||
|
||||
@Test(description = "returns Data when response format is binary", enabled = false)
|
||||
public void binaryDataTest() {
|
||||
// TODO update json file
|
||||
|
||||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/binaryDataTest.json");
|
||||
final DefaultCodegen codegen = new Swift4Codegen();
|
||||
final String path = "/tests/binaryResponse";
|
||||
final Operation p = openAPI.getPaths().get(path).getPost();
|
||||
final CodegenOperation op = codegen.fromOperation(path, "post", p, openAPI.getComponents().getSchemas());
|
||||
|
||||
Assert.assertEquals(op.returnType, "Data");
|
||||
Assert.assertEquals(op.bodyParam.dataType, "Data");
|
||||
Assert.assertTrue(op.bodyParam.isBinary);
|
||||
Assert.assertTrue(op.responses.get(0).isBinary);
|
||||
}
|
||||
|
||||
@Test(description = "returns Date when response format is date", enabled = false)
|
||||
public void dateTest() {
|
||||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/datePropertyTest.json");
|
||||
final DefaultCodegen codegen = new Swift4Codegen();
|
||||
final String path = "/tests/dateResponse";
|
||||
final Operation p = openAPI.getPaths().get(path).getPost();
|
||||
final CodegenOperation op = codegen.fromOperation(path, "post", p, openAPI.getComponents().getSchemas());
|
||||
|
||||
Assert.assertEquals(op.returnType, "Date");
|
||||
Assert.assertEquals(op.bodyParam.dataType, "Date");
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testDefaultPodAuthors() throws Exception {
|
||||
// Given
|
||||
|
||||
// When
|
||||
swiftCodegen.processOpts();
|
||||
|
||||
// Then
|
||||
final String podAuthors = (String) swiftCodegen.additionalProperties().get(Swift4Codegen.POD_AUTHORS);
|
||||
Assert.assertEquals(podAuthors, Swift4Codegen.DEFAULT_POD_AUTHORS);
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testPodAuthors() throws Exception {
|
||||
// Given
|
||||
final String openAPIDevs = "OpenAPI Devs";
|
||||
swiftCodegen.additionalProperties().put(Swift4Codegen.POD_AUTHORS, openAPIDevs);
|
||||
|
||||
// When
|
||||
swiftCodegen.processOpts();
|
||||
|
||||
// Then
|
||||
final String podAuthors = (String) swiftCodegen.additionalProperties().get(Swift4Codegen.POD_AUTHORS);
|
||||
Assert.assertEquals(podAuthors, openAPIDevs);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
package org.openapitools.codegen.swift4;
|
||||
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.languages.Swift4Codegen;
|
||||
import io.swagger.v3.oas.models.media.BinarySchema;
|
||||
import io.swagger.v3.oas.models.media.ByteArraySchema;
|
||||
import io.swagger.v3.oas.models.media.DateSchema;
|
||||
import io.swagger.v3.oas.models.media.DateTimeSchema;
|
||||
import io.swagger.v3.oas.models.media.Discriminator;
|
||||
import io.swagger.v3.oas.models.media.IntegerSchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.media.StringSchema;
|
||||
import io.swagger.v3.oas.models.media.UUIDSchema;
|
||||
import io.swagger.v3.parser.util.SchemaTypeUtil;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
public class Swift4ModelTest {
|
||||
|
||||
@Test(description = "convert a simple java model", enabled = true)
|
||||
public void simpleModelTest() {
|
||||
final Schema schema = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT))
|
||||
.addProperties("name", new StringSchema())
|
||||
.addProperties("createdAt", new DateTimeSchema())
|
||||
.addProperties("binary", new BinarySchema())
|
||||
.addProperties("byte", new ByteArraySchema())
|
||||
.addProperties("uuid", new UUIDSchema())
|
||||
.addProperties("dateOfBirth", new DateSchema())
|
||||
.addRequiredItem("id")
|
||||
.addRequiredItem("name")
|
||||
.discriminator(new Discriminator().propertyName("test"));
|
||||
final DefaultCodegen codegen = new Swift4Codegen();
|
||||
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(), 7);
|
||||
Assert.assertEquals(cm.getDiscriminatorName(),"test");
|
||||
|
||||
final CodegenProperty property1 = cm.vars.get(0);
|
||||
Assert.assertEquals(property1.baseName, "id");
|
||||
Assert.assertEquals(property1.datatype, "Int64");
|
||||
Assert.assertEquals(property1.name, "_id");
|
||||
Assert.assertNull(property1.defaultValue);
|
||||
Assert.assertEquals(property1.baseType, "Int64");
|
||||
Assert.assertTrue(property1.hasMore);
|
||||
Assert.assertTrue(property1.required);
|
||||
Assert.assertTrue(property1.isPrimitiveType);
|
||||
Assert.assertTrue(property1.isNotContainer);
|
||||
|
||||
final CodegenProperty property2 = cm.vars.get(1);
|
||||
Assert.assertEquals(property2.baseName, "name");
|
||||
Assert.assertEquals(property2.datatype, "String");
|
||||
Assert.assertEquals(property2.name, "name");
|
||||
Assert.assertNull(property2.defaultValue);
|
||||
Assert.assertEquals(property2.baseType, "String");
|
||||
Assert.assertTrue(property2.hasMore);
|
||||
Assert.assertTrue(property2.required);
|
||||
Assert.assertTrue(property2.isPrimitiveType);
|
||||
Assert.assertTrue(property2.isNotContainer);
|
||||
|
||||
final CodegenProperty property3 = cm.vars.get(2);
|
||||
Assert.assertEquals(property3.baseName, "createdAt");
|
||||
Assert.assertEquals(property3.datatype, "Date");
|
||||
Assert.assertEquals(property3.name, "createdAt");
|
||||
Assert.assertNull(property3.defaultValue);
|
||||
Assert.assertEquals(property3.baseType, "Date");
|
||||
Assert.assertTrue(property3.hasMore);
|
||||
Assert.assertFalse(property3.required);
|
||||
Assert.assertTrue(property3.isNotContainer);
|
||||
|
||||
final CodegenProperty property4 = cm.vars.get(3);
|
||||
Assert.assertEquals(property4.baseName, "binary");
|
||||
Assert.assertEquals(property4.datatype, "URL");
|
||||
Assert.assertEquals(property4.name, "binary");
|
||||
Assert.assertNull(property4.defaultValue);
|
||||
Assert.assertEquals(property4.baseType, "URL");
|
||||
Assert.assertTrue(property4.hasMore);
|
||||
Assert.assertFalse(property4.required);
|
||||
Assert.assertTrue(property4.isNotContainer);
|
||||
|
||||
final CodegenProperty property5 = cm.vars.get(4);
|
||||
Assert.assertEquals(property5.baseName, "byte");
|
||||
Assert.assertEquals(property5.datatype, "Data");
|
||||
Assert.assertEquals(property5.name, "byte");
|
||||
Assert.assertNull(property5.defaultValue);
|
||||
Assert.assertEquals(property5.baseType, "Data");
|
||||
Assert.assertTrue(property5.hasMore);
|
||||
Assert.assertFalse(property5.required);
|
||||
Assert.assertTrue(property5.isNotContainer);
|
||||
|
||||
final CodegenProperty property6 = cm.vars.get(5);
|
||||
Assert.assertEquals(property6.baseName, "uuid");
|
||||
Assert.assertEquals(property6.datatype, "UUID");
|
||||
Assert.assertEquals(property6.name, "uuid");
|
||||
Assert.assertNull(property6.defaultValue);
|
||||
Assert.assertEquals(property6.baseType, "UUID");
|
||||
Assert.assertTrue(property6.hasMore);
|
||||
Assert.assertFalse(property6.required);
|
||||
Assert.assertTrue(property6.isNotContainer);
|
||||
|
||||
final CodegenProperty property7 = cm.vars.get(6);
|
||||
Assert.assertEquals(property7.baseName, "dateOfBirth");
|
||||
Assert.assertEquals(property7.datatype, "Date");
|
||||
Assert.assertEquals(property7.name, "dateOfBirth");
|
||||
Assert.assertNull(property7.defaultValue);
|
||||
Assert.assertEquals(property7.baseType, "Date");
|
||||
Assert.assertFalse(property7.hasMore);
|
||||
Assert.assertFalse(property7.required);
|
||||
Assert.assertTrue(property7.isNotContainer);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package org.openapitools.codegen.swift4;
|
||||
|
||||
import org.openapitools.codegen.AbstractOptionsTest;
|
||||
import org.openapitools.codegen.CodegenConfig;
|
||||
import org.openapitools.codegen.languages.Swift4Codegen;
|
||||
import org.openapitools.codegen.options.Swift4OptionsProvider;
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
public class Swift4OptionsTest extends AbstractOptionsTest {
|
||||
|
||||
@Tested
|
||||
private Swift4Codegen clientCodegen;
|
||||
|
||||
public Swift4OptionsTest() {
|
||||
super(new Swift4OptionsProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
new Expectations(clientCodegen) {{
|
||||
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(Swift4OptionsProvider.SORT_PARAMS_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setProjectName(Swift4OptionsProvider.PROJECT_NAME_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setResponseAs(Swift4OptionsProvider.RESPONSE_AS_VALUE.split(","));
|
||||
times = 1;
|
||||
clientCodegen.setUnwrapRequired(Boolean.valueOf(Swift4OptionsProvider.UNWRAP_REQUIRED_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setObjcCompatible(Boolean.valueOf(Swift4OptionsProvider.OBJC_COMPATIBLE_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setLenientTypeCast(Boolean.valueOf(Swift4OptionsProvider.LENIENT_TYPE_CAST_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(Swift4OptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE));
|
||||
times = 1;
|
||||
}};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user