Skip to content

Commit 5639709

Browse files
Don't create enums from literal constants (microsoft#7408)
Fixes microsoft#7405 --------- Co-authored-by: Dapeng Zhang <[email protected]>
1 parent 1300a0e commit 5639709

File tree

53 files changed

+95
-973
lines changed

Some content is hidden

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

53 files changed

+95
-973
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/RestClientProvider.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,9 @@ private IEnumerable<MethodBodyStatement> AppendHeaderParameters(HttpRequestApi r
220220
string? format;
221221
ValueExpression valueExpression;
222222
GetParamInfo(paramMap, operation, inputParameter, out type, out format, out valueExpression);
223-
ValueExpression toStringExpression;
224-
if (type == null || type.IsLiteral || type.Equals(typeof(string)))
225-
{
226-
toStringExpression = valueExpression;
227-
}
228-
else
229-
{
230-
toStringExpression = TypeFormattersSnippets.ConvertToString(valueExpression, Literal(format));
231-
}
223+
ValueExpression toStringExpression = type?.Equals(typeof(string)) == true ?
224+
valueExpression :
225+
valueExpression.ConvertToString(Literal(format));
232226
MethodBodyStatement statement;
233227
if (type?.IsCollection == true)
234228
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/ModelReaderWriterValidation/TestProjects/Sample_TypeSpec/ThingTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ protected override void VerifyModel(Thing model, string format)
5252
Assert.AreEqual("\"mockUnion\"", model.RequiredUnion.ToString());
5353
Assert.AreEqual(parsedWireJson.GetProperty("requiredBadDescription").GetString(), model.RequiredBadDescription);
5454
Assert.AreEqual(parsedWireJson.GetProperty("requiredNullableList").GetArrayLength(), model.RequiredNullableList.Count);
55-
Assert.AreEqual(new ThingRequiredLiteralString(parsedWireJson.GetProperty("requiredLiteralString").GetString()), model.RequiredLiteralString);
56-
Assert.AreEqual(new ThingRequiredLiteralInt(parsedWireJson.GetProperty("requiredLiteralInt").GetInt32()), model.RequiredLiteralInt);
57-
Assert.AreEqual(new ThingRequiredLiteralFloat(parsedWireJson.GetProperty("requiredLiteralFloat").GetSingle()), model.RequiredLiteralFloat);
55+
Assert.AreEqual("accept", model.RequiredLiteralString);
56+
Assert.AreEqual(123, model.RequiredLiteralInt);
57+
Assert.AreEqual(1.23f, model.RequiredLiteralFloat);
5858
Assert.AreEqual(parsedWireJson.GetProperty("requiredLiteralBool").GetBoolean(), model.RequiredLiteralBool);
59-
Assert.AreEqual(new ThingOptionalLiteralString(parsedWireJson.GetProperty("optionalLiteralString").GetString()), model.OptionalLiteralString);
60-
Assert.AreEqual(new ThingOptionalLiteralInt(parsedWireJson.GetProperty("optionalLiteralInt").GetInt32()), model.OptionalLiteralInt);
61-
Assert.AreEqual(new ThingOptionalLiteralFloat(parsedWireJson.GetProperty("optionalLiteralFloat").GetSingle()), model.OptionalLiteralFloat);
59+
Assert.AreEqual("hi", model.OptionalLiteralString);
60+
Assert.AreEqual(456, model.OptionalLiteralInt);
61+
Assert.AreEqual(4.56f, model.OptionalLiteralFloat);
6262
Assert.AreEqual(parsedWireJson.GetProperty("optionalLiteralBool").GetBoolean(), model.OptionalLiteralBool);
6363
Assert.AreEqual(parsedWireJson.GetProperty("optionalNullableList").GetArrayLength(), model.OptionalNullableList.Count);
6464

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/ModelReaderWriterValidation/TestProjects/Sample_TypeSpec/ThingWithNullsTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ protected override void VerifyModel(Thing model, string format)
5353
Assert.AreEqual(parsedWireJson.GetProperty("requiredBadDescription").GetString(), model.RequiredBadDescription);
5454
Assert.AreEqual(JsonValueKind.Null, parsedWireJson.GetProperty("requiredNullableList").ValueKind);
5555
Assert.IsEmpty(model.RequiredNullableList);
56-
Assert.AreEqual(new ThingRequiredLiteralString(parsedWireJson.GetProperty("requiredLiteralString").GetString()), model.RequiredLiteralString);
57-
Assert.AreEqual(new ThingRequiredLiteralInt(parsedWireJson.GetProperty("requiredLiteralInt").GetInt32()), model.RequiredLiteralInt);
58-
Assert.AreEqual(new ThingRequiredLiteralFloat(parsedWireJson.GetProperty("requiredLiteralFloat").GetSingle()), model.RequiredLiteralFloat);
56+
Assert.AreEqual("accept", model.RequiredLiteralString);
57+
Assert.AreEqual(123, model.RequiredLiteralInt);
58+
Assert.AreEqual(1.23f, model.RequiredLiteralFloat);
5959
Assert.AreEqual(parsedWireJson.GetProperty("requiredLiteralBool").GetBoolean(), model.RequiredLiteralBool);
60-
Assert.AreEqual(new ThingOptionalLiteralString(parsedWireJson.GetProperty("optionalLiteralString").GetString()), model.OptionalLiteralString);
61-
Assert.AreEqual(new ThingOptionalLiteralInt(parsedWireJson.GetProperty("optionalLiteralInt").GetInt32()), model.OptionalLiteralInt);
62-
Assert.AreEqual(new ThingOptionalLiteralFloat(parsedWireJson.GetProperty("optionalLiteralFloat").GetSingle()), model.OptionalLiteralFloat);
60+
Assert.AreEqual("hi", model.OptionalLiteralString);
61+
Assert.AreEqual(456, model.OptionalLiteralInt);
62+
Assert.AreEqual(4.56f, model.OptionalLiteralFloat);
6363
Assert.AreEqual(parsedWireJson.GetProperty("optionalLiteralBool").GetBoolean(), model.OptionalLiteralBool);
6464
Assert.IsFalse(parsedWireJson.TryGetProperty("optionalNullableList", out _));
6565
Assert.IsEmpty(model.OptionalNullableList);

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/OutputLibrary.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,6 @@ private static TypeProvider[] BuildEnums()
4242
}
4343
}
4444

45-
foreach (var inputLiteral in input.Constants)
46-
{
47-
var valueType = CodeModelGenerator.Instance.TypeFactory.GetLiteralValueType(inputLiteral);
48-
if (valueType is not InputEnumType enumType)
49-
{
50-
continue;
51-
}
52-
53-
var outputEnum = CodeModelGenerator.Instance.TypeFactory.CreateEnum(enumType);
54-
55-
// If there is a custom code view for a fixed enum, then we should not emit the generated enum as the custom code will have
56-
// the implementation. We will still need to emit the serialization code.
57-
if (outputEnum is FixedEnumProvider { CustomCodeView: { IsEnum: true, Type: { IsValueType: true, IsStruct: false } } })
58-
{
59-
enums.AddRange(outputEnum.SerializationProviders);
60-
}
61-
else if (outputEnum != null)
62-
{
63-
enums.Add(outputEnum);
64-
}
65-
}
66-
6745
return [.. enums];
6846
}
6947

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/CanonicalTypeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private static CSharpType EnsureEnum(InputModelProperty? specProperty, CSharpTyp
257257
{
258258
InputNullableType nullableType => GetEnumValueType(nullableType.Type),
259259
InputEnumType enumType => enumType.ValueType,
260-
InputLiteralType literalType => GetEnumValueType(CodeModelGenerator.Instance.TypeFactory.GetLiteralValueType(literalType)),
260+
InputLiteralType inputLiteral => inputLiteral.ValueType,
261261
InputArrayType arrayType => GetEnumValueType(arrayType.ValueType),
262262
InputDictionaryType dictionaryType => GetEnumValueType(dictionaryType.ValueType),
263263
_ => null

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/TypeFactory.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,13 @@ protected internal TypeFactory()
4747
return type;
4848
}
4949

50-
internal InputType GetLiteralValueType(InputLiteralType literal)
51-
{
52-
if (LiteralValueTypeCache.TryGetValue(literal, out var valueType))
53-
{
54-
return valueType;
55-
}
56-
57-
// we only convert the literal into enum when it is not a boolean
58-
if (literal.ValueType.Kind == InputPrimitiveTypeKind.Boolean)
59-
{
60-
valueType = literal.ValueType;
61-
}
62-
else
63-
{
64-
var values = new List<InputEnumTypeValue>();
65-
var enumType = new InputEnumType(literal.Name, literal.Namespace, $"{literal.Namespace}.{literal.Name}", null, null, null, $"The {literal.Name}", InputModelTypeUsage.Input | InputModelTypeUsage.Output, literal.ValueType, values, true);
66-
values.Add(new InputEnumTypeValue(literal.Value.ToString() ?? "Null", literal.Value, literal.ValueType, null, literal.Value.ToString(), enumType));
67-
valueType = enumType;
68-
}
69-
LiteralValueTypeCache.Add(literal, valueType);
70-
return valueType;
71-
}
72-
7350
protected virtual CSharpType? CreateCSharpTypeCore(InputType inputType)
7451
{
7552
CSharpType? type;
7653
switch (inputType)
7754
{
7855
case InputLiteralType literalType:
79-
var input = CreateCSharpType(GetLiteralValueType(literalType));
56+
var input = CreateCSharpType(literalType.ValueType);
8057
type = input != null ? CSharpType.FromLiteral(input, literalType.Value) : null;
8158
break;
8259
case InputEnumTypeValue enumValueType:

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/OutputLibraryTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Linq;
56
using Microsoft.TypeSpec.Generator.Providers;
7+
using Microsoft.TypeSpec.Generator.Tests.Common;
68
using Moq;
79
using Moq.Protected;
810
using NUnit.Framework;
@@ -20,5 +22,13 @@ public void BuildTypeProviders_Override()
2022
mockOutputLibrary.Protected().Setup<TypeProvider[]>("BuildTypeProviders").Throws<NotImplementedException>();
2123
Assert.Throws<NotImplementedException>(() => { object shouldFail = mockOutputLibrary.Object.TypeProviders; });
2224
}
25+
26+
[Test]
27+
public void ConstantsShouldNotBeTurnedIntoEnums()
28+
{
29+
var plugin = MockHelpers.LoadMockGenerator(inputLiteralTypes: [InputFactory.Literal.String("foo"), InputFactory.Literal.Int32(42)]);
30+
var outputLibrary = plugin.Object.OutputLibrary;
31+
Assert.AreEqual(0, outputLibrary.TypeProviders.Count(t => t is EnumProvider));
32+
}
2333
}
2434
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/TestHelpers/MockHelpers.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public async static Task<Mock<CodeModelGenerator>> LoadMockGeneratorAsync(
2929
string? configuration = null,
3030
InputModelType[]? inputModelTypes = null,
3131
InputEnumType[]? inputEnumTypes = null,
32+
InputLiteralType[]? inputLiteralTypes = null,
3233
Func<Task<Compilation>>? compilation = null,
3334
Func<Task<Compilation>>? lastContractCompilation = null,
3435
IEnumerable<MetadataReference>? additionalMetadataReferences = null,
@@ -46,6 +47,7 @@ public async static Task<Mock<CodeModelGenerator>> LoadMockGeneratorAsync(
4647
configuration,
4748
inputModelTypes,
4849
inputEnumTypes,
50+
inputLiteralTypes,
4951
additionalMetadataReferences,
5052
sharedSourceDirectories,
5153
typesToKeep,
@@ -70,6 +72,7 @@ public static Mock<CodeModelGenerator> LoadMockGenerator(
7072
string? configuration = null,
7173
InputModelType[]? inputModelTypes = null,
7274
InputEnumType[]? inputEnumTypes = null,
75+
InputLiteralType[]? inputLiteralTypes = null,
7376
IEnumerable<MetadataReference>? additionalMetadataReferences = null,
7477
IEnumerable<string>? sharedSourceDirectories = null,
7578
IEnumerable<string>? typesToKeep = null,
@@ -113,7 +116,8 @@ public static Mock<CodeModelGenerator> LoadMockGenerator(
113116
mockInputLibrary.Setup(l => l.InputNamespace).Returns(InputFactory.Namespace(
114117
inputNamespaceName ?? "Sample",
115118
models: inputModelTypes,
116-
enums: inputEnumTypes));
119+
enums: inputEnumTypes,
120+
constants: inputLiteralTypes));
117121

118122
mockGenerator.Setup(p => p.InputLibrary).Returns(mockInputLibrary.Object);
119123

packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/GetUnknownValueResponse6.cs

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

0 commit comments

Comments
 (0)