Skip to content

Commit 6a575e9

Browse files
committed
feat: Use Discriminator mapping values for names AnyOf property names.
1 parent 10c2a9f commit 6a575e9

File tree

132 files changed

+5759
-5786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+5759
-5786
lines changed

src/libs/AutoSDK/Models/AnyOfData.cs

+47-42
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public readonly record struct AnyOfData(
1010
int Count,
1111
TypeData? DiscriminatorType,
1212
string? DiscriminatorPropertyName,
13-
//EquatableArray<(string, string)>? DiscriminatorMapping,
1413
bool IsTrimming,
1514
string Namespace,
1615
string Name,
@@ -19,6 +18,8 @@ public readonly record struct AnyOfData(
1918
Settings Settings
2019
)
2120
{
21+
public bool IsNamed => !string.IsNullOrWhiteSpace(Name);
22+
2223
public static AnyOfData FromSchemaContext(SchemaContext context)
2324
{
2425
context = context ?? throw new ArgumentNullException(nameof(context));
@@ -33,26 +34,55 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
3334
var className = context.Id.ToClassName();
3435
TypeData? discriminatorType = null;
3536
string? discriminatorPropertyName = null;
36-
//IDictionary<string, string>? discriminatorMapping = null;
3737

3838
if (context.Schema.Discriminator != null &&
3939
context.Schema.Discriminator.Mapping.Count != 0)
4040
{
4141
discriminatorType = context.Children.FirstOrDefault(x => x.Hint == Hint.Discriminator)?.TypeData;
4242
discriminatorPropertyName = context.Schema.Discriminator.PropertyName.ToPropertyName();
43-
44-
// if (context.Schema.Discriminator.Mapping.Count == 0)
45-
// {
46-
// if (children.All(x =>
47-
// x.Children.FirstOrDefault(y => y.PropertyName == discriminatorPropertyName)?.GetDefaultValue() != null &&
48-
// x.ClassName != null))
49-
// {
50-
// context.Schema.Discriminator.Mapping = children
51-
// .ToDictionary(
52-
// x => x.GetDefaultValue()!,
53-
// x => x.ClassName);
54-
// }
55-
// }
43+
}
44+
45+
var count = context.IsAnyOf
46+
? context.Schema.AnyOf.Count
47+
: context.IsOneOf
48+
? context.Schema.OneOf.Count
49+
: context.Schema.AllOf.Count;
50+
var properties = context.IsNamedAnyOfLike
51+
? children.Select((x, i) => PropertyData.Default with
52+
{
53+
Type = x.TypeData,
54+
Name = SmartNamedAnyOfNames.ComputePropertyName(children, className, i),
55+
Summary = x.Schema.GetSummary(),
56+
DiscriminatorValue = context.Schema.Discriminator?.Mapping?
57+
.FirstOrDefault(y =>
58+
y.Value.Contains(x.Id) ||
59+
(x.Schema.Properties.ContainsKey(context.Schema.Discriminator.PropertyName) &&
60+
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.Count == 1 &&
61+
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.FirstOrDefault()
62+
?.GetString() == y.Key))
63+
.Key?.ToEnumValue(string.Empty, context.Settings).Name ?? string.Empty,
64+
}).ToImmutableArray().AsEquatableArray()
65+
: Enumerable
66+
.Range(1, count)
67+
.Select(i => PropertyData.Default with
68+
{
69+
Name = $"Value{i}",
70+
Type = TypeData.Default with
71+
{
72+
CSharpTypeRaw = $"T{i}",
73+
},
74+
})
75+
.ToImmutableArray().AsEquatableArray();
76+
if (context.IsNamedAnyOfLike &&
77+
!properties.IsEmpty &&
78+
properties.All(x => !string.IsNullOrWhiteSpace(x.DiscriminatorValue)))
79+
{
80+
properties = properties
81+
.Select(x => x with
82+
{
83+
Name = x.DiscriminatorValue,
84+
})
85+
.ToImmutableArray().AsEquatableArray();
5686
}
5787

5888
return new AnyOfData(
@@ -61,16 +91,9 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
6191
: context.IsOneOf
6292
? "OneOf"
6393
: "AllOf",
64-
Count: context.IsAnyOf
65-
? context.Schema.AnyOf.Count
66-
: context.IsOneOf
67-
? context.Schema.OneOf.Count
68-
: context.Schema.AllOf.Count,
94+
Count: count,
6995
DiscriminatorType: discriminatorType,
7096
DiscriminatorPropertyName: discriminatorPropertyName,
71-
//DiscriminatorMapping: discriminatorMapping?
72-
// .Select(x => (x.Key, x.Value))
73-
// .ToImmutableArray(),
7497
IsTrimming:
7598
context.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson &&
7699
(!string.IsNullOrWhiteSpace(context.Settings.JsonSerializerContext) ||
@@ -82,25 +105,7 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
82105
Summary: context.IsNamedAnyOfLike
83106
? context.Schema.GetSummary()
84107
: string.Empty,
85-
Properties: context.IsNamedAnyOfLike
86-
? children.Select((x, i) => PropertyData.Default with
87-
{
88-
Type = x.TypeData,
89-
Name = SmartNamedAnyOfNames.ShouldUseSmartName(children, className)
90-
? SmartNamedAnyOfNames.ComputeSmartName(
91-
x.TypeData,
92-
className)
93-
: $"Value{i + 1}",
94-
Summary = x.Schema.GetSummary(),
95-
DiscriminatorValue = context.Schema.Discriminator?.Mapping?
96-
.FirstOrDefault(y =>
97-
y.Value.Contains(x.Id) ||
98-
(x.Schema.Properties.ContainsKey(context.Schema.Discriminator.PropertyName) &&
99-
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.Count == 1 &&
100-
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.FirstOrDefault()?.GetString() == y.Key))
101-
.Key?.ToEnumValue(string.Empty, context.Settings).Name ?? string.Empty,
102-
}).ToImmutableArray()
103-
: ImmutableArray<PropertyData>.Empty,
108+
Properties: properties,
104109
Settings: context.Settings);
105110
}
106111
}

src/libs/AutoSDK/Models/PropertyData.cs

-13
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,4 @@ internal static string HandleWordSeparators(string name)
240240
.ReplaceIfEquals("void", "@void")
241241
.ReplaceIfEquals("volatile", "@volatile")
242242
.ReplaceIfEquals("while", "@while");
243-
244-
public string ArgumentName
245-
{
246-
get
247-
{
248-
if (Type.EnumValues.Length != 0 && Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
249-
{
250-
return ParameterName + "Value";
251-
}
252-
253-
return ParameterName;
254-
}
255-
}
256243
}

src/libs/AutoSDK/Naming/AnyOfs/SmartNamedAnyOfNames.cs

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ namespace AutoSDK.Naming.AnyOfs;
44

55
public static class SmartNamedAnyOfNames
66
{
7+
public static string ComputePropertyName(IList<SchemaContext> children, string className, int i)
8+
{
9+
return ShouldUseSmartName(children, className)
10+
? ComputeSmartName(
11+
children.ElementAt(i).TypeData,
12+
className)
13+
: $"Value{i + 1}";
14+
}
15+
716
public static bool ShouldUseSmartName(IList<SchemaContext> children, string className)
817
{
918
return children.All(x =>

src/libs/AutoSDK/Sources/Sources.AnyOf.cs

+26-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.Immutable;
21
using AutoSDK.Extensions;
32
using AutoSDK.Models;
43

@@ -11,82 +10,69 @@ public static string GenerateAnyOf(
1110
CancellationToken cancellationToken = default)
1211
{
1312
var types = $"<{string.Join(", ", Enumerable.Range(1, anyOfData.Count).Select(x => $"T{x}"))}>";
14-
var classNameWithoutTypes = string.IsNullOrWhiteSpace(anyOfData.Name)
13+
var classNameWithoutTypes = !anyOfData.IsNamed
1514
? $"{anyOfData.SubType}"
1615
: anyOfData.Name;
17-
var className = string.IsNullOrWhiteSpace(anyOfData.Name)
16+
var className = !anyOfData.IsNamed
1817
? $"{anyOfData.SubType}{types}"
1918
: anyOfData.Name;
20-
var allTypes = anyOfData.Properties.IsEmpty
21-
? Enumerable
22-
.Range(1, anyOfData.Count)
23-
.Select(i => PropertyData.Default with
24-
{
25-
Name = $"Value{i}",
26-
Type = TypeData.Default with
27-
{
28-
CSharpTypeRaw = $"T{i}",
29-
},
30-
})
31-
.ToImmutableArray().AsEquatableArray()
32-
: anyOfData.Properties;
3319
var validation = anyOfData.SubType switch
3420
{
35-
"AnyOf" => string.Join(" || ", allTypes.Select(x => $"Is{x.Name}")),
36-
"OneOf" => string.Join(" || ", allTypes.Select((x, xi) =>
37-
string.Join(" && ", allTypes.Select((y, yi) => $"{(yi == xi ? "" : "!")}Is{y.Name}")))),
38-
"AllOf" => string.Join(" && ", allTypes.Select(x => $"Is{x.Name}")),
21+
"AnyOf" => string.Join(" || ", anyOfData.Properties.Select(x => $"Is{x.Name}")),
22+
"OneOf" => string.Join(" || ", anyOfData.Properties.Select((x, xi) =>
23+
string.Join(" && ", anyOfData.Properties.Select((y, yi) => $"{(yi == xi ? "" : "!")}Is{y.Name}")))),
24+
"AllOf" => string.Join(" && ", anyOfData.Properties.Select(x => $"Is{x.Name}")),
3925
_ => throw new NotImplementedException(),
4026
};
4127
var constructorWithAllValues =
4228
anyOfData.Count > 1 ||
43-
(!string.IsNullOrWhiteSpace(anyOfData.Name) &&
29+
(anyOfData.IsNamed &&
4430
anyOfData.DiscriminatorType != null &&
4531
anyOfData.DiscriminatorPropertyName != null &&
46-
allTypes.All(x => !string.IsNullOrWhiteSpace(x.DiscriminatorValue))) ? $@"
32+
anyOfData.Properties.All(x => !string.IsNullOrWhiteSpace(x.DiscriminatorValue))) ? $@"
4733
{string.Empty.ToXmlDocumentationSummary(level: 8)}
4834
public {classNameWithoutTypes}(
49-
{(string.IsNullOrWhiteSpace(anyOfData.Name) ||
35+
{(!anyOfData.IsNamed ||
5036
anyOfData.DiscriminatorType == null ||
5137
anyOfData.DiscriminatorPropertyName == null ||
52-
allTypes.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
38+
anyOfData.Properties.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
5339
{anyOfData.DiscriminatorType.Value.CSharpTypeWithoutNullability}{anyOfData.DiscriminatorPropertyName}? {anyOfData.DiscriminatorPropertyName.ToParameterName()},
5440
")}
55-
{allTypes.Select(x => $@"
41+
{anyOfData.Properties.Select(x => $@"
5642
{x.Type.CSharpTypeWithNullability} {x.ParameterName},
5743
").Inject().TrimEnd(',', '\n')}
5844
)
5945
{{
60-
{(string.IsNullOrWhiteSpace(anyOfData.Name) ||
46+
{(!anyOfData.IsNamed ||
6147
anyOfData.DiscriminatorType == null ||
6248
anyOfData.DiscriminatorPropertyName == null ||
63-
allTypes.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
49+
anyOfData.Properties.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
6450
{anyOfData.DiscriminatorPropertyName} = {anyOfData.DiscriminatorPropertyName.ToParameterName()};
6551
")}
66-
{allTypes.Select(x => $@"
52+
{anyOfData.Properties.Select(x => $@"
6753
{x.Name} = {x.ParameterName};
6854
").Inject()}
6955
}}" : " ";
7056
var json = GenerateFromToJsonMethods(anyOfData.Namespace, className, anyOfData.Settings, isValueType: true, cancellationToken);
7157

7258
return $@"using System.Linq;
73-
{(anyOfData.Properties.IsEmpty ? "" : @"#pragma warning disable CS0618 // Type or member is obsolete
74-
")}
59+
{(anyOfData.IsNamed ? @"#pragma warning disable CS0618 // Type or member is obsolete
60+
" : "")}
7561
#nullable enable
7662
7763
namespace {anyOfData.Namespace}
7864
{{
7965
{anyOfData.Summary.ToXmlDocumentationSummary(level: 4)}
8066
public readonly partial struct {className} : global::System.IEquatable<{className}>
8167
{{
82-
{(string.IsNullOrWhiteSpace(anyOfData.Name) ||
68+
{(!anyOfData.IsNamed ||
8369
anyOfData.DiscriminatorType == null ||
8470
anyOfData.DiscriminatorPropertyName == null ||
85-
allTypes.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
71+
anyOfData.Properties.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
8672
{string.Empty.ToXmlDocumentationSummary(level: 8)}
8773
public {anyOfData.DiscriminatorType.Value.CSharpTypeWithoutNullability}{anyOfData.DiscriminatorPropertyName}? {anyOfData.DiscriminatorPropertyName} {{ get; }}
8874
")}
89-
{allTypes.Select(x => $@"
75+
{anyOfData.Properties.Select(x => $@"
9076
{x.Summary.ToXmlDocumentationSummary(level: 8)}
9177
#if NET6_0_OR_GREATER
9278
public {x.Type.CSharpTypeWithNullability} {x.Name} {{ get; init; }}
@@ -117,7 +103,7 @@ namespace {anyOfData.Namespace}
117103
118104
{string.Empty.ToXmlDocumentationSummary(level: 8)}
119105
public object? Object =>
120-
{allTypes.Reverse().Select(x => $@"
106+
{anyOfData.Properties.Reverse().Select(x => $@"
121107
{x.Name} as object ??
122108
").Inject().TrimEnd('?', '\n')}
123109
;
@@ -130,7 +116,7 @@ public bool Validate()
130116
131117
{string.Empty.ToXmlDocumentationSummary(level: 8)}
132118
public TResult? Match<TResult>(
133-
{allTypes.Select(x => $@"
119+
{anyOfData.Properties.Select(x => $@"
134120
global::System.Func<{x.Type.CSharpType}, TResult>? {x.ParameterName} = null,
135121
").Inject()}
136122
bool validate = true)
@@ -140,7 +126,7 @@ public bool Validate()
140126
Validate();
141127
}}
142128
143-
{allTypes.Select((x, i) => $@"
129+
{anyOfData.Properties.Select((x, i) => $@"
144130
{(i > 0 ? "else " : "")}if (Is{x.Name} && {x.ParameterName} != null)
145131
{{
146132
return {x.ParameterName}({x.Name}!);
@@ -151,7 +137,7 @@ public bool Validate()
151137
152138
{string.Empty.ToXmlDocumentationSummary(level: 8)}
153139
public void Match(
154-
{allTypes.Select(x => $@"
140+
{anyOfData.Properties.Select(x => $@"
155141
global::System.Action<{x.Type.CSharpType}>? {x.ParameterName} = null,
156142
").Inject()}
157143
bool validate = true)
@@ -161,7 +147,7 @@ public void Match(
161147
Validate();
162148
}}
163149
164-
{allTypes.Select((x, i) => $@"
150+
{anyOfData.Properties.Select((x, i) => $@"
165151
{(i > 0 ? "else " : "")}if (Is{x.Name})
166152
{{
167153
{x.ParameterName}?.Invoke({x.Name}!);
@@ -173,7 +159,7 @@ public override int GetHashCode()
173159
{{
174160
var fields = new object?[]
175161
{{
176-
{allTypes.Select(x => $@"
162+
{anyOfData.Properties.Select(x => $@"
177163
{x.Name},
178164
typeof({x.Type.CSharpTypeWithoutNullability}),
179165
").Inject()}
@@ -193,7 +179,7 @@ static int HashCodeAggregator(int hashCode, object? value) => value == null
193179
public bool Equals({className} other)
194180
{{
195181
return
196-
{allTypes.Select(x => $@"
182+
{anyOfData.Properties.Select(x => $@"
197183
global::System.Collections.Generic.EqualityComparer<{x.Type.CSharpTypeWithNullability}>.Default.Equals({x.Name}, other.{x.Name}) &&
198184
").Inject().TrimEnd('&', '\n')}
199185
;

0 commit comments

Comments
 (0)