Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AnyType support to Java generators #6246

Merged
merged 5 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3110,9 +3110,15 @@ public CodegenProperty fromProperty(String name, Schema p) {
} else if (ModelUtils.isFreeFormObject(p)) {
property.isFreeFormObject = true;
property.baseType = getSchemaType(p);
if (languageSpecificPrimitives.contains(property.dataType)) {
property.isPrimitiveType = true;
}
} else if (ModelUtils.isAnyTypeSchema(p)) {
property.isAnyType = true;
property.baseType = getSchemaType(p);
if (languageSpecificPrimitives.contains(property.dataType)) {
property.isPrimitiveType = true;
}
} else { // model
setNonArrayMapProperty(property, type);
Schema refOrCurrent = ModelUtils.getReferencedSchema(this.openAPI, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import java.time.LocalDate;
import java.time.ZoneId;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -38,6 +35,8 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -82,7 +81,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected String licenseUrl = "http://unlicense.org";
protected String projectFolder = "src/main";
protected String projectTestFolder = "src/test";
protected String sourceFolder = projectFolder + File.separator +"java";
protected String sourceFolder = projectFolder + File.separator + "java";
protected String testFolder = projectTestFolder + "/java";
protected boolean fullJavaUtil;
protected boolean discriminatorCaseSensitive = true; // True if the discriminator value lookup should be case-sensitive.
Expand Down Expand Up @@ -169,6 +168,7 @@ public AbstractJavaCodegen() {
instantiationTypes.put("map", "HashMap");
typeMapping.put("date", "Date");
typeMapping.put("file", "File");
typeMapping.put("AnyType", "Object");

cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
Expand Down Expand Up @@ -389,12 +389,12 @@ public void processOpts() {
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
}
if (additionalProperties.containsKey(DISCRIMINATOR_CASE_SENSITIVE)) {
this.setDiscriminatorCaseSensitive(Boolean.valueOf(additionalProperties.get(DISCRIMINATOR_CASE_SENSITIVE).toString()));
this.setDiscriminatorCaseSensitive(Boolean.valueOf(additionalProperties.get(DISCRIMINATOR_CASE_SENSITIVE).toString()));
} else {
// By default, the discriminator lookup should be case sensitive. There is nothing in the OpenAPI specification
// that indicates the lookup should be case insensitive. However, some implementations perform
// a case-insensitive lookup.
this.setDiscriminatorCaseSensitive(Boolean.TRUE);
this.setDiscriminatorCaseSensitive(Boolean.TRUE);
}
additionalProperties.put(DISCRIMINATOR_CASE_SENSITIVE, this.discriminatorCaseSensitive);

Expand Down Expand Up @@ -843,12 +843,11 @@ public String toDefaultValue(Schema schema) {
} else if (ModelUtils.isStringSchema(schema)) {
if (schema.getDefault() != null) {
String _default;
if (schema.getDefault() instanceof Date){
if (schema.getDefault() instanceof Date) {
Date date = (Date) schema.getDefault();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return String.format(Locale.ROOT, localDate.toString(), "");
}
else{
} else {
_default = (String) schema.getDefault();
}
if (schema.getEnum() == null) {
Expand Down Expand Up @@ -1113,7 +1112,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}

// TODO: Setting additionalProperties is not the responsibility of this method. These side-effects should be moved elsewhere to prevent unexpected behaviors.
if(artifactVersion == null) {
if (artifactVersion == null) {
// If no artifactVersion is provided in additional properties, version from API specification is used.
// If none of them is provided then fallbacks to default version
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION) && additionalProperties.get(CodegenConstants.ARTIFACT_VERSION) != null) {
Expand Down Expand Up @@ -1445,7 +1444,7 @@ public void setFullJavaUtil(boolean fullJavaUtil) {

/**
* Set whether discriminator value lookup is case-sensitive or not.
*
*
* @param discriminatorCaseSensitive true if the discriminator value lookup should be case sensitive.
*/
public void setDiscriminatorCaseSensitive(boolean discriminatorCaseSensitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ public void testFreeFormObjects() {
codegen.setOpenAPI(openAPI);
CodegenModel cm1 = codegen.fromModel("MapTest1", test1);
Assert.assertEquals(cm1.getDataType(), "Map");
Assert.assertEquals(cm1.getParent(), "HashMap<String, oas_any_type_not_mapped>");
Assert.assertEquals(cm1.getParent(), "HashMap<String, Object>");
Assert.assertEquals(cm1.getClassname(), "MapTest1");

Schema test2 = openAPI.getComponents().getSchemas().get("MapTest2");
Expand Down Expand Up @@ -683,29 +683,29 @@ public void testAnyType() {

final CodegenProperty property1 = cm1.allVars.get(0);
Assert.assertEquals(property1.baseName, "any_value");
Assert.assertEquals(property1.dataType, "oas_any_type_not_mapped");
Assert.assertEquals(property1.dataType, "Object");
Assert.assertTrue(property1.hasMore);
Assert.assertFalse(property1.isPrimitiveType);
Assert.assertTrue(property1.isPrimitiveType);
Assert.assertFalse(property1.isContainer);
Assert.assertFalse(property1.isFreeFormObject);
Assert.assertTrue(property1.isAnyType);

final CodegenProperty property2 = cm1.allVars.get(1);
Assert.assertEquals(property2.baseName, "any_value_with_desc");
Assert.assertEquals(property2.dataType, "oas_any_type_not_mapped");
Assert.assertEquals(property2.dataType, "Object");
Assert.assertTrue(property2.hasMore);
Assert.assertFalse(property2.required);
Assert.assertFalse(property2.isPrimitiveType);
Assert.assertTrue(property2.isPrimitiveType);
Assert.assertFalse(property2.isContainer);
Assert.assertFalse(property2.isFreeFormObject);
Assert.assertTrue(property2.isAnyType);

final CodegenProperty property3 = cm1.allVars.get(2);
Assert.assertEquals(property3.baseName, "any_value_nullable");
Assert.assertEquals(property3.dataType, "oas_any_type_not_mapped");
Assert.assertEquals(property3.dataType, "Object");
Assert.assertFalse(property3.hasMore);
Assert.assertFalse(property3.required);
Assert.assertFalse(property3.isPrimitiveType);
Assert.assertTrue(property3.isPrimitiveType);
Assert.assertFalse(property3.isContainer);
Assert.assertFalse(property3.isFreeFormObject);
Assert.assertTrue(property3.isAnyType);
Expand All @@ -717,63 +717,63 @@ public void testAnyType() {

final CodegenProperty cp1 = cm2.vars.get(0);
Assert.assertEquals(cp1.baseName, "any_value");
Assert.assertEquals(cp1.dataType, "oas_any_type_not_mapped");
Assert.assertEquals(cp1.dataType, "Object");
Assert.assertTrue(cp1.hasMore);
Assert.assertFalse(cp1.required);
Assert.assertFalse(cp1.isPrimitiveType);
Assert.assertTrue(cp1.isPrimitiveType);
Assert.assertFalse(cp1.isContainer);
Assert.assertFalse(cp1.isFreeFormObject);
Assert.assertTrue(cp1.isAnyType);

final CodegenProperty cp2 = cm2.vars.get(1);
Assert.assertEquals(cp2.baseName, "any_value_with_desc");
Assert.assertEquals(cp2.dataType, "oas_any_type_not_mapped");
Assert.assertEquals(cp2.dataType, "Object");
Assert.assertTrue(cp2.hasMore);
Assert.assertFalse(cp2.required);
Assert.assertFalse(cp2.isPrimitiveType);
Assert.assertTrue(cp2.isPrimitiveType);
Assert.assertFalse(cp2.isContainer);
Assert.assertFalse(cp2.isFreeFormObject);
Assert.assertTrue(cp2.isAnyType);

final CodegenProperty cp3 = cm2.vars.get(2);
Assert.assertEquals(cp3.baseName, "any_value_nullable");
Assert.assertEquals(cp3.dataType, "oas_any_type_not_mapped");
Assert.assertEquals(cp3.dataType, "Object");
Assert.assertTrue(cp3.hasMore);
Assert.assertFalse(cp3.required);
Assert.assertFalse(cp3.isPrimitiveType);
Assert.assertTrue(cp3.isPrimitiveType);
Assert.assertFalse(cp3.isContainer);
Assert.assertFalse(cp3.isFreeFormObject);
Assert.assertTrue(cp3.isAnyType);

// map
final CodegenProperty cp4 = cm2.vars.get(3);
Assert.assertEquals(cp4.baseName, "map_any_value");
Assert.assertEquals(cp4.dataType, "Map<String, oas_any_type_not_mapped>");
Assert.assertEquals(cp4.dataType, "Map<String, Object>");
Assert.assertTrue(cp4.hasMore);
Assert.assertFalse(cp4.required);
Assert.assertFalse(cp4.isPrimitiveType);
Assert.assertTrue(cp4.isPrimitiveType);
Assert.assertTrue(cp4.isContainer);
Assert.assertTrue(cp4.isMapContainer);
Assert.assertTrue(cp4.isFreeFormObject);
Assert.assertFalse(cp4.isAnyType);

final CodegenProperty cp5 = cm2.vars.get(4);
Assert.assertEquals(cp5.baseName, "map_any_value_with_desc");
Assert.assertEquals(cp5.dataType, "Map<String, oas_any_type_not_mapped>");
Assert.assertEquals(cp5.dataType, "Map<String, Object>");
Assert.assertTrue(cp5.hasMore);
Assert.assertFalse(cp5.required);
Assert.assertFalse(cp5.isPrimitiveType);
Assert.assertTrue(cp5.isPrimitiveType);
Assert.assertTrue(cp5.isContainer);
Assert.assertTrue(cp5.isMapContainer);
Assert.assertTrue(cp5.isFreeFormObject);
Assert.assertFalse(cp5.isAnyType);

final CodegenProperty cp6 = cm2.vars.get(5);
Assert.assertEquals(cp6.baseName, "map_any_value_nullable");
Assert.assertEquals(cp6.dataType, "Map<String, oas_any_type_not_mapped>");
Assert.assertEquals(cp6.dataType, "Map<String, Object>");
Assert.assertTrue(cp6.hasMore);
Assert.assertFalse(cp6.required);
Assert.assertFalse(cp6.isPrimitiveType);
Assert.assertTrue(cp6.isPrimitiveType);
Assert.assertTrue(cp6.isContainer);
Assert.assertTrue(cp6.isMapContainer);
Assert.assertTrue(cp6.isFreeFormObject);
Expand All @@ -782,32 +782,32 @@ public void testAnyType() {
// array
final CodegenProperty cp7 = cm2.vars.get(6);
Assert.assertEquals(cp7.baseName, "array_any_value");
Assert.assertEquals(cp7.dataType, "List<oas_any_type_not_mapped>");
Assert.assertEquals(cp7.dataType, "List<Object>");
Assert.assertTrue(cp7.hasMore);
Assert.assertFalse(cp7.required);
Assert.assertFalse(cp7.isPrimitiveType);
Assert.assertTrue(cp7.isPrimitiveType);
Assert.assertTrue(cp7.isContainer);
Assert.assertTrue(cp7.isListContainer);
Assert.assertFalse(cp7.isFreeFormObject);
Assert.assertFalse(cp7.isAnyType);

final CodegenProperty cp8 = cm2.vars.get(7);
Assert.assertEquals(cp8.baseName, "array_any_value_with_desc");
Assert.assertEquals(cp8.dataType, "List<oas_any_type_not_mapped>");
Assert.assertEquals(cp8.dataType, "List<Object>");
Assert.assertTrue(cp8.hasMore);
Assert.assertFalse(cp8.required);
Assert.assertFalse(cp8.isPrimitiveType);
Assert.assertTrue(cp8.isPrimitiveType);
Assert.assertTrue(cp8.isContainer);
Assert.assertTrue(cp8.isListContainer);
Assert.assertFalse(cp8.isFreeFormObject);
Assert.assertFalse(cp8.isAnyType);

final CodegenProperty cp9 = cm2.vars.get(8);
Assert.assertEquals(cp9.baseName, "array_any_value_nullable");
Assert.assertEquals(cp9.dataType, "List<oas_any_type_not_mapped>");
Assert.assertEquals(cp9.dataType, "List<Object>");
Assert.assertFalse(cp9.hasMore);
Assert.assertFalse(cp9.required);
Assert.assertFalse(cp9.isPrimitiveType);
Assert.assertTrue(cp9.isPrimitiveType);
Assert.assertTrue(cp9.isContainer);
Assert.assertTrue(cp9.isListContainer);
Assert.assertFalse(cp9.isFreeFormObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Name | Type | Description | Notes
**MapArrayAnytype** | **Dictionary&lt;string, List&lt;Object&gt;&gt;** | | [optional]
**MapMapString** | **Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;** | | [optional]
**MapMapAnytype** | **Dictionary&lt;string, Dictionary&lt;string, Object&gt;&gt;** | | [optional]
**Anytype1** | [**Object**](.md) | | [optional]
**Anytype2** | [**Object**](.md) | | [optional]
**Anytype3** | [**Object**](.md) | | [optional]
**Anytype1** | **Object** | | [optional]
**Anytype2** | **Object** | | [optional]
**Anytype3** | **Object** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Name | Type | Description | Notes
**MapArrayAnytype** | **Dictionary&lt;string, List&lt;Object&gt;&gt;** | | [optional]
**MapMapString** | **Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;** | | [optional]
**MapMapAnytype** | **Dictionary&lt;string, Dictionary&lt;string, Object&gt;&gt;** | | [optional]
**Anytype1** | [**Object**](.md) | | [optional]
**Anytype2** | [**Object**](.md) | | [optional]
**Anytype3** | [**Object**](.md) | | [optional]
**Anytype1** | **Object** | | [optional]
**Anytype2** | **Object** | | [optional]
**Anytype3** | **Object** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Name | Type | Description | Notes
**MapArrayAnytype** | **Dictionary&lt;string, List&lt;Object&gt;&gt;** | | [optional]
**MapMapString** | **Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;** | | [optional]
**MapMapAnytype** | **Dictionary&lt;string, Dictionary&lt;string, Object&gt;&gt;** | | [optional]
**Anytype1** | [**Object**](.md) | | [optional]
**Anytype2** | [**Object**](.md) | | [optional]
**Anytype3** | [**Object**](.md) | | [optional]
**Anytype1** | **Object** | | [optional]
**Anytype2** | **Object** | | [optional]
**Anytype3** | **Object** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models)
[[Back to API list]](../README.md#documentation-for-api-endpoints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ defmodule OpenapiPetstore.Model.AdditionalPropertiesClass do
end

defimpl Poison.Decoder, for: OpenapiPetstore.Model.AdditionalPropertiesClass do
import OpenapiPetstore.Deserializer
def decode(value, options) do
def decode(value, _options) do
value
|> deserialize(:"anytype_1", :struct, OpenapiPetstore.Model.Map, options)
|> deserialize(:"anytype_2", :struct, OpenapiPetstore.Model.Map, options)
|> deserialize(:"anytype_3", :struct, OpenapiPetstore.Model.Map, options)
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Name | Type | Description | Notes
**MapArrayAnytype** | Pointer to [**map[string][]map[string]interface{}**](array.md) | | [optional]
**MapMapString** | Pointer to [**map[string]map[string]string**](map.md) | | [optional]
**MapMapAnytype** | Pointer to [**map[string]map[string]map[string]interface{}**](map.md) | | [optional]
**Anytype1** | Pointer to [**map[string]interface{}**](.md) | | [optional]
**Anytype2** | Pointer to [**map[string]interface{}**](.md) | | [optional]
**Anytype3** | Pointer to [**map[string]interface{}**](.md) | | [optional]
**Anytype1** | Pointer to **map[string]interface{}** | | [optional]
**Anytype2** | Pointer to **map[string]interface{}** | | [optional]
**Anytype3** | Pointer to **map[string]interface{}** | | [optional]

## Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Name | Type | Description | Notes
**MapArrayAnytype** | [**map[string][]map[string]interface{}**](array.md) | | [optional]
**MapMapString** | [**map[string]map[string]string**](map.md) | | [optional]
**MapMapAnytype** | [**map[string]map[string]map[string]interface{}**](map.md) | | [optional]
**Anytype1** | [**map[string]interface{}**](.md) | | [optional]
**Anytype2** | [**map[string]interface{}**](.md) | | [optional]
**Anytype3** | [**map[string]interface{}**](.md) | | [optional]
**Anytype1** | **map[string]interface{}** | | [optional]
**Anytype2** | **map[string]interface{}** | | [optional]
**Anytype3** | **map[string]interface{}** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Name | Type | Description | Notes
**MapArrayAnytype** | [**map[string][]map[string]interface{}**](array.md) | | [optional]
**MapMapString** | [**map[string]map[string]string**](map.md) | | [optional]
**MapMapAnytype** | [**map[string]map[string]map[string]interface{}**](map.md) | | [optional]
**Anytype1** | [**map[string]interface{}**](.md) | | [optional]
**Anytype2** | [**map[string]interface{}**](.md) | | [optional]
**Anytype3** | [**map[string]interface{}**](.md) | | [optional]
**Anytype1** | **map[string]interface{}** | | [optional]
**Anytype2** | **map[string]interface{}** | | [optional]
**Anytype3** | **map[string]interface{}** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Name | Type | Description | Notes
**mapArrayAnytype** | [**Map&lt;String, List&lt;Object&gt;&gt;**](List.md) | | [optional]
**mapMapString** | [**Map&lt;String, Map&lt;String, String&gt;&gt;**](Map.md) | | [optional]
**mapMapAnytype** | [**Map&lt;String, Map&lt;String, Object&gt;&gt;**](Map.md) | | [optional]
**anytype1** | [**Object**](.md) | | [optional]
**anytype2** | [**Object**](.md) | | [optional]
**anytype3** | [**Object**](.md) | | [optional]
**anytype1** | **Object** | | [optional]
**anytype2** | **Object** | | [optional]
**anytype3** | **Object** | | [optional]



Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Name | Type | Description | Notes
**mapArrayAnytype** | [**Map&lt;String, List&lt;Object&gt;&gt;**](List.md) | | [optional]
**mapMapString** | [**Map&lt;String, Map&lt;String, String&gt;&gt;**](Map.md) | | [optional]
**mapMapAnytype** | [**Map&lt;String, Map&lt;String, Object&gt;&gt;**](Map.md) | | [optional]
**anytype1** | [**Object**](.md) | | [optional]
**anytype2** | [**Object**](.md) | | [optional]
**anytype3** | [**Object**](.md) | | [optional]
**anytype1** | **Object** | | [optional]
**anytype2** | **Object** | | [optional]
**anytype3** | **Object** | | [optional]



Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.1-SNAPSHOT
5.0.0-SNAPSHOT
Loading