Skip to content

Commit

Permalink
refactor: Refactored computing of AnyOf smart property names.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Oct 25, 2024
1 parent 7ced9c8 commit 10c2a9f
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 168 deletions.
13 changes: 6 additions & 7 deletions src/libs/AutoSDK/Extensions/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,10 @@ public static bool HasAllOfTypeForMetadata(
public static string? GetDefaultValue(this SchemaContext context)
{
context = context ?? throw new ArgumentNullException(nameof(context));
context.TypeData = context.TypeData ?? throw new InvalidOperationException("Invalid state: TypeData is null");

if (context.TypeData.Value.CSharpType == "object?" ||
if (context.TypeData.CSharpType == "object?" ||
context.Schema.Default is OpenApiArray ||
context.TypeData.Value.CSharpTypeNullability)
context.TypeData.CSharpTypeNullability)
{
return string.Empty;
}
Expand All @@ -364,7 +363,7 @@ context.Schema.Default is OpenApiArray ||
return string.Empty;
}

return context.TypeData.Value.CSharpTypeWithoutNullability + "." + result.Name;
return context.TypeData.CSharpTypeWithoutNullability + "." + result.Name;
}
if (context.Schema.AnyOf.Any(x => x.Enum.Any()) && context.Schema.Default != null)
{
Expand All @@ -390,7 +389,7 @@ context.Schema.Default is OpenApiArray ||
return null;
}

return enumChildContext.TypeData?.CSharpTypeWithoutNullability + "." + value;
return enumChildContext.TypeData.CSharpTypeWithoutNullability + "." + value;
}
if (context.Schema.OneOf.Any(x => x.Enum.Any()) && context.Schema.Default != null)
{
Expand All @@ -403,7 +402,7 @@ context.Schema.Default is OpenApiArray ||
return string.Empty;
}

return enumChildContext.TypeData?.CSharpTypeWithoutNullability + "." + result.Name;
return enumChildContext.TypeData.CSharpTypeWithoutNullability + "." + result.Name;
}
if (context.Schema.AllOf.Any(x => x.Enum.Any()) && context.Schema.Default != null)
{
Expand All @@ -416,7 +415,7 @@ context.Schema.Default is OpenApiArray ||
return string.Empty;
}

return enumChildContext.TypeData?.CSharpTypeWithoutNullability + "." + result.Name;
return enumChildContext.TypeData.CSharpTypeWithoutNullability + "." + result.Name;
}
if (context.Schema.Default is OpenApiString @string && !string.IsNullOrWhiteSpace(@string.Value))
{
Expand Down
13 changes: 4 additions & 9 deletions src/libs/AutoSDK/Models/AnyOfData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Immutable;
using AutoSDK.Extensions;
using AutoSDK.Helpers;
using AutoSDK.Naming.AnyOfs;
using AutoSDK.Serialization.Json;

namespace AutoSDK.Models;
Expand Down Expand Up @@ -31,11 +31,6 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
: Hint.AllOf))
.ToList();
var className = context.Id.ToClassName();
var useSmartNames = children.All(x =>
x.Schema.Reference != null &&
!string.IsNullOrWhiteSpace(SmartNamedAnyOfNames.ComputeSmartName(
(x.TypeData ?? TypeData.Default).ShortCSharpTypeWithoutNullability,
className)));
TypeData? discriminatorType = null;
string? discriminatorPropertyName = null;
//IDictionary<string, string>? discriminatorMapping = null;
Expand Down Expand Up @@ -90,10 +85,10 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
Properties: context.IsNamedAnyOfLike
? children.Select((x, i) => PropertyData.Default with
{
Type = x.TypeData ?? TypeData.Default,
Name = useSmartNames && (x.TypeData ?? TypeData.Default).CSharpTypeWithoutNullability.StartsWith("global::System.", StringComparison.Ordinal) != true
Type = x.TypeData,
Name = SmartNamedAnyOfNames.ShouldUseSmartName(children, className)
? SmartNamedAnyOfNames.ComputeSmartName(
(x.TypeData ?? TypeData.Default).ShortCSharpTypeWithoutNullability,
x.TypeData,
className)
: $"Value{i + 1}",
Summary = x.Schema.GetSummary(),
Expand Down
2 changes: 1 addition & 1 deletion src/libs/AutoSDK/Models/MethodParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static MethodParameter FromSchemaContext(SchemaContext context)
context = context ?? throw new ArgumentNullException(nameof(context));
var parameter = context.Parameter ?? throw new InvalidOperationException("Parameter or parameter data is required.");
var parameterName = context.ParameterName ?? throw new InvalidOperationException("Property name or parameter name is required.");
var type = context.TypeData ?? throw new InvalidOperationException("TypeData is required.");
var type = context.TypeData;
if (parameter.In == ParameterLocation.Query &&
(context.IsClass || context.ResolvedReference?.IsClass == true) &&
(context.ResolvedReference?.ClassData ?? context.ClassData)?.Properties.FirstOrDefault(x => x.Id == parameterName) is { } property &&
Expand Down
2 changes: 1 addition & 1 deletion src/libs/AutoSDK/Models/PropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static PropertyData FromSchemaContext(SchemaContext context)
{
context = context ?? throw new ArgumentNullException(nameof(context));
var propertyName = context.PropertyName ?? throw new InvalidOperationException("Property name or parameter name is required.");
var type = context.TypeData ?? throw new InvalidOperationException("TypeData is required.");
var type = context.TypeData;

// OpenAPI doesn't allow metadata for references so sometimes allOf with single item is used to add metadata.
if (context.HasAllOfTypeForMetadata())
Expand Down
17 changes: 8 additions & 9 deletions src/libs/AutoSDK/Models/SchemaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class SchemaContext
public OpenApiResponse? Response { get; init; }
public bool IsOperation => OperationPath != null;

public TypeData? TypeData { get; set; }
public TypeData TypeData { get; set; } = TypeData.Default;

public bool IsClass => Type == "class";// || ResolvedReference?.IsClass == true;
//public ModelData? ClassData { get; set; }
Expand Down Expand Up @@ -181,8 +181,6 @@ private static string ComputeType(OpenApiSchema schema, bool isComponent)
return schema.Type ?? "class";
}

public string ShortType => TypeData?.CSharpType ?? Type;

public bool IsRequired =>
IsProperty && Parent?.Schema.Required.Contains(PropertyName) == true ||
Hint == Models.Hint.Parameter && Parameter?.Required == true ||
Expand Down Expand Up @@ -378,14 +376,15 @@ public void ComputeData(int level = 0, int maxDepth = 20)
}

TypeData = IsReference
? ResolvedReference?.TypeData
: Models.TypeData.FromSchemaContext(this);
if (IsReference && ResolvedReference != null && TypeData.HasValue)
? ResolvedReference?.TypeData ??
throw new InvalidOperationException("Resolved reference must have type data.")
: TypeData.FromSchemaContext(this);
if (IsReference && ResolvedReference != null && TypeData != TypeData.Default)
{
TypeData = TypeData.Value with
TypeData = TypeData with
{
CSharpTypeRaw = global::AutoSDK.Models.TypeData.GetCSharpType(ResolvedReference),
CSharpTypeNullability = global::AutoSDK.Models.TypeData.GetCSharpNullability(ResolvedReference, this),
CSharpTypeRaw = TypeData.GetCSharpType(ResolvedReference),
CSharpTypeNullability = TypeData.GetCSharpNullability(ResolvedReference, this),
};
}
if (IsProperty)
Expand Down
24 changes: 12 additions & 12 deletions src/libs/AutoSDK/Models/TypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,29 @@ public static TypeData FromSchemaContext(SchemaContext context)
if (context.Schema.IsAnyOf())
{
subTypes = context.Children
.Where(x => x is { Hint: Hint.AnyOf, TypeData: not null })
.Select(x => x.TypeData!.Value)
.Where(x => x is { Hint: Hint.AnyOf } && x.TypeData != Default)
.Select(x => x.TypeData)
.ToImmutableArray();
}
else if (context.Schema.IsOneOf())
{
subTypes = context.Children
.Where(x => x is { Hint: Hint.OneOf, TypeData: not null })
.Select(x => x.TypeData!.Value)
.Where(x => x is { Hint: Hint.OneOf } && x.TypeData != Default)
.Select(x => x.TypeData)
.ToImmutableArray();
}
else if (context.Schema.IsAllOf())
{
subTypes = context.Children
.Where(x => x is { Hint: Hint.AllOf, TypeData: not null })
.Select(x => x.TypeData!.Value)
.Where(x => x is { Hint: Hint.AllOf } && x.TypeData != Default)
.Select(x => x.TypeData)
.ToImmutableArray();
}
if (context.Schema.IsArray())
{
subTypes = [
context.Children
.FirstOrDefault(x => x is { Hint: Hint.ArrayItem, TypeData: not null })
.FirstOrDefault(x => x is { Hint: Hint.ArrayItem } && x.TypeData != Default)
?.TypeData ??
Default with
{
Expand Down Expand Up @@ -218,13 +218,13 @@ public static string GetCSharpType(SchemaContext context)
(_, _) when context.Schema.IsUnixTimestamp() => "global::System.DateTimeOffset",

(_, _) when context.Schema.IsArray() =>
$"{context.Children.FirstOrDefault(x => x.Hint == Hint.ArrayItem)?.TypeData?.CSharpTypeWithoutNullability}".AsArray(),
$"{context.Children.FirstOrDefault(x => x.Hint == Hint.ArrayItem)?.TypeData.CSharpTypeWithoutNullability}".AsArray(),

(_, _) when context.IsNamedAnyOfLike => $"global::{context.Settings.Namespace}.{context.Id}",

(_, _) when context.Schema.IsAnyOf() => $"global::{context.Settings.Namespace}.AnyOf<{string.Join(", ", context.Children.Where(x => x.Hint == Hint.AnyOf).Select(x => x.TypeData?.CSharpTypeWithNullabilityForValueTypes))}>",
(_, _) when context.Schema.IsOneOf() => $"global::{context.Settings.Namespace}.OneOf<{string.Join(", ", context.Children.Where(x => x.Hint == Hint.OneOf).Select(x => x.TypeData?.CSharpTypeWithNullabilityForValueTypes))}>",
(_, _) when context.Schema.IsAllOf() => $"global::{context.Settings.Namespace}.AllOf<{string.Join(", ", context.Children.Where(x => x.Hint == Hint.AllOf).Select(x => x.TypeData?.CSharpTypeWithNullabilityForValueTypes))}>",
(_, _) when context.Schema.IsAnyOf() => $"global::{context.Settings.Namespace}.AnyOf<{string.Join(", ", context.Children.Where(x => x.Hint == Hint.AnyOf).Select(x => x.TypeData.CSharpTypeWithNullabilityForValueTypes))}>",
(_, _) when context.Schema.IsOneOf() => $"global::{context.Settings.Namespace}.OneOf<{string.Join(", ", context.Children.Where(x => x.Hint == Hint.OneOf).Select(x => x.TypeData.CSharpTypeWithNullabilityForValueTypes))}>",
(_, _) when context.Schema.IsAllOf() => $"global::{context.Settings.Namespace}.AllOf<{string.Join(", ", context.Children.Where(x => x.Hint == Hint.AllOf).Select(x => x.TypeData.CSharpTypeWithNullabilityForValueTypes))}>",

("object", _) or (null, _) when
context.Schema.Reference != null &&
Expand All @@ -243,7 +243,7 @@ public static string GetCSharpType(SchemaContext context)

("object", _) when
context.Schema.AdditionalProperties?.Type is not null =>
$"global::System.Collections.Generic.Dictionary<string, {context.Children.FirstOrDefault(x => x.Hint == Hint.AdditionalProperties)?.TypeData?.CSharpType}>",
$"global::System.Collections.Generic.Dictionary<string, {context.Children.FirstOrDefault(x => x.Hint == Hint.AdditionalProperties)?.TypeData.CSharpType}>",

("string", _) when context.Schema.Enum.Any() =>
$"global::{context.Settings.Namespace}.{context.Id}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
namespace AutoSDK.Helpers;
using AutoSDK.Models;

namespace AutoSDK.Naming.AnyOfs;

public static class SmartNamedAnyOfNames
{
public static string ComputeSmartName(string typeName, string className)
public static bool ShouldUseSmartName(IList<SchemaContext> children, string className)
{
return children.All(x =>
x.Schema.Reference != null &&
!string.IsNullOrWhiteSpace(ComputeSmartName(x.TypeData, className)) &&
x.TypeData.CSharpTypeWithoutNullability.StartsWith("global::System.", StringComparison.Ordinal) != true);
}

public static string ComputeSmartName(TypeData typeData, string className)
{
var typeName = typeData.ShortCSharpTypeWithoutNullability;
var nameWords = SplitToWordsByUpperCharacters(typeName);
var classNameWords = SplitToWordsByUpperCharacters(className);

Expand Down
6 changes: 3 additions & 3 deletions src/libs/AutoSDK/Sources/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ .. includedTags.Select(tag => PropertyData.Default with
settings.GenerateJsonSerializerContextTypes
? filteredSchemas
.Where(x =>
x is { TypeData: not null } &&
!string.IsNullOrWhiteSpace(x.TypeData!.Value.CSharpType))
.Select(x => x.TypeData!.Value)
x.TypeData != TypeData.Default &&
!string.IsNullOrWhiteSpace(x.TypeData.CSharpType))
.Select(x => x.TypeData)
.GroupBy(x => x.CSharpTypeWithNullability)
.Select(x => x.First())
.ToImmutableArray()
Expand Down
Loading

0 comments on commit 10c2a9f

Please sign in to comment.