forked from loafle/openapi-generator-original
add back php test cases
This commit is contained in:
parent
5326152ccd
commit
af4dcb9cbb
@ -1102,8 +1102,11 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
if (StringUtils.isNotBlank(schema.get$ref())) { // object
|
||||
try {
|
||||
datatype = schema.get$ref();
|
||||
// get the model name from $ref
|
||||
if (datatype.indexOf("#/components/schemas/") == 0) {
|
||||
datatype = datatype.substring("#/components/schemas/".length());
|
||||
} else if (datatype.indexOf("#/definitions/") == 0) {
|
||||
datatype = datatype.substring("#/definitions/".length());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Error obtaining the datatype (" + datatype + ") from ref:" + schema + ". Datatype default to Object");
|
||||
@ -1307,6 +1310,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
m.title = escapeText(schema.getTitle());
|
||||
m.description = escapeText(schema.getDescription());
|
||||
LOGGER.info("debugging fromModel: " + m.description);
|
||||
m.unescapedDescription = schema.getDescription();
|
||||
m.classname = toModelName(name);
|
||||
m.classVarName = toVarName(name);
|
||||
@ -3695,19 +3699,6 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
protected Schema getSchemaFromBody(RequestBody requestBody) {
|
||||
/*
|
||||
if (requestBody == null) {
|
||||
LOGGER.warn("requestBody is null in getSchemaFromBody");
|
||||
return null;
|
||||
}
|
||||
if (requestBody.getContent() == null) {
|
||||
LOGGER.warn("requestBody.getContent() is null in getSchemaFromBody");
|
||||
return null;
|
||||
}
|
||||
if ( requestBody.getContent().keySet() == null) {
|
||||
LOGGER.warn("requestBody.getContent().keySet() is null in getSchemaFromBody");
|
||||
return null;
|
||||
} */
|
||||
String contentType = new ArrayList<>(requestBody.getContent().keySet()).get(0);
|
||||
MediaType mediaType = requestBody.getContent().get(contentType);
|
||||
return mediaType.getSchema();
|
||||
@ -3875,7 +3866,10 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
protected String getSimpleRef(String ref) {
|
||||
if (ref.startsWith("#/components/")) {
|
||||
ref = ref.substring(ref.lastIndexOf("/") + 1);
|
||||
} else if (ref.startsWith("#/definitions/")) {
|
||||
ref = ref.substring(ref.lastIndexOf("/") + 1);
|
||||
}
|
||||
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,65 @@
|
||||
package org.openapitools.codegen;
|
||||
|
||||
import org.openapitools.codegen.options.OptionsProvider;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import mockit.FullVerifications;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class AbstractOptionsTest {
|
||||
private final OptionsProvider optionsProvider;
|
||||
|
||||
protected AbstractOptionsTest(OptionsProvider optionsProvider) {
|
||||
this.optionsProvider = optionsProvider;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Test
|
||||
public void checkOptionsProcessing() {
|
||||
getCodegenConfig().additionalProperties().putAll(optionsProvider.createOptions());
|
||||
setExpectations();
|
||||
|
||||
getCodegenConfig().processOpts();
|
||||
|
||||
new FullVerifications() {{
|
||||
}};
|
||||
}
|
||||
|
||||
@Test(description = "check if all options described in documentation are presented in test case")
|
||||
public void checkOptionsHelp() {
|
||||
final List<String> cliOptions = Lists.transform(getCodegenConfig().cliOptions(), getCliOptionTransformer());
|
||||
final Set<String> testOptions = optionsProvider.createOptions().keySet();
|
||||
final Set<String> skipped = new HashSet<String>(cliOptions);
|
||||
skipped.removeAll(testOptions);
|
||||
if (!skipped.isEmpty()) {
|
||||
Assert.fail(String.format("These options weren't checked: %s.", StringUtils.join(skipped, ", ")));
|
||||
}
|
||||
final Set<String> undocumented = new HashSet<String>(testOptions);
|
||||
undocumented.removeAll(cliOptions);
|
||||
if (!undocumented.isEmpty()) {
|
||||
Assert.fail(String.format("These options weren't documented: %s.", StringUtils.join(undocumented, ", ")));
|
||||
}
|
||||
}
|
||||
|
||||
private static Function<CliOption, String> getCliOptionTransformer() {
|
||||
return new Function<CliOption, String>() {
|
||||
@Override
|
||||
public String apply(CliOption option) {
|
||||
return option.getOpt();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected abstract CodegenConfig getCodegenConfig();
|
||||
|
||||
protected abstract void setExpectations();
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package org.openapitools.codegen.php;
|
||||
|
||||
import org.openapitools.codegen.CodegenParameter;
|
||||
import org.openapitools.codegen.languages.PhpClientCodegen;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class PhpClientExampleTest {
|
||||
@Test(description = "sets example value")
|
||||
public void exampleValueTest() {
|
||||
PhpClientCodegen clientCodegen = new PhpClientCodegen();
|
||||
CodegenParameter p = new CodegenParameter();
|
||||
p.baseType = "object";
|
||||
|
||||
clientCodegen.setParameterExampleValue(p);
|
||||
Assert.assertEquals(p.example, "new \\stdClass");
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package org.openapitools.codegen.php;
|
||||
|
||||
import org.openapitools.codegen.AbstractOptionsTest;
|
||||
import org.openapitools.codegen.CodegenConfig;
|
||||
import org.openapitools.codegen.languages.PhpClientCodegen;
|
||||
import org.openapitools.codegen.options.PhpClientOptionsProvider;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
public class PhpClientOptionsTest extends AbstractOptionsTest {
|
||||
|
||||
@Tested
|
||||
private PhpClientCodegen clientCodegen;
|
||||
|
||||
public PhpClientOptionsTest() {
|
||||
super(new PhpClientOptionsProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
new Expectations(clientCodegen) {{
|
||||
clientCodegen.setModelPackage(PhpClientOptionsProvider.MODEL_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setApiPackage(PhpClientOptionsProvider.API_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(PhpClientOptionsProvider.SORT_PARAMS_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setParameterNamingConvention(PhpClientOptionsProvider.VARIABLE_NAMING_CONVENTION_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setInvokerPackage(PhpClientOptionsProvider.INVOKER_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setPackagePath(PhpClientOptionsProvider.PACKAGE_PATH_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSrcBasePath(PhpClientOptionsProvider.SRC_BASE_PATH_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setComposerVendorName(PhpClientOptionsProvider.COMPOSER_VENDOR_NAME_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setGitUserId(PhpClientOptionsProvider.GIT_USER_ID_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setComposerProjectName(PhpClientOptionsProvider.COMPOSER_PROJECT_NAME_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setGitRepoId(PhpClientOptionsProvider.GIT_REPO_ID_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setArtifactVersion(PhpClientOptionsProvider.ARTIFACT_VERSION_VALUE);
|
||||
times = 1;
|
||||
}};
|
||||
}
|
||||
}
|
@ -0,0 +1,364 @@
|
||||
package org.openapitools.codegen.php;
|
||||
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenOperation;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.languages.PhpClientCodegen;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.media.*;
|
||||
import io.swagger.v3.parser.OpenAPIV3Parser;
|
||||
import io.swagger.v3.parser.util.SchemaTypeUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
public class PhpModelTest {
|
||||
protected static final Logger LOGGER = LoggerFactory.getLogger(PhpModelTest.class);
|
||||
|
||||
@Test(description = "convert a simple php model")
|
||||
public void simpleModelTest() {
|
||||
final Schema model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("id", new IntegerSchema())
|
||||
.addProperties("name", new StringSchema())
|
||||
.addProperties("createdAt", new DateTimeSchema())
|
||||
.addRequiredItem("id")
|
||||
.addRequiredItem("name");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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);
|
||||
// {{imports}} is not used in template
|
||||
//Assert.assertEquals(cm.imports.size(), 1);
|
||||
|
||||
final CodegenProperty property1 = cm.vars.get(0);
|
||||
Assert.assertEquals(property1.baseName, "id");
|
||||
Assert.assertEquals(property1.datatype, "int");
|
||||
Assert.assertEquals(property1.name, "id");
|
||||
Assert.assertEquals(property1.defaultValue, null);
|
||||
Assert.assertEquals(property1.baseType, "int");
|
||||
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.assertEquals(property2.defaultValue, null);
|
||||
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.complexType, "\\DateTime");
|
||||
Assert.assertEquals(property3.datatype, "\\DateTime");
|
||||
Assert.assertEquals(property3.name, "created_at");
|
||||
Assert.assertEquals(property3.defaultValue, null);
|
||||
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())
|
||||
.addProperties("urls", new ArraySchema()
|
||||
.items(new StringSchema()))
|
||||
.addRequiredItem("id");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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, "int");
|
||||
Assert.assertEquals(property1.name, "id");
|
||||
Assert.assertEquals(property1.defaultValue, null);
|
||||
Assert.assertEquals(property1.baseType, "int");
|
||||
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, "string[]");
|
||||
Assert.assertEquals(property2.name, "urls");
|
||||
Assert.assertEquals(property2.baseType, "array");
|
||||
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 model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("translations", new MapSchema()
|
||||
.additionalProperties(new StringSchema()))
|
||||
.addRequiredItem("id");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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, "translations");
|
||||
Assert.assertEquals(property1.datatype, "map[string,string]");
|
||||
Assert.assertEquals(property1.name, "translations");
|
||||
Assert.assertEquals(property1.baseType, "map");
|
||||
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 model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("children", new Schema().$ref("#/definitions/Children"));
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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, "\\Swagger\\Client\\Model\\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 complexListProperty() {
|
||||
final Schema model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("children", new ArraySchema()
|
||||
.items(new Schema().$ref("#/definitions/Children")));
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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, "\\Swagger\\Client\\Model\\Children[]");
|
||||
Assert.assertEquals(property1.name, "children");
|
||||
Assert.assertEquals(property1.baseType, "array");
|
||||
Assert.assertEquals(property1.containerType, "array");
|
||||
Assert.assertFalse(property1.required);
|
||||
Assert.assertTrue(property1.isContainer);
|
||||
}
|
||||
|
||||
@Test(description = "convert a model with complex map property")
|
||||
public void complexMapSchema() {
|
||||
final Schema model = new Schema()
|
||||
.description("a sample model")
|
||||
.addProperties("children", new MapSchema()
|
||||
.additionalProperties(new Schema().$ref("#/definitions/Children")));
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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);
|
||||
// {{imports}} is not used in template
|
||||
//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, "map[string,\\Swagger\\Client\\Model\\Children]");
|
||||
Assert.assertEquals(property1.name, "children");
|
||||
Assert.assertEquals(property1.baseType, "map");
|
||||
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 model = new ArraySchema()
|
||||
.items(new Schema().$ref("#/definitions/Children"))
|
||||
.description("an array model");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model);
|
||||
|
||||
Assert.assertEquals(model.getDescription(), "an array model");
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertTrue(cm.isArrayModel);
|
||||
Assert.assertEquals(cm.description, "an array model");
|
||||
Assert.assertEquals(cm.vars.size(), 0);
|
||||
// skip import test as import is not used by PHP codegen
|
||||
}
|
||||
|
||||
@Test(description = "convert an map model")
|
||||
public void mapModelTest() {
|
||||
final Schema model = new Schema()
|
||||
.description("a map model")
|
||||
.additionalProperties(new Schema().$ref("#/definitions/Children"));
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
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);
|
||||
}
|
||||
|
||||
@DataProvider(name = "modelNames")
|
||||
public static Object[][] primeNumbers() {
|
||||
return new Object[][] {
|
||||
{"sample", "Sample"},
|
||||
{"sample_name", "SampleName"},
|
||||
{"sample__name", "SampleName"},
|
||||
{"/sample", "Sample"},
|
||||
{"\\sample", "\\Sample"},
|
||||
{"sample.name", "SampleName"},
|
||||
{"_sample", "Sample"},
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "modelNames", description = "avoid inner class")
|
||||
public void modelNameTest(String name, String expectedName) {
|
||||
final Schema model = new Schema();
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel(name, model);
|
||||
|
||||
Assert.assertEquals(cm.name, name);
|
||||
Assert.assertEquals(cm.classname, expectedName);
|
||||
}
|
||||
|
||||
@Test(description = "test enum array model")
|
||||
public void enumArrayMdoelTest() {
|
||||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
final Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
|
||||
final Schema definition = schemas.get("EnumArrays");
|
||||
|
||||
Schema property = (Schema) definition.getProperties().get("array_enum");
|
||||
CodegenProperty prope = codegen.fromProperty("array_enum", property);
|
||||
codegen.updateCodegenPropertyEnum(prope);
|
||||
Assert.assertEquals(prope.datatypeWithEnum, "ARRAY_ENUM[]");
|
||||
Assert.assertEquals(prope.enumName, "ARRAY_ENUM");
|
||||
Assert.assertTrue(prope.isEnum);
|
||||
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList("fish", "crab"));
|
||||
|
||||
HashMap<String, String> fish= new HashMap<String, String>();
|
||||
fish.put("name", "FISH");
|
||||
fish.put("value", "\'fish\'");
|
||||
HashMap<String, String> crab= new HashMap<String, String>();
|
||||
crab.put("name", "CRAB");
|
||||
crab.put("value", "\'crab\'");
|
||||
Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(fish, crab));
|
||||
|
||||
// assert inner items
|
||||
Assert.assertEquals(prope.datatypeWithEnum, "ARRAY_ENUM[]");
|
||||
Assert.assertEquals(prope.enumName, "ARRAY_ENUM");
|
||||
Assert.assertTrue(prope.items.isEnum);
|
||||
Assert.assertEquals(prope.items.allowableValues.get("values"), Arrays.asList("fish", "crab"));
|
||||
Assert.assertEquals(prope.items.allowableValues.get("enumVars"), Arrays.asList(fish, crab));
|
||||
|
||||
}
|
||||
|
||||
@Test(description = "test enum model for values (numeric, string, etc)")
|
||||
public void enumMdoelValueTest() {
|
||||
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
final Schema definition = openAPI.getComponents().getSchemas().get("Enum_Test");
|
||||
|
||||
Schema property = (Schema) definition.getProperties().get("enum_integer");
|
||||
CodegenProperty prope = codegen.fromProperty("enum_integer", property);
|
||||
codegen.updateCodegenPropertyEnum(prope);
|
||||
Assert.assertEquals(prope.datatypeWithEnum, "ENUM_INTEGER");
|
||||
Assert.assertEquals(prope.enumName, "ENUM_INTEGER");
|
||||
Assert.assertTrue(prope.isEnum);
|
||||
Assert.assertFalse(prope.isContainer);
|
||||
Assert.assertNull(prope.items);
|
||||
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(1, -1));
|
||||
|
||||
HashMap<String, String> one = new HashMap<String, String>();
|
||||
one.put("name", "1");
|
||||
one.put("value", "1");
|
||||
HashMap<String, String> minusOne = new HashMap<String, String>();
|
||||
minusOne.put("name", "MINUS_1");
|
||||
minusOne.put("value", "-1");
|
||||
Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(one, minusOne));
|
||||
}
|
||||
|
||||
@Test(description = "test enum variable names for reserved words")
|
||||
public void testReservedWord() throws Exception {
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
Assert.assertEquals(codegen.toEnumVarName("public", null), "_PUBLIC");
|
||||
Assert.assertEquals(codegen.toEnumVarName("Private", null), "_PRIVATE");
|
||||
Assert.assertEquals(codegen.toEnumVarName("IF", null), "_IF");
|
||||
// should not escape non-reserved
|
||||
Assert.assertEquals(codegen.toEnumVarName("hello", null), "HELLO");
|
||||
}
|
||||
|
||||
@Test(description = "returns DateTime when using `--model-name-prefix`")
|
||||
public void dateTest() {
|
||||
final OpenAPI model = new OpenAPIV3Parser().read("src/test/resources/2_0/datePropertyTest.json");
|
||||
final DefaultCodegen codegen = new PhpClientCodegen();
|
||||
codegen.setModelNamePrefix("foo");
|
||||
|
||||
final String path = "/tests/dateResponse";
|
||||
final Operation p = model.getPaths().get(path).getPost();
|
||||
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getComponents().getSchemas());
|
||||
|
||||
Assert.assertEquals(op.returnType, "\\DateTime");
|
||||
|
||||
// datetime (or primitive type) not yet supported in HTTP request body
|
||||
//Assert.assertEquals(op.bodyParam.dataType, "\\DateTime");
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.openapitools.codegen.options;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface OptionsProvider {
|
||||
String getLanguage();
|
||||
Map<String, String> createOptions();
|
||||
boolean isServer();
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package org.openapitools.codegen.options;
|
||||
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.languages.PhpClientCodegen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class PhpClientOptionsProvider implements OptionsProvider {
|
||||
public static final String MODEL_PACKAGE_VALUE = "package";
|
||||
public static final String API_PACKAGE_VALUE = "apiPackage";
|
||||
public static final String SORT_PARAMS_VALUE = "false";
|
||||
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
|
||||
public static final String VARIABLE_NAMING_CONVENTION_VALUE = "snake_case";
|
||||
public static final String INVOKER_PACKAGE_VALUE = "Swagger\\Client\\Php";
|
||||
public static final String PACKAGE_PATH_VALUE = "SwaggerClient-php";
|
||||
public static final String SRC_BASE_PATH_VALUE = "libPhp";
|
||||
public static final String COMPOSER_VENDOR_NAME_VALUE = "swaggerPhp";
|
||||
public static final String COMPOSER_PROJECT_NAME_VALUE = "swagger-client-php";
|
||||
public static final String GIT_USER_ID_VALUE = "gitSwaggerPhp";
|
||||
public static final String GIT_REPO_ID_VALUE = "git-swagger-client-php";
|
||||
public static final String ARTIFACT_VERSION_VALUE = "1.0.0-SNAPSHOT";
|
||||
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 "php";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> createOptions() {
|
||||
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
|
||||
return builder.put(CodegenConstants.MODEL_PACKAGE, MODEL_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.API_PACKAGE, API_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
|
||||
.put(PhpClientCodegen.VARIABLE_NAMING_CONVENTION, VARIABLE_NAMING_CONVENTION_VALUE)
|
||||
.put(CodegenConstants.INVOKER_PACKAGE, INVOKER_PACKAGE_VALUE)
|
||||
.put(PhpClientCodegen.PACKAGE_PATH, PACKAGE_PATH_VALUE)
|
||||
.put(PhpClientCodegen.SRC_BASE_PATH, SRC_BASE_PATH_VALUE)
|
||||
.put(PhpClientCodegen.COMPOSER_VENDOR_NAME, COMPOSER_VENDOR_NAME_VALUE)
|
||||
.put(CodegenConstants.GIT_USER_ID, GIT_USER_ID_VALUE)
|
||||
.put(PhpClientCodegen.COMPOSER_PROJECT_NAME, COMPOSER_PROJECT_NAME_VALUE)
|
||||
.put(CodegenConstants.GIT_REPO_ID, GIT_REPO_ID_VALUE)
|
||||
.put(CodegenConstants.ARTIFACT_VERSION, ARTIFACT_VERSION_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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user