Skip to content

Commit

Permalink
[csharp][generichost] Removed redundant properties (OpenAPITools#15869)
Browse files Browse the repository at this point in the history
* removed redundant properties

* removed commneted code

* restored discriminator as a model property

* build samples
  • Loading branch information
devhl-labs authored Jun 25, 2023
1 parent c23560a commit ea2c686
Show file tree
Hide file tree
Showing 63 changed files with 161 additions and 1,827 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)

List<CodegenProperty> anyOf = composedSchemas.getAnyOf();
if (anyOf != null) {
removePropertiesDeclaredInComposedTypes(objs, model, anyOf);
for(CodegenProperty property : anyOf) {
property.name = patchPropertyName(model, property.baseType);
property.isNullable = true;
Expand All @@ -514,6 +515,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)

List<CodegenProperty> oneOf = composedSchemas.getOneOf();
if (oneOf != null) {
removePropertiesDeclaredInComposedTypes(objs, model, oneOf);
for(CodegenProperty property : oneOf) {
property.name = patchPropertyName(model, property.baseType);
property.isNullable = true;
Expand Down Expand Up @@ -552,6 +554,9 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
return processed;
}

protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> objs, CodegenModel model, List<CodegenProperty> composedProperties) {
}

private String patchPropertyName(CodegenModel model, String value) {
// the casing will be wrong if we just set the name to escapeReservedWord
// if we try to fix it with camelize, underscores get stripped out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,42 @@ public ModelsMap postProcessModels(ModelsMap objs) {
return objs;
}

// https://github.com/OpenAPITools/openapi-generator/issues/15867
@Override
protected void removePropertiesDeclaredInComposedTypes(Map<String, ModelsMap> objs, CodegenModel model, List<CodegenProperty> composedProperties) {
if (!GENERICHOST.equals(getLibrary())) {
return;
}

String discriminatorName = model.discriminator == null
? null
: model.discriminator.getPropertyName();

for(CodegenProperty oneOfProperty : composedProperties) {
String ref = oneOfProperty.getRef();
if (ref != null) {
for (Map.Entry<String, ModelsMap> composedEntry : objs.entrySet()) {
CodegenModel composedModel = ModelUtils.getModelByName(composedEntry.getKey(), objs);
if (ref.endsWith("/" + composedModel.name)) {
for (CodegenProperty composedProperty : composedModel.allVars) {
if (discriminatorName != null && composedProperty.name.equals(discriminatorName)) {
continue;
}
model.vars.removeIf(v -> v.name.equals(composedProperty.name));
model.allVars.removeIf(v -> v.name.equals(composedProperty.name));
model.readOnlyVars.removeIf(v -> v.name.equals(composedProperty.name));
model.nonNullableVars.removeIf(v -> v.name.equals(composedProperty.name));
model.optionalVars.removeIf(v -> v.name.equals(composedProperty.name));
model.parentRequiredVars.removeIf(v -> v.name.equals(composedProperty.name));
model.readWriteVars.removeIf(v -> v.name.equals(composedProperty.name));
model.requiredVars.removeIf(v -> v.name.equals(composedProperty.name));
}
}
}
}
}
}

/**
* Return true if the property being passed is a C# value type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Color** | **string** | | [optional]
**Cultivar** | **string** | | [optional]
**LengthCm** | **decimal** | | [optional]
**Origin** | **string** | | [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 @@ -4,10 +4,6 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Cultivar** | **string** | |
**LengthCm** | **decimal** | |
**Mealy** | **bool** | | [optional]
**Sweet** | **bool** | | [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 @@ -5,9 +5,6 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Color** | **string** | | [optional]
**Cultivar** | **string** | | [optional]
**LengthCm** | **decimal** | | [optional]
**Origin** | **string** | | [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 @@ -5,9 +5,6 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ClassName** | **string** | |
**HasBaleen** | **bool** | | [optional]
**HasTeeth** | **bool** | | [optional]
**Type** | **string** | | [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 @@ -5,7 +5,6 @@ The value may be a shape or the 'null' value. The 'nullable' attribute was intro

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**QuadrilateralType** | **string** | |
**ShapeType** | **string** | |

[[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 @@ -5,7 +5,6 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**QuadrilateralType** | **string** | |
**ShapeType** | **string** | |

[[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 @@ -4,7 +4,6 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**QuadrilateralType** | **string** | |
**ShapeType** | **string** | |

[[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 @@ -5,7 +5,6 @@ The value may be a shape or the 'null' value. This is introduced in OAS schema >

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**QuadrilateralType** | **string** | |
**ShapeType** | **string** | |

[[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 @@ -4,7 +4,6 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ShapeType** | **string** | |
**TriangleType** | **string** | |

[[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 @@ -148,8 +148,6 @@ public static string SanitizeFilename(string filename)
return EnumTest.EnumStringEnumToJsonValue(enumTestEnumStringEnum);
if (obj is EnumTest.EnumStringRequiredEnum enumTestEnumStringRequiredEnum)
return EnumTest.EnumStringRequiredEnumToJsonValue(enumTestEnumStringRequiredEnum);
if (obj is Mammal.TypeEnum mammalTypeEnum)
return Mammal.TypeEnumToJsonValue(mammalTypeEnum);
if (obj is MapTest.InnerEnum mapTestInnerEnum)
return MapTest.InnerEnumToJsonValue(mapTestInnerEnum);
if (obj is Order.StatusEnum orderStatusEnum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ public partial class Fruit : IValidatableObject
/// Initializes a new instance of the <see cref="Fruit" /> class.
/// </summary>
/// <param name="apple"></param>
/// <param name="cultivar">cultivar</param>
/// <param name="lengthCm">lengthCm</param>
/// <param name="origin">origin</param>
/// <param name="color">color</param>
[JsonConstructor]
public Fruit(Apple? apple, string cultivar, decimal lengthCm, string origin, string color)
public Fruit(Apple? apple, string color)
{
Apple = apple;
Cultivar = cultivar;
LengthCm = lengthCm;
Origin = origin;
Color = color;
OnCreated();
}
Expand All @@ -53,17 +47,11 @@ public Fruit(Apple? apple, string cultivar, decimal lengthCm, string origin, str
/// Initializes a new instance of the <see cref="Fruit" /> class.
/// </summary>
/// <param name="banana"></param>
/// <param name="cultivar">cultivar</param>
/// <param name="lengthCm">lengthCm</param>
/// <param name="origin">origin</param>
/// <param name="color">color</param>
[JsonConstructor]
public Fruit(Banana? banana, string cultivar, decimal lengthCm, string origin, string color)
public Fruit(Banana? banana, string color)
{
Banana = banana;
Cultivar = cultivar;
LengthCm = lengthCm;
Origin = origin;
Color = color;
OnCreated();
}
Expand All @@ -80,24 +68,6 @@ public Fruit(Banana? banana, string cultivar, decimal lengthCm, string origin, s
/// </summary>
public Banana? Banana { get; set; }

/// <summary>
/// Gets or Sets Cultivar
/// </summary>
[JsonPropertyName("cultivar")]
public string Cultivar { get; set; }

/// <summary>
/// Gets or Sets LengthCm
/// </summary>
[JsonPropertyName("lengthCm")]
public decimal LengthCm { get; set; }

/// <summary>
/// Gets or Sets Origin
/// </summary>
[JsonPropertyName("origin")]
public string Origin { get; set; }

/// <summary>
/// Gets or Sets Color
/// </summary>
Expand All @@ -113,9 +83,6 @@ public override string ToString()
StringBuilder sb = new StringBuilder();
sb.Append("class Fruit {\n");
sb.Append(" Color: ").Append(Color).Append("\n");
sb.Append(" Cultivar: ").Append(Cultivar).Append("\n");
sb.Append(" LengthCm: ").Append(LengthCm).Append("\n");
sb.Append(" Origin: ").Append(Origin).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
Expand All @@ -127,20 +94,6 @@ public override string ToString()
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
// Cultivar (string) pattern
Regex regexCultivar = new Regex(@"^[a-zA-Z\s]*$", RegexOptions.CultureInvariant);
if (false == regexCultivar.Match(this.Cultivar).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Cultivar, must match a pattern of " + regexCultivar, new [] { "Cultivar" });
}

// Origin (string) pattern
Regex regexOrigin = new Regex(@"^[A-Z\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (false == regexOrigin.Match(this.Origin).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Origin, must match a pattern of " + regexOrigin, new [] { "Origin" });
}

yield break;
}
}
Expand All @@ -167,9 +120,6 @@ public override Fruit Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert

JsonTokenType startingTokenType = utf8JsonReader.TokenType;

string? cultivar = default;
decimal? lengthCm = default;
string? origin = default;
string? color = default;

while (utf8JsonReader.Read())
Expand All @@ -187,16 +137,6 @@ public override Fruit Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert

switch (propertyName)
{
case "cultivar":
cultivar = utf8JsonReader.GetString();
break;
case "lengthCm":
if (utf8JsonReader.TokenType != JsonTokenType.Null)
lengthCm = utf8JsonReader.GetDecimal();
break;
case "origin":
origin = utf8JsonReader.GetString();
break;
case "color":
color = utf8JsonReader.GetString();
break;
Expand All @@ -206,25 +146,16 @@ public override Fruit Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert
}
}

if (cultivar == null)
throw new ArgumentNullException(nameof(cultivar), "Property is required for class Fruit.");

if (lengthCm == null)
throw new ArgumentNullException(nameof(lengthCm), "Property is required for class Fruit.");

if (origin == null)
throw new ArgumentNullException(nameof(origin), "Property is required for class Fruit.");

if (color == null)
throw new ArgumentNullException(nameof(color), "Property is required for class Fruit.");

Utf8JsonReader appleReader = utf8JsonReader;
if (Client.ClientUtils.TryDeserialize<Apple>(ref appleReader, jsonSerializerOptions, out Apple? apple))
return new Fruit(apple, cultivar, lengthCm.Value, origin, color);
return new Fruit(apple, color);

Utf8JsonReader bananaReader = utf8JsonReader;
if (Client.ClientUtils.TryDeserialize<Banana>(ref bananaReader, jsonSerializerOptions, out Banana? banana))
return new Fruit(banana, cultivar, lengthCm.Value, origin, color);
return new Fruit(banana, color);

throw new JsonException();
}
Expand All @@ -244,9 +175,6 @@ public override void Write(Utf8JsonWriter writer, Fruit fruit, JsonSerializerOpt

writer.WriteStartObject();

writer.WriteString("cultivar", fruit.Cultivar);
writer.WriteNumber("lengthCm", fruit.LengthCm);
writer.WriteString("origin", fruit.Origin);
writer.WriteString("color", fruit.Color);

writer.WriteEndObject();
Expand Down
Loading

0 comments on commit ea2c686

Please sign in to comment.