Skip to content

Commit 095bf0a

Browse files
committed
fix: Fixed Discriminator property name for some cases.
1 parent f4299e4 commit 095bf0a

14 files changed

+130
-335
lines changed

src/libs/AutoSDK/Extensions/OpenApiExtensions.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.OpenApi.Models;
99
using Microsoft.OpenApi.Readers;
1010
using AutoSDK.Models;
11+
using AutoSDK.Naming.Properties;
1112
using AutoSDK.Serialization.Form;
1213
using Microsoft.OpenApi.Validations;
1314

@@ -571,20 +572,6 @@ public static string ClearForXml(this string text)
571572
};
572573
}
573574

574-
internal static string ToCSharpName(this string text, Settings settings, SchemaContext? parent)
575-
{
576-
var name = text.ToPropertyName();
577-
578-
name = PropertyData.HandleWordSeparators(name);
579-
580-
if (parent != null)
581-
{
582-
name = name.FixPropertyName(parent.Id);
583-
}
584-
585-
return PropertyData.SanitizeName(name, settings.ClsCompliantEnumPrefix, true);
586-
}
587-
588575
public static T ResolveIfRequired<T>(this T referenceable) where T : class, IOpenApiReferenceable
589576
{
590577
referenceable = referenceable ?? throw new ArgumentNullException(nameof(referenceable));
@@ -693,7 +680,7 @@ public static PropertyData ToEnumValue(
693680
return PropertyData.Default with
694681
{
695682
Id = id,
696-
Name = PropertyData.SanitizeName(name, settings.ClsCompliantEnumPrefix),
683+
Name = CSharpPropertyNameGenerator.SanitizeName(name, settings.ClsCompliantEnumPrefix),
697684
Summary = ClearForXml(ExtractEnumSummaryFromDescription(id, description)),
698685
};
699686
}

src/libs/AutoSDK/Models/AnyOfData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Immutable;
22
using AutoSDK.Extensions;
33
using AutoSDK.Naming.AnyOfs;
4+
using AutoSDK.Naming.Properties;
45
using AutoSDK.Serialization.Json;
56

67
namespace AutoSDK.Models;
@@ -39,7 +40,8 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
3940
context.Schema.Discriminator.Mapping.Count != 0)
4041
{
4142
discriminatorType = context.Children.FirstOrDefault(x => x.Hint == Hint.Discriminator)?.TypeData;
42-
discriminatorPropertyName = context.Schema.Discriminator.PropertyName.ToPropertyName();
43+
discriminatorPropertyName = context.Schema.Discriminator.PropertyName.ToPropertyName()
44+
.ToCSharpName(context.Settings, context.Parent);
4345
}
4446

4547
var count = context.IsAnyOf

src/libs/AutoSDK/Models/MethodParameter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Immutable;
22
using Microsoft.OpenApi.Models;
33
using AutoSDK.Extensions;
4+
using AutoSDK.Naming.Properties;
45
using AutoSDK.Serialization.Json;
56

67
namespace AutoSDK.Models;
@@ -66,7 +67,7 @@ public static MethodParameter FromSchemaContext(SchemaContext context)
6667
name = name.FixPropertyName(context.Parent.Id);
6768
}
6869

69-
name = PropertyData.SanitizeName(name, context.Settings.ClsCompliantEnumPrefix, true);
70+
name = CSharpPropertyNameGenerator.SanitizeName(name, context.Settings.ClsCompliantEnumPrefix, true);
7071

7172
var isRequired =
7273
parameter.Required ||

src/libs/AutoSDK/Models/PropertyData.cs

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AutoSDK.Extensions;
2+
using AutoSDK.Naming.Properties;
23

34
namespace AutoSDK.Models;
45

@@ -37,7 +38,6 @@ public readonly record struct PropertyData(
3738
public static PropertyData FromSchemaContext(SchemaContext context)
3839
{
3940
context = context ?? throw new ArgumentNullException(nameof(context));
40-
var propertyName = context.PropertyName ?? throw new InvalidOperationException("Property name or parameter name is required.");
4141
var type = context.TypeData;
4242

4343
// OpenAPI doesn't allow metadata for references so sometimes allOf with single item is used to add metadata.
@@ -50,21 +50,11 @@ public static PropertyData FromSchemaContext(SchemaContext context)
5050
};
5151
}
5252

53-
var name = propertyName.ToPropertyName();
54-
55-
name = HandleWordSeparators(name);
56-
57-
if (context.Parent != null)
58-
{
59-
name = name.FixPropertyName(context.Parent.Id);
60-
}
61-
62-
name = SanitizeName(name, context.Settings.ClsCompliantEnumPrefix, true);
63-
6453
var requiredProperties = context.Parent != null
6554
? new HashSet<string>(context.Parent.Schema.Required)
6655
: [];
6756

57+
var propertyName = context.PropertyName ?? throw new InvalidOperationException("Property name or parameter name is required.");
6858
var isRequired =
6959
requiredProperties.Contains(propertyName) &&
7060
context.Schema is { WriteOnly: false };
@@ -76,7 +66,7 @@ public static PropertyData FromSchemaContext(SchemaContext context)
7666

7767
return new PropertyData(
7868
Id: propertyName,
79-
Name: name,
69+
Name: CSharpPropertyNameGenerator.ComputePropertyName(context),
8070
Type: type with
8171
{
8272
CSharpTypeNullability = type.CSharpTypeNullability || context.Schema is { WriteOnly: true },
@@ -99,70 +89,6 @@ public static PropertyData FromSchemaContext(SchemaContext context)
9989
DiscriminatorValue: string.Empty);
10090
}
10191

102-
internal static string SanitizeName(string? name, string clsCompliantEnumPrefix, bool skipHandlingWordSeparators = false)
103-
{
104-
static bool InvalidFirstChar(char ch)
105-
=> ch is not ('_' or >= 'A' and <= 'Z' or >= 'a' and <= 'z');
106-
107-
static bool InvalidSubsequentChar(char ch)
108-
=> ch is not (
109-
'_'
110-
or >= 'A' and <= 'Z'
111-
or >= 'a' and <= 'z'
112-
or >= '0' and <= '9'
113-
);
114-
115-
if (name is null || name.Length == 0)
116-
{
117-
return "";
118-
}
119-
120-
if (!skipHandlingWordSeparators)
121-
{
122-
name = HandleWordSeparators(name);
123-
}
124-
125-
if (name.Length == 0)
126-
{
127-
return string.IsNullOrWhiteSpace(clsCompliantEnumPrefix)
128-
? "_"
129-
: clsCompliantEnumPrefix;
130-
}
131-
132-
if (InvalidFirstChar(name[0]))
133-
{
134-
name = (string.IsNullOrWhiteSpace(clsCompliantEnumPrefix)
135-
? "_"
136-
: clsCompliantEnumPrefix) + name;
137-
}
138-
139-
if (!name.Skip(1).Any(InvalidSubsequentChar))
140-
{
141-
return name;
142-
}
143-
144-
Span<char> buf = stackalloc char[name.Length];
145-
name.AsSpan().CopyTo(buf);
146-
147-
for (var i = 1; i < buf.Length; i++)
148-
{
149-
if (InvalidSubsequentChar(buf[i]))
150-
{
151-
buf[i] = '_';
152-
}
153-
}
154-
155-
// Span<char>.ToString implementation checks for char type, new string(&buf[0], buf.length)
156-
return buf.ToString();
157-
}
158-
159-
internal static string HandleWordSeparators(string name)
160-
{
161-
return name
162-
.ReplacePlusAndMinusOnStart()
163-
.UseWordSeparator('_', '+', '-', '.', '/', '(', '[', ']', ')');
164-
}
165-
16692
public string ParameterName => Name
16793
.Replace(".", string.Empty)
16894
.ToParameterName()

src/libs/AutoSDK/Models/SchemaContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.OpenApi.Models;
22
using AutoSDK.Extensions;
33
using AutoSDK.Naming.Models;
4+
using AutoSDK.Naming.Properties;
45
using Microsoft.OpenApi.Any;
56

67
namespace AutoSDK.Models;

src/libs/AutoSDK/Naming/Clients/ClientNameGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.OpenApi.Models;
22
using AutoSDK.Extensions;
33
using AutoSDK.Models;
4+
using AutoSDK.Naming.Properties;
45

56
namespace AutoSDK.Naming.Clients;
67

@@ -25,6 +26,6 @@ public static string GeneratePropertyName(
2526
.SkipWhile(c => !char.IsDigit(c) && !char.IsLetter(c))
2627
.ToArray());
2728

28-
return PropertyData.SanitizeName(name.ToClassName(), settings.ClsCompliantEnumPrefix);
29+
return CSharpPropertyNameGenerator.SanitizeName(name.ToClassName(), settings.ClsCompliantEnumPrefix);
2930
}
3031
}

src/libs/AutoSDK/Naming/Models/ModelNameGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.OpenApi.Models;
22
using AutoSDK.Extensions;
33
using AutoSDK.Models;
4+
using AutoSDK.Naming.Properties;
45

56
namespace AutoSDK.Naming.Models;
67

@@ -112,7 +113,7 @@ public static string ComputeId(SchemaContext context)
112113
{
113114
context = context ?? throw new ArgumentNullException(nameof(context));
114115

115-
context.ClassName = PropertyData.SanitizeName(context.ComputeClassName(), context.Settings.ClsCompliantEnumPrefix);
116+
context.ClassName = CSharpPropertyNameGenerator.SanitizeName(context.ComputeClassName(), context.Settings.ClsCompliantEnumPrefix);
116117
context.Id = context.ClassName;
117118

118119
return context.Id;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using AutoSDK.Extensions;
2+
using AutoSDK.Models;
3+
4+
namespace AutoSDK.Naming.Properties;
5+
6+
public static class CSharpPropertyNameGenerator
7+
{
8+
public static string ComputePropertyName(
9+
SchemaContext context)
10+
{
11+
context = context ?? throw new ArgumentNullException(nameof(context));
12+
var propertyName = context.PropertyName ?? throw new InvalidOperationException("Property name or parameter name is required.");
13+
14+
var name = propertyName.ToPropertyName();
15+
16+
name = HandleWordSeparators(name);
17+
18+
if (context.Parent != null)
19+
{
20+
name = name.FixPropertyName(context.Parent.Id);
21+
}
22+
23+
name = SanitizeName(name, context.Settings.ClsCompliantEnumPrefix, true);
24+
25+
return name;
26+
}
27+
28+
internal static string SanitizeName(string? name, string clsCompliantEnumPrefix, bool skipHandlingWordSeparators = false)
29+
{
30+
static bool InvalidFirstChar(char ch)
31+
=> ch is not ('_' or >= 'A' and <= 'Z' or >= 'a' and <= 'z');
32+
33+
static bool InvalidSubsequentChar(char ch)
34+
=> ch is not (
35+
'_'
36+
or >= 'A' and <= 'Z'
37+
or >= 'a' and <= 'z'
38+
or >= '0' and <= '9'
39+
);
40+
41+
if (name is null || name.Length == 0)
42+
{
43+
return "";
44+
}
45+
46+
if (!skipHandlingWordSeparators)
47+
{
48+
name = HandleWordSeparators(name);
49+
}
50+
51+
if (name.Length == 0)
52+
{
53+
return string.IsNullOrWhiteSpace(clsCompliantEnumPrefix)
54+
? "_"
55+
: clsCompliantEnumPrefix;
56+
}
57+
58+
if (InvalidFirstChar(name[0]))
59+
{
60+
name = (string.IsNullOrWhiteSpace(clsCompliantEnumPrefix)
61+
? "_"
62+
: clsCompliantEnumPrefix) + name;
63+
}
64+
65+
if (!name.Skip(1).Any(InvalidSubsequentChar))
66+
{
67+
return name;
68+
}
69+
70+
Span<char> buf = stackalloc char[name.Length];
71+
name.AsSpan().CopyTo(buf);
72+
73+
for (var i = 1; i < buf.Length; i++)
74+
{
75+
if (InvalidSubsequentChar(buf[i]))
76+
{
77+
buf[i] = '_';
78+
}
79+
}
80+
81+
// Span<char>.ToString implementation checks for char type, new string(&buf[0], buf.length)
82+
return buf.ToString();
83+
}
84+
85+
internal static string HandleWordSeparators(string name)
86+
{
87+
return name
88+
.ReplacePlusAndMinusOnStart()
89+
.UseWordSeparator('_', '+', '-', '.', '/', '(', '[', ']', ')');
90+
}
91+
92+
internal static string ToCSharpName(this string text, Settings settings, SchemaContext? parent)
93+
{
94+
var name = text.ToPropertyName();
95+
96+
name = HandleWordSeparators(name);
97+
98+
if (parent != null)
99+
{
100+
name = name.FixPropertyName(parent.Id);
101+
}
102+
103+
return SanitizeName(name, settings.ClsCompliantEnumPrefix, true);
104+
}
105+
}

src/tests/AutoSDK.SnapshotTests/Snapshots/luma/NewtonsoftJson/Tests.SdkGenerator_Diagnostics.received.txt

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)