Add support for isFreeFormObject flag (#16127)

* correctly set the free form object flag

* update

* better code format
This commit is contained in:
William Cheng
2023-07-20 14:10:12 +08:00
committed by GitHub
parent bfcd646356
commit a729cb4e09
7 changed files with 61 additions and 9 deletions

View File

@@ -66,7 +66,8 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public String defaultValue;
public String arrayModelType;
public boolean isAlias; // Is this effectively an alias of another simple type
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime, isDecimal, isShort, isUnboundedInteger, isPrimitiveType, isBoolean;
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime,
isDecimal, isShort, isUnboundedInteger, isPrimitiveType, isBoolean, isFreeFormObject;
private boolean additionalPropertiesIsAnyType;
public List<CodegenProperty> vars = new ArrayList<>(); // all properties (without parent's properties)
public List<CodegenProperty> allVars = new ArrayList<>(); // all properties (with parent's properties)
@@ -961,6 +962,16 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
this.isAnyType = isAnyType;
}
@Override
public boolean getIsFreeFormObject() {
return isFreeFormObject;
}
@Override
public void setIsFreeFormObject(boolean isFreeFormObject) {
this.isFreeFormObject = isFreeFormObject;
}
public boolean getIsUuid() { return isUuid; }
public void setIsUuid(boolean isUuid) { this.isUuid = isUuid; }

View File

@@ -914,6 +914,16 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
this.isAnyType = isAnyType;
}
@Override
public boolean getIsFreeFormObject() {
return isFreeFormObject;
}
@Override
public void setIsFreeFormObject(boolean isFreeFormObject) {
this.isFreeFormObject = isFreeFormObject;
}
@Override
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
this.composedSchemas = composedSchemas;

View File

@@ -989,6 +989,16 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
this.isAnyType = isAnyType;
}
@Override
public boolean getIsFreeFormObject() {
return isFreeFormObject;
}
@Override
public void setIsFreeFormObject(boolean isFreeFormObject) {
this.isFreeFormObject = isFreeFormObject;
}
@Override
public boolean getHasMultipleTypes() {
return hasMultipleTypes;

View File

@@ -734,6 +734,16 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
this.isAnyType = isAnyType;
}
@Override
public boolean getIsFreeFormObject() {
return isFreeFormObject;
}
@Override
public void setIsFreeFormObject(boolean isFreeFormObject) {
this.isFreeFormObject = isFreeFormObject;
}
@Override
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
this.composedSchemas = composedSchemas;

View File

@@ -189,6 +189,10 @@ public interface IJsonSchemaValidationProperties {
void setIsAnyType(boolean isAnyType);
boolean getIsFreeFormObject();
void setIsFreeFormObject(boolean isFreeFormObject);
String getRef();
void setRef(String ref);
@@ -223,11 +227,12 @@ public interface IJsonSchemaValidationProperties {
* Syncs all the schema's type properties into the IJsonSchemaValidationProperties instance
* for now this only supports types without format information
* TODO: in the future move the format handling in here too
*
* @param p the schema which contains the type info
*/
default void setTypeProperties(Schema p) {
if (ModelUtils.isModelWithPropertiesOnly(p)) {
setIsModel(true);
setIsModel(true);
} else if (ModelUtils.isArraySchema(p)) {
setIsArray(true);
} else if (ModelUtils.isFileSchema(p) && !ModelUtils.isStringSchema(p)) {
@@ -249,7 +254,7 @@ public interface IJsonSchemaValidationProperties {
} else if (ModelUtils.isEmailSchema(p)) {
;
} else if (ModelUtils.isPasswordSchema(p)) {
;
;
} else if (ModelUtils.isDateSchema(p)) {
;
} else if (ModelUtils.isDateTimeSchema(p)) {
@@ -279,6 +284,10 @@ public interface IJsonSchemaValidationProperties {
setIsNull(true);
} else if (ModelUtils.isAnyType(p)) {
setIsAnyType(true);
} else if (ModelUtils.isFreeFormObject(p)) {
setIsFreeFormObject(true);
// TODO: remove below later after updating generators to properly use isFreeFormObject
setIsMap(true);
} else if (ModelUtils.isTypeObjectSchema(p)) {
setIsMap(true);
}
@@ -289,21 +298,21 @@ public interface IJsonSchemaValidationProperties {
*/
default String getBaseType() {
return null;
};
}
/**
* @return complex type that can contain type parameters - like {@code List<Items>} for Java
*/
default String getComplexType() {
return getBaseType();
};
}
/**
* Recursively collect all necessary imports to include so that the type may be resolved.
*
* @param importContainerType whether or not to include the container types in the returned imports.
* @param importBaseType whether or not to include the base types in the returned imports.
* @param featureSet the generator feature set, used to determine if composed schemas should be added
* @param importBaseType whether or not to include the base types in the returned imports.
* @param featureSet the generator feature set, used to determine if composed schemas should be added
* @return all of the imports
*/
default Set<String> getImports(boolean importContainerType, boolean importBaseType, FeatureSet featureSet) {

View File

@@ -872,7 +872,7 @@ public class JavaClientCodegenTest {
// map
// Should allow in any type including map, https://github.com/swagger-api/swagger-parser/issues/1603
final CodegenProperty cp4 = cm2.vars.get(3);
Assert.assertEquals(cp4.baseName, "map_any_value");
Assert.assertEquals(cp4.baseName, "map_free_form_object");
Assert.assertEquals(cp4.dataType, "Map<String, Object>");
Assert.assertFalse(cp4.required);
Assert.assertTrue(cp4.isPrimitiveType);
@@ -880,6 +880,7 @@ public class JavaClientCodegenTest {
Assert.assertTrue(cp4.isMap);
Assert.assertTrue(cp4.isFreeFormObject);
Assert.assertFalse(cp4.isAnyType);
Assert.assertFalse(cp4.isModel);
// Should allow in any type including map, https://github.com/swagger-api/swagger-parser/issues/1603
final CodegenProperty cp5 = cm2.vars.get(4);
@@ -891,6 +892,7 @@ public class JavaClientCodegenTest {
Assert.assertTrue(cp5.isMap);
Assert.assertTrue(cp5.isFreeFormObject);
Assert.assertFalse(cp5.isAnyType);
Assert.assertFalse(cp5.isModel);
// Should allow in any type including map, https://github.com/swagger-api/swagger-parser/issues/1603
final CodegenProperty cp6 = cm2.vars.get(5);

View File

@@ -39,7 +39,7 @@ components:
any_value_nullable:
nullable: true
description: inline any value nullable
map_any_value:
map_free_form_object:
additionalProperties: {}
map_any_value_with_desc:
additionalProperties: