Skip to content

Commit

Permalink
[Default] update isAdditionalPropertiesTrue tag to cover more types (#…
Browse files Browse the repository at this point in the history
…16227)

* enhance additional properties support

* update samples

* update tests

* add more tests

* update samples

* fix samples
  • Loading branch information
wing328 committed Aug 5, 2023
1 parent c080660 commit f6fb838
Show file tree
Hide file tree
Showing 142 changed files with 2,966 additions and 333 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public String additionalPropertiesType;

/**
* True if additionalProperties is set to true (boolean value)
* True if additionalProperties is set to true (boolean value), any type, free form object, etc
*
* TODO: we may rename this to isAdditionalPropertiesEnabled or something
* else to avoid confusions
*/
public boolean isAdditionalPropertiesTrue;

Expand Down Expand Up @@ -1088,6 +1091,7 @@ public boolean equals(Object o) {
Objects.equals(externalDocumentation, that.externalDocumentation) &&
Objects.equals(vendorExtensions, that.vendorExtensions) &&
Objects.equals(additionalPropertiesType, that.additionalPropertiesType) &&
Objects.equals(isAdditionalPropertiesTrue, that.isAdditionalPropertiesTrue) &&
Objects.equals(getMaxProperties(), that.getMaxProperties()) &&
Objects.equals(getMinProperties(), that.getMinProperties()) &&
Objects.equals(getMaxItems(), that.getMaxItems()) &&
Expand Down Expand Up @@ -1193,6 +1197,7 @@ public String toString() {
sb.append(", externalDocumentation=").append(externalDocumentation);
sb.append(", vendorExtensions=").append(vendorExtensions);
sb.append(", additionalPropertiesType='").append(additionalPropertiesType).append('\'');
sb.append(", isAdditionalPropertiesTrue='").append(isAdditionalPropertiesTrue).append('\'');
sb.append(", maxProperties=").append(maxProperties);
sb.append(", minProperties=").append(minProperties);
sb.append(", uniqueItems=").append(uniqueItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3179,6 +3179,9 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
// if we are trying to set additionalProperties on an empty schema stop recursing
return;
}
// Note: This flag is set to true if additioanl properties
// is set (any type, free form object, boolean true, string, etc).
// The variable name may be renamed later to avoid confusion.
boolean additionalPropertiesIsAnyType = false;
CodegenModel m = null;
if (property instanceof CodegenModel) {
Expand All @@ -3199,15 +3202,14 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
additionalPropertiesIsAnyType = true;
}
} else {
// if additioanl properties is set (e.g. free form object, any type, string, etc)
addPropProp = fromProperty(getAdditionalPropertiesName(), (Schema) schema.getAdditionalProperties(), false);
if (ModelUtils.isAnyType((Schema) schema.getAdditionalProperties())) {
additionalPropertiesIsAnyType = true;
}
additionalPropertiesIsAnyType = true;
}
if (additionalPropertiesIsAnyType) {
property.setAdditionalPropertiesIsAnyType(true);
}
if (m != null && isAdditionalPropertiesTrue) {
if (m != null && (isAdditionalPropertiesTrue || additionalPropertiesIsAnyType)) {
m.isAdditionalPropertiesTrue = true;
}
if (ModelUtils.isComposedSchema(schema) && !supportsAdditionalPropertiesWithComposedSchema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2616,7 +2616,7 @@ public void testAdditionalPropertiesPresentInModels() {
cm = codegen.fromModel(modelName, sc);
CodegenProperty stringCp = codegen.fromProperty("additional_properties", new Schema().type("string"));
assertEquals(cm.getAdditionalProperties(), stringCp);
assertFalse(cm.getAdditionalPropertiesIsAnyType());
assertTrue(cm.getAdditionalPropertiesIsAnyType());
}

@Test
Expand Down Expand Up @@ -2656,7 +2656,7 @@ public void testAdditionalPropertiesPresentInModelProperties() {
assertFalse(mapWithAddPropsFalse.getAdditionalPropertiesIsAnyType());
mapWithAddPropsSchema = cm.getVars().get(3);
assertEquals(mapWithAddPropsSchema.getAdditionalProperties(), stringCp);
assertFalse(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());
assertTrue(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());

modelName = "ObjectModelWithAddPropsInProps";
sc = openAPI.getComponents().getSchemas().get(modelName);
Expand All @@ -2672,7 +2672,7 @@ public void testAdditionalPropertiesPresentInModelProperties() {
assertFalse(mapWithAddPropsFalse.getAdditionalPropertiesIsAnyType());
mapWithAddPropsSchema = cm.getVars().get(3);
assertEquals(mapWithAddPropsSchema.getAdditionalProperties(), stringCp);
assertFalse(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());
assertTrue(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());

if (isGenerateAliasAsModel) { // restore the setting
GlobalSettings.setProperty("generateAliasAsModel", "true");
Expand Down Expand Up @@ -2717,7 +2717,7 @@ public void testAdditionalPropertiesPresentInParameters() {
assertFalse(mapWithAddPropsFalse.getAdditionalPropertiesIsAnyType());
mapWithAddPropsSchema = co.queryParams.get(3);
assertEquals(mapWithAddPropsSchema.getAdditionalProperties(), stringCp);
assertFalse(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());
assertTrue(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());

path = "/additional_properties/";
operation = openAPI.getPaths().get(path).getPost();
Expand All @@ -2733,7 +2733,7 @@ public void testAdditionalPropertiesPresentInParameters() {
assertFalse(mapWithAddPropsFalse.getAdditionalPropertiesIsAnyType());
mapWithAddPropsSchema = co.queryParams.get(3);
assertEquals(mapWithAddPropsSchema.getAdditionalProperties(), stringCp);
assertFalse(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());
assertTrue(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());

if (isGenerateAliasAsModel) { // restore the setting
GlobalSettings.setProperty("generateAliasAsModel", "true");
Expand Down Expand Up @@ -2778,7 +2778,7 @@ public void testAdditionalPropertiesPresentInResponses() {
assertFalse(mapWithAddPropsFalse.getAdditionalPropertiesIsAnyType());
mapWithAddPropsSchema = co.responses.get(3);
assertEquals(mapWithAddPropsSchema.getAdditionalProperties(), stringCp);
assertFalse(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());
assertTrue(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());

path = "/additional_properties/";
operation = openAPI.getPaths().get(path).getPost();
Expand All @@ -2794,7 +2794,7 @@ public void testAdditionalPropertiesPresentInResponses() {
assertFalse(mapWithAddPropsFalse.getAdditionalPropertiesIsAnyType());
mapWithAddPropsSchema = co.responses.get(3);
assertEquals(mapWithAddPropsSchema.getAdditionalProperties(), stringCp);
assertFalse(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());
assertTrue(mapWithAddPropsSchema.getAdditionalPropertiesIsAnyType());

if (isGenerateAliasAsModel) { // restore the setting
GlobalSettings.setProperty("generateAliasAsModel", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2272,4 +2272,23 @@ components:
properties:
skill:
type: string

AdditionalPropertiesObject:
type: object
properties:
name:
type: string
additionalProperties:
type: object
AdditionalPropertiesAnyType:
type: object
properties:
name:
type: string
additionalProperties: {}
AdditionalPropertiesWithDescriptionOnly:
type: object
properties:
name:
type: string
additionalProperties:
description: This is what the additional property is
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public partial class Drawing : Dictionary<String, Fruit>, IEquatable<Drawing>, I
{
this._flagShapes = true;
}
this.AdditionalProperties = new Dictionary<string, object>();
}

/// <summary>
Expand Down Expand Up @@ -159,6 +160,12 @@ public bool ShouldSerializeShapes()
{
return _flagShapes;
}
/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public IDictionary<string, object> AdditionalProperties { get; set; }

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -172,6 +179,7 @@ public override string ToString()
sb.Append(" ShapeOrNull: ").Append(ShapeOrNull).Append("\n");
sb.Append(" NullableShape: ").Append(NullableShape).Append("\n");
sb.Append(" Shapes: ").Append(Shapes).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down Expand Up @@ -230,6 +238,10 @@ public override int GetHashCode()
{
hashCode = (hashCode * 59) + this.Shapes.GetHashCode();
}
if (this.AdditionalProperties != null)
{
hashCode = (hashCode * 59) + this.AdditionalProperties.GetHashCode();
}
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public partial class NullableClass : Dictionary<String, Object>, IEquatable<Null
{
this._flagObjectItemsNullable = true;
}
this.AdditionalProperties = new Dictionary<string, object>();
}

/// <summary>
Expand Down Expand Up @@ -400,6 +401,12 @@ public bool ShouldSerializeObjectItemsNullable()
{
return _flagObjectItemsNullable;
}
/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public IDictionary<string, object> AdditionalProperties { get; set; }

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -421,6 +428,7 @@ public override string ToString()
sb.Append(" ObjectNullableProp: ").Append(ObjectNullableProp).Append("\n");
sb.Append(" ObjectAndItemsNullableProp: ").Append(ObjectAndItemsNullableProp).Append("\n");
sb.Append(" ObjectItemsNullable: ").Append(ObjectItemsNullable).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down Expand Up @@ -511,6 +519,10 @@ public override int GetHashCode()
{
hashCode = (hashCode * 59) + this.ObjectItemsNullable.GetHashCode();
}
if (this.AdditionalProperties != null)
{
hashCode = (hashCode * 59) + this.AdditionalProperties.GetHashCode();
}
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public Drawing(Shape mainShape, List<Shape> shapes, NullableShape? nullableShape
[JsonPropertyName("shapeOrNull")]
public ShapeOrNull? ShapeOrNull { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalProperties { get; } = new Dictionary<string, JsonElement>();

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -86,6 +92,7 @@ public override string ToString()
sb.Append(" Shapes: ").Append(Shapes).Append("\n");
sb.Append(" NullableShape: ").Append(NullableShape).Append("\n");
sb.Append(" ShapeOrNull: ").Append(ShapeOrNull).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public NullableClass(List<Object> arrayItemsNullable, Dictionary<string, Object>
[JsonPropertyName("string_prop")]
public string? StringProp { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalProperties { get; } = new Dictionary<string, JsonElement>();

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -158,6 +164,7 @@ public override string ToString()
sb.Append(" ObjectAndItemsNullableProp: ").Append(ObjectAndItemsNullableProp).Append("\n");
sb.Append(" ObjectNullableProp: ").Append(ObjectNullableProp).Append("\n");
sb.Append(" StringProp: ").Append(StringProp).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public Drawing(Shape mainShape, List<Shape> shapes, NullableShape nullableShape
[JsonPropertyName("shapeOrNull")]
public ShapeOrNull ShapeOrNull { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalProperties { get; } = new Dictionary<string, JsonElement>();

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -84,6 +90,7 @@ public override string ToString()
sb.Append(" Shapes: ").Append(Shapes).Append("\n");
sb.Append(" NullableShape: ").Append(NullableShape).Append("\n");
sb.Append(" ShapeOrNull: ").Append(ShapeOrNull).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public NullableClass(List<Object> arrayItemsNullable, Dictionary<string, Object>
[JsonPropertyName("string_prop")]
public string StringProp { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalProperties { get; } = new Dictionary<string, JsonElement>();

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -156,6 +162,7 @@ public override string ToString()
sb.Append(" ObjectAndItemsNullableProp: ").Append(ObjectAndItemsNullableProp).Append("\n");
sb.Append(" ObjectNullableProp: ").Append(ObjectNullableProp).Append("\n");
sb.Append(" StringProp: ").Append(StringProp).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public Drawing(Shape mainShape, List<Shape> shapes, NullableShape nullableShape
[JsonPropertyName("shapeOrNull")]
public ShapeOrNull ShapeOrNull { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalProperties { get; } = new Dictionary<string, JsonElement>();

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -84,6 +90,7 @@ public override string ToString()
sb.Append(" Shapes: ").Append(Shapes).Append("\n");
sb.Append(" NullableShape: ").Append(NullableShape).Append("\n");
sb.Append(" ShapeOrNull: ").Append(ShapeOrNull).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public NullableClass(List<Object> arrayItemsNullable, Dictionary<string, Object>
[JsonPropertyName("string_prop")]
public string StringProp { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> AdditionalProperties { get; } = new Dictionary<string, JsonElement>();

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -156,6 +162,7 @@ public override string ToString()
sb.Append(" ObjectAndItemsNullableProp: ").Append(ObjectAndItemsNullableProp).Append("\n");
sb.Append(" ObjectNullableProp: ").Append(ObjectNullableProp).Append("\n");
sb.Append(" StringProp: ").Append(StringProp).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public partial class Drawing : Dictionary<String, Fruit>, IEquatable<Drawing>, I
this.ShapeOrNull = shapeOrNull;
this.NullableShape = nullableShape;
this.Shapes = shapes;
this.AdditionalProperties = new Dictionary<string, object>();
}

/// <summary>
Expand All @@ -72,6 +73,12 @@ public partial class Drawing : Dictionary<String, Fruit>, IEquatable<Drawing>, I
[DataMember(Name = "shapes", EmitDefaultValue = false)]
public List<Shape> Shapes { get; set; }

/// <summary>
/// Gets or Sets additional properties
/// </summary>
[JsonExtensionData]
public IDictionary<string, object> AdditionalProperties { get; set; }

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand All @@ -85,6 +92,7 @@ public override string ToString()
sb.Append(" ShapeOrNull: ").Append(ShapeOrNull).Append("\n");
sb.Append(" NullableShape: ").Append(NullableShape).Append("\n");
sb.Append(" Shapes: ").Append(Shapes).Append("\n");
sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand Down Expand Up @@ -143,6 +151,10 @@ public override int GetHashCode()
{
hashCode = (hashCode * 59) + this.Shapes.GetHashCode();
}
if (this.AdditionalProperties != null)
{
hashCode = (hashCode * 59) + this.AdditionalProperties.GetHashCode();
}
return hashCode;
}
}
Expand Down
Loading

0 comments on commit f6fb838

Please sign in to comment.