From 09f77ab515dd7517c1e3b27e22da6b505dc3cec8 Mon Sep 17 00:00:00 2001 From: archerzz Date: Sat, 28 Jan 2023 22:03:52 +0800 Subject: [PATCH 01/10] feat(cadl): support literal type model property - change json coverter for `InputModelProperty` to set the `DefaultValue` property if the property type is a literal type - update `ObjectTypeProperty` so that `DefaultValue` is set if it's from literal type - change `ModelTypeProviderFields`: - exclude literal properties from constructor parameter list - literal properties are `internal readonly` - change `ModelTypeProvider`, update the criteria on properties to be serialized, now literal type property will be serialized regardless it's read-only - fix a issue in emitter that boolean literal type is not identified - minor refactoring - update test projects resolve #3035 --- .../Common/Generation/Writers/ModelWriter.cs | 2 +- .../Input/CadlInputModelPropertyConverter.cs | 24 +- .../Output/Models/Types/ModelTypeProvider.cs | 5 + .../Models/Types/ModelTypeProviderFields.cs | 9 +- .../Output/Models/Types/ObjectTypeProperty.cs | 3 +- .../Emitter.Csharp/src/lib/model.ts | 2 + .../FirstTest-Cadl/FirstTest-Cadl.cadl | 18 +- .../Generated/Docs/CadlFirstTestClient.xml | 200 +++++++--- .../Generated/Models/Thing.Serialization.cs | 28 +- .../FirstTest-Cadl/Generated/Models/Thing.cs | 17 +- .../FirstTest-Cadl/Generated/cadl.json | 368 ++++++++++-------- 11 files changed, 442 insertions(+), 234 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Generation/Writers/ModelWriter.cs b/src/AutoRest.CSharp/Common/Generation/Writers/ModelWriter.cs index 0dbb34867bd..0dea35edaea 100644 --- a/src/AutoRest.CSharp/Common/Generation/Writers/ModelWriter.cs +++ b/src/AutoRest.CSharp/Common/Generation/Writers/ModelWriter.cs @@ -309,7 +309,7 @@ protected virtual void AddCtorAttribute(CodeWriter writer, ObjectType schema, Ob { } - public void WriteConstructor(CodeWriter writer, ObjectType schema) + private void WriteConstructor(CodeWriter writer, ObjectType schema) { foreach (var constructor in schema.Constructors) { diff --git a/src/AutoRest.CSharp/Common/Input/CadlInputModelPropertyConverter.cs b/src/AutoRest.CSharp/Common/Input/CadlInputModelPropertyConverter.cs index db55b9286b7..59b9543aac3 100644 --- a/src/AutoRest.CSharp/Common/Input/CadlInputModelPropertyConverter.cs +++ b/src/AutoRest.CSharp/Common/Input/CadlInputModelPropertyConverter.cs @@ -53,7 +53,7 @@ private static InputModelProperty ReadInputModelProperty(ref Utf8JsonReader read description = description ?? throw new JsonException($"{nameof(InputModelProperty)} must have a description."); propertyType = propertyType ?? throw new JsonException($"{nameof(InputModelProperty)} must have a property type."); - var property = new InputModelProperty(name, serializedName ?? name, description, propertyType, isRequired, isReadOnly, isDiscriminator); + var property = new InputModelProperty(name, serializedName ?? name, description, propertyType, isRequired, isReadOnly, isDiscriminator, GetDefaultValue(propertyType)); if (id != null) { resolver.AddReference(id, property); @@ -61,5 +61,27 @@ private static InputModelProperty ReadInputModelProperty(ref Utf8JsonReader read return property; } + + private static FormattableString? GetDefaultValue(InputType propertyType) + { + if (propertyType is not InputLiteralType literalType) + { + return null; + } + + return literalType.LiteralValueType switch + { + InputPrimitiveType primitiveType => primitiveType.Kind switch + { + InputTypeKind.Boolean => $"{literalType.Value.ToString()!.ToLower()}", + InputTypeKind.Float32 or InputTypeKind.Float64 or InputTypeKind.Float128 + or InputTypeKind.Int32 or InputTypeKind.Int64 => $"{literalType.Value.ToString()}", + InputTypeKind.String => $"\"{(literalType.Value).ToString()}\"", + _ => throw new Exception($"Unsupported literal value type: {primitiveType}"), + + }, + _ => throw new Exception($"Unsupported literal value type: {literalType.LiteralValueType}"), + }; + } } } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs index fc64bf3616b..2363b97d92d 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs @@ -160,6 +160,11 @@ private bool ShouldSkipSerialization(ObjectTypeProperty property) return false; } + if (property.InputModelProperty.Type is InputLiteralType) + { + return false; + } + if (property.InputModelProperty!.IsReadOnly) { return true; diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProviderFields.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProviderFields.cs index 1bd9dba5125..ec80dde7375 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProviderFields.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProviderFields.cs @@ -64,6 +64,10 @@ public ModelTypeProviderFields(InputModelType inputModel, TypeFactory typeFactor fields.Add(field); fieldsToInputs[field] = inputModelProperty; + if (inputModelProperty.Type is InputLiteralType) + { + continue; // literal property does not show up in the constructor parameter list + } var parameter = Parameter.FromModelProperty(inputModelProperty, existingMember is IFieldSymbol ? inputModelProperty.Name.ToVariableName() : field.Name.FirstCharToLowerCase(), field.Type); parametersToFields[parameter.Name] = field; serializationParameters.Add(parameter); @@ -95,9 +99,10 @@ private static FieldDeclaration CreateField(string fieldName, CSharpType fieldTy inputModelProperty.Type is CodeModelType type && (type.Schema is ArraySchema or DictionarySchema); var propertyIsRequiredInNonRoundTripModel = inputModel.Usage is InputModelTypeUsage.Input or InputModelTypeUsage.Output && inputModelProperty.IsRequired; var propertyIsOptionalInOutputModel = inputModel.Usage is InputModelTypeUsage.Output && !inputModelProperty.IsRequired; - var propertyIsReadOnly = inputModelProperty.IsReadOnly || propertyIsCollection || propertyIsRequiredInNonRoundTripModel || propertyIsOptionalInOutputModel; + var propertyIsLiteralType = inputModelProperty.Type is InputLiteralType; + var propertyIsReadOnly = inputModelProperty.IsReadOnly || propertyIsLiteralType || propertyIsCollection || propertyIsRequiredInNonRoundTripModel || propertyIsOptionalInOutputModel; - var fieldModifiers = propertyIsReadOnly ? Public | ReadOnly : Public; + var fieldModifiers = propertyIsReadOnly ? (propertyIsLiteralType ? Internal : Public ) | ReadOnly : Public; CodeWriterDeclaration declaration = new CodeWriterDeclaration(fieldName); declaration.SetActualName(fieldName); diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs index 0aab3f3dd91..3949aef6c4d 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs @@ -20,7 +20,8 @@ public ObjectTypeProperty(FieldDeclaration field, InputModelProperty inputModelP : this(new MemberDeclarationOptions(field.Accessibility, field.Name, field.Type), field.Description?.ToString() ?? String.Empty, field.Modifiers.HasFlag(FieldModifiers.ReadOnly), null, field.IsRequired, inputModelProperty: inputModelProperty) { // now the default value will be set only when the model is generated from property bag - if (enclosingType is ModelTypeProvider model && model.IsPropertyBag) + if ((enclosingType is ModelTypeProvider model && model.IsPropertyBag) || + (inputModelProperty.Type is InputLiteralType)) // or the property is a literal type { DefaultValue = field.DefaultValue; } diff --git a/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts b/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts index 6a55d8b6598..da3f2d78a7e 100644 --- a/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts +++ b/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts @@ -71,6 +71,8 @@ export function mapCadlTypeToCSharpInputTypeKind( return InputTypeKind.Enum; case "Number": return InputTypeKind.Int32; + case "Boolean": + return InputTypeKind.Boolean; case "String": if (format === "date") return InputTypeKind.DateTime; if (format === "uri") return InputTypeKind.Uri; diff --git a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl index ef813a2cd5f..56ced2c6134 100644 --- a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl +++ b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl @@ -43,10 +43,22 @@ enum ExtensibleEnum { model Thing { @doc("name of the Thing") name: string; + @doc("required Union") requiredUnion: string | string[]|int32; - @doc("required literal type") - requiredLiteral: "accept"; + + @doc("required literal string") + requiredLiteralString: "accept"; + + @doc("required literal int") + requiredLiteralInt: 123; + + // TODO: how do we identify if the number literal is a float or double? +// @doc("required literal float") +// requiredLiteralFloat: 1.23; + + @doc("required literal bool") + requiredLiteralBool: false; } @friendlyName("Friend") @@ -146,4 +158,4 @@ namespace EnumTest { @route("/unknown-value") @doc("get extensible enum") op getUnknownValue(): DaysOfWeekExtensibleEnum; -} \ No newline at end of file +} diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml index dfd98d8fb3a..8d12a1b1247 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml +++ b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml @@ -13,7 +13,9 @@ Response response = await client.TopActionAsync(""); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -25,7 +27,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -43,7 +47,9 @@ Response response = client.TopAction(""); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -55,7 +61,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -73,7 +81,9 @@ Response response = await client.TopAction2Async(); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -85,7 +95,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -103,7 +115,9 @@ Response response = client.TopAction2(); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -115,7 +129,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -131,7 +147,9 @@ var client = new CadlFirstTestClient(credential); var data = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }; Response response = await client.PatchActionAsync(RequestContent.Create(data)); @@ -139,7 +157,9 @@ Response response = await client.PatchActionAsync(RequestContent.Create(data)); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -151,7 +171,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -161,7 +183,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -177,7 +201,9 @@ var client = new CadlFirstTestClient(credential); var data = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }; Response response = client.PatchAction(RequestContent.Create(data)); @@ -185,7 +211,9 @@ Response response = client.PatchAction(RequestContent.Create(data)); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -197,7 +225,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -207,7 +237,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -223,7 +255,9 @@ var client = new CadlFirstTestClient(credential); var data = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }; Response response = await client.AnonymousBodyAsync(RequestContent.Create(data)); @@ -231,7 +265,9 @@ Response response = await client.AnonymousBodyAsync(RequestContent.Create(data)) JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -243,7 +279,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -253,7 +291,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -269,7 +309,9 @@ var client = new CadlFirstTestClient(credential); var data = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }; Response response = client.AnonymousBody(RequestContent.Create(data)); @@ -277,7 +319,9 @@ Response response = client.AnonymousBody(RequestContent.Create(data)); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -289,7 +333,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -299,7 +345,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -433,7 +481,9 @@ Response response = await client.SayHiAsync("", " JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> This sample shows how to call SayHiAsync with all parameters, and how to parse the result. ", " JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -457,7 +509,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -475,7 +529,9 @@ Response response = client.SayHi("", ""); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> This sample shows how to call SayHi with all parameters, and how to parse the result. ", "", " @@ -499,7 +557,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -524,7 +584,9 @@ var data = new { requiredModel = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }, }; @@ -533,7 +595,9 @@ Response response = await client.HelloAgainAsync("", "", RequestContent. JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -550,7 +614,9 @@ Schema for RoundTripModel: requiredModel: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. }, # Required. } @@ -561,7 +627,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -586,7 +654,9 @@ var data = new { requiredModel = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }, }; @@ -595,7 +665,9 @@ Response response = client.HelloAgain("", "", RequestContent.Create(data JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -612,7 +684,9 @@ Schema for RoundTripModel: requiredModel: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. }, # Required. } @@ -623,7 +697,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -648,7 +724,9 @@ var data = new { requiredModel = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }, }; @@ -657,7 +735,9 @@ Response response = await client.NoContentTypeAsync("", "", RequestConte JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -674,7 +754,9 @@ Schema for RoundTripModel: requiredModel: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. }, # Required. } @@ -685,7 +767,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -710,7 +794,9 @@ var data = new { requiredModel = new { name = "", requiredUnion = new {}, - requiredLiteral = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralBool = new {}, }, }; @@ -719,7 +805,9 @@ Response response = client.NoContentType("", "", RequestContent.Create(d JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -736,7 +824,9 @@ Schema for RoundTripModel: requiredModel: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. }, # Required. } @@ -747,7 +837,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -765,7 +857,9 @@ Response response = await client.HelloDemo2Async(); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -777,7 +871,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } @@ -795,7 +891,9 @@ Response response = client.HelloDemo2(); JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); -Console.WriteLine(result.GetProperty("requiredLiteral").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -807,7 +905,9 @@ Schema for Thing: { name: string, # Required. requiredUnion: Union, # Required. - requiredLiteral: Literal, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralBool: Literal, # Required. } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs index dbe2be53cd6..e10fd687308 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs @@ -20,8 +20,12 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) writer.WriteStringValue(Name); writer.WritePropertyName("requiredUnion"); writer.WriteStringValue(RequiredUnion); - writer.WritePropertyName("requiredLiteral"); - writer.WriteStringValue(RequiredLiteral); + writer.WritePropertyName("requiredLiteralString"); + writer.WriteStringValue(RequiredLiteralString); + writer.WritePropertyName("requiredLiteralInt"); + writer.WriteNumberValue(RequiredLiteralInt); + writer.WritePropertyName("requiredLiteralBool"); + writer.WriteBooleanValue(RequiredLiteralBool); writer.WriteEndObject(); } @@ -29,7 +33,9 @@ internal static Thing DeserializeThing(JsonElement element) { string name = default; string requiredUnion = default; - string requiredLiteral = default; + string requiredLiteralString = default; + int requiredLiteralInt = default; + bool requiredLiteralBool = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("name")) @@ -42,13 +48,23 @@ internal static Thing DeserializeThing(JsonElement element) requiredUnion = property.Value.GetString(); continue; } - if (property.NameEquals("requiredLiteral")) + if (property.NameEquals("requiredLiteralString")) { - requiredLiteral = property.Value.GetString(); + requiredLiteralString = property.Value.GetString(); + continue; + } + if (property.NameEquals("requiredLiteralInt")) + { + requiredLiteralInt = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("requiredLiteralBool")) + { + requiredLiteralBool = property.Value.GetBoolean(); continue; } } - return new Thing(name, requiredUnion, requiredLiteral); + return new Thing(name, requiredUnion); } /// Deserializes the model from a raw response. diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs index f29f0c67ce9..2d9475f2709 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs @@ -16,24 +16,27 @@ public partial class Thing /// Initializes a new instance of Thing. /// /// - /// - /// , or is null. - public Thing(string name, string requiredUnion, string requiredLiteral) + /// or is null. + public Thing(string name, string requiredUnion) { Argument.AssertNotNull(name, nameof(name)); Argument.AssertNotNull(requiredUnion, nameof(requiredUnion)); - Argument.AssertNotNull(requiredLiteral, nameof(requiredLiteral)); Name = name; RequiredUnion = requiredUnion; - RequiredLiteral = requiredLiteral; } /// Gets or sets the name. public string Name { get; set; } /// Gets or sets the required union. public string RequiredUnion { get; set; } - /// Gets or sets the required literal. - public string RequiredLiteral { get; set; } + /// Gets the required literal string. + internal string RequiredLiteralString { get; } = "accept"; + + /// Gets the required literal int. + internal int RequiredLiteralInt { get; } = 123; + + /// Gets the required literal bool. + internal bool RequiredLiteralBool { get; } = false; } } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json index 1d9134b9c7f..223a33cf238 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json +++ b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json @@ -172,8 +172,8 @@ }, { "$id": "27", - "Name": "requiredLiteral", - "SerializedName": "requiredLiteral", + "Name": "requiredLiteralString", + "SerializedName": "requiredLiteralString", "Description": "", "Type": { "$id": "28", @@ -190,23 +190,65 @@ "IsRequired": true, "IsReadOnly": false, "IsDiscriminator": false + }, + { + "$id": "30", + "Name": "requiredLiteralInt", + "SerializedName": "requiredLiteralInt", + "Description": "", + "Type": { + "$id": "31", + "Name": "Literal", + "LiteralValueType": { + "$id": "32", + "Name": "Number", + "Kind": "Int32", + "IsNullable": false + }, + "Value": 123, + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false + }, + { + "$id": "33", + "Name": "requiredLiteralBool", + "SerializedName": "requiredLiteralBool", + "Description": "", + "Type": { + "$id": "34", + "Name": "Literal", + "LiteralValueType": { + "$id": "35", + "Name": "Boolean", + "Kind": "Boolean", + "IsNullable": false + }, + "Value": false, + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false } ] }, { - "$id": "30", + "$id": "36", "Name": "Friend", "Namespace": "CadlFirstTest", "IsNullable": false, "Usage": "RoundTrip", "Properties": [ { - "$id": "31", + "$id": "37", "Name": "name", "SerializedName": "name", "Description": "", "Type": { - "$id": "32", + "$id": "38", "Name": "string", "Kind": "String", "IsNullable": false @@ -218,19 +260,19 @@ ] }, { - "$id": "33", + "$id": "39", "Name": "RoundTripModel", "Namespace": "CadlFirstTest", "IsNullable": false, "Usage": "Input", "Properties": [ { - "$id": "34", + "$id": "40", "Name": "requiredString", "SerializedName": "requiredString", "Description": "", "Type": { - "$id": "35", + "$id": "41", "Name": "string", "Kind": "String", "IsNullable": false @@ -240,12 +282,12 @@ "IsDiscriminator": false }, { - "$id": "36", + "$id": "42", "Name": "requiredInt", "SerializedName": "requiredInt", "Description": "", "Type": { - "$id": "37", + "$id": "43", "Name": "int32", "Kind": "Int32", "IsNullable": false @@ -255,12 +297,12 @@ "IsDiscriminator": false }, { - "$id": "38", + "$id": "44", "Name": "requiredCollection", "SerializedName": "requiredCollection", "Description": "", "Type": { - "$id": "39", + "$id": "45", "Name": "Array", "ElementType": { "$ref": "2" @@ -272,15 +314,15 @@ "IsDiscriminator": false }, { - "$id": "40", + "$id": "46", "Name": "requiredDictionary", "SerializedName": "requiredDictionary", "Description": "", "Type": { - "$id": "41", + "$id": "47", "Name": "Dictionary", "KeyType": { - "$id": "42", + "$id": "48", "Name": "string", "Kind": "String", "IsNullable": false @@ -295,7 +337,7 @@ "IsDiscriminator": false }, { - "$id": "43", + "$id": "49", "Name": "requiredModel", "SerializedName": "requiredModel", "Description": "", @@ -311,23 +353,23 @@ ], "Clients": [ { - "$id": "44", + "$id": "50", "Name": "CadlFirstTestClient", "Description": "This is a sample cadl project.", "Operations": [ { - "$id": "45", + "$id": "51", "Name": "topAction", "ResourceName": "CadlFirstTest", "Description": "top level method", "Parameters": [ { - "$id": "46", + "$id": "52", "Name": "host", "NameInRequest": "host", "Description": "Endpoint Service", "Type": { - "$id": "47", + "$id": "53", "Name": "String", "Kind": "String", "IsNullable": false @@ -342,9 +384,9 @@ "Explode": false, "Kind": "Client", "DefaultValue": { - "$id": "48", + "$id": "54", "Type": { - "$id": "49", + "$id": "55", "Name": "String", "Kind": "String", "IsNullable": false @@ -353,11 +395,11 @@ } }, { - "$id": "50", + "$id": "56", "Name": "action", "NameInRequest": "action", "Type": { - "$id": "51", + "$id": "57", "Name": "string", "Kind": "String", "IsNullable": false @@ -373,11 +415,11 @@ "Kind": "Method" }, { - "$id": "52", + "$id": "58", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "53", + "$id": "59", "Name": "String", "Kind": "String", "IsNullable": false @@ -392,20 +434,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "54", + "$id": "60", "Type": { - "$ref": "53" + "$ref": "59" }, "Value": "application/json" } }, { - "$id": "55", + "$id": "61", "Name": "apiVersion", "NameInRequest": "api-version", "Description": "", "Type": { - "$id": "56", + "$id": "62", "Name": "String", "Kind": "String", "IsNullable": false @@ -420,9 +462,9 @@ "Explode": false, "Kind": "Client", "DefaultValue": { - "$id": "57", + "$id": "63", "Type": { - "$id": "58", + "$id": "64", "Name": "String", "Kind": "String", "IsNullable": false @@ -433,7 +475,7 @@ ], "Responses": [ { - "$id": "59", + "$id": "65", "StatusCodes": [ 200 ], @@ -453,20 +495,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "60", + "$id": "66", "Name": "topAction2", "ResourceName": "CadlFirstTest", "Description": "top level method2", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "61", + "$id": "67", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "62", + "$id": "68", "Name": "String", "Kind": "String", "IsNullable": false @@ -481,20 +523,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "63", + "$id": "69", "Type": { - "$ref": "62" + "$ref": "68" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "64", + "$id": "70", "StatusCodes": [ 200 ], @@ -514,16 +556,16 @@ "GenerateConvenienceMethod": false }, { - "$id": "65", + "$id": "71", "Name": "patchAction", "ResourceName": "CadlFirstTest", "Description": "top level patch", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "66", + "$id": "72", "Name": "body", "NameInRequest": "body", "Type": { @@ -540,11 +582,11 @@ "Kind": "Method" }, { - "$id": "67", + "$id": "73", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "68", + "$id": "74", "Name": "String", "Kind": "String", "IsNullable": false @@ -559,19 +601,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "69", + "$id": "75", "Type": { - "$ref": "68" + "$ref": "74" }, "Value": "application/json" } }, { - "$id": "70", + "$id": "76", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "71", + "$id": "77", "Name": "String", "Kind": "String", "IsNullable": false @@ -586,20 +628,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "72", + "$id": "78", "Type": { - "$ref": "71" + "$ref": "77" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "73", + "$id": "79", "StatusCodes": [ 200 ], @@ -622,16 +664,16 @@ "GenerateConvenienceMethod": false }, { - "$id": "74", + "$id": "80", "Name": "anonymousBody", "ResourceName": "CadlFirstTest", "Description": "body parameter without body decorator", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "75", + "$id": "81", "Name": "Thing", "NameInRequest": "Thing", "Type": { @@ -648,11 +690,11 @@ "Kind": "Method" }, { - "$id": "76", + "$id": "82", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "77", + "$id": "83", "Name": "String", "Kind": "String", "IsNullable": false @@ -667,19 +709,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "78", + "$id": "84", "Type": { - "$ref": "77" + "$ref": "83" }, "Value": "application/json" } }, { - "$id": "79", + "$id": "85", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "80", + "$id": "86", "Name": "String", "Kind": "String", "IsNullable": false @@ -694,20 +736,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "81", + "$id": "87", "Type": { - "$ref": "80" + "$ref": "86" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "82", + "$id": "88", "StatusCodes": [ 200 ], @@ -730,20 +772,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "83", + "$id": "89", "Name": "friendlyModel", "ResourceName": "CadlFirstTest", "Description": "Model can have its friendly name", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "84", + "$id": "90", "Name": "NotFriend", "NameInRequest": "NotFriend", "Type": { - "$ref": "30" + "$ref": "36" }, "Location": "Body", "IsRequired": true, @@ -756,11 +798,11 @@ "Kind": "Method" }, { - "$id": "85", + "$id": "91", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "86", + "$id": "92", "Name": "String", "Kind": "String", "IsNullable": false @@ -775,19 +817,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "87", + "$id": "93", "Type": { - "$ref": "86" + "$ref": "92" }, "Value": "application/json" } }, { - "$id": "88", + "$id": "94", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "89", + "$id": "95", "Name": "String", "Kind": "String", "IsNullable": false @@ -802,25 +844,25 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "90", + "$id": "96", "Type": { - "$ref": "89" + "$ref": "95" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "91", + "$id": "97", "StatusCodes": [ 200 ], "BodyType": { - "$ref": "30" + "$ref": "36" }, "BodyMediaType": "Json", "Headers": [], @@ -838,19 +880,19 @@ "GenerateConvenienceMethod": true }, { - "$id": "92", + "$id": "98", "Name": "addTimeHeader", "ResourceName": "CadlFirstTest", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "93", + "$id": "99", "Name": "repeatabilityFirstSent", "NameInRequest": "Repeatability-First-Sent", "Type": { - "$id": "94", + "$id": "100", "Name": "zonedDateTime", "Kind": "DateTime", "IsNullable": false @@ -866,11 +908,11 @@ "Kind": "Method" }, { - "$id": "95", + "$id": "101", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "96", + "$id": "102", "Name": "String", "Kind": "String", "IsNullable": false @@ -885,20 +927,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "97", + "$id": "103", "Type": { - "$ref": "96" + "$ref": "102" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "98", + "$id": "104", "StatusCodes": [ 204 ], @@ -915,20 +957,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "99", + "$id": "105", "Name": "sayHi", "ResourceName": "Demo", "Description": "Return hi", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "100", + "$id": "106", "Name": "headParameter", "NameInRequest": "head-parameter", "Type": { - "$id": "101", + "$id": "107", "Name": "string", "Kind": "String", "IsNullable": false @@ -944,11 +986,11 @@ "Kind": "Method" }, { - "$id": "102", + "$id": "108", "Name": "queryParameter", "NameInRequest": "queryParameter", "Type": { - "$id": "103", + "$id": "109", "Name": "string", "Kind": "String", "IsNullable": false @@ -964,11 +1006,11 @@ "Kind": "Method" }, { - "$id": "104", + "$id": "110", "Name": "optionalQuery", "NameInRequest": "optionalQuery", "Type": { - "$id": "105", + "$id": "111", "Name": "string", "Kind": "String", "IsNullable": false @@ -984,11 +1026,11 @@ "Kind": "Method" }, { - "$id": "106", + "$id": "112", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "107", + "$id": "113", "Name": "String", "Kind": "String", "IsNullable": false @@ -1003,20 +1045,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "108", + "$id": "114", "Type": { - "$ref": "107" + "$ref": "113" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "109", + "$id": "115", "StatusCodes": [ 200 ], @@ -1036,20 +1078,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "110", + "$id": "116", "Name": "helloAgain", "ResourceName": "Demo2", "Description": "Return hi again", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "111", + "$id": "117", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "112", + "$id": "118", "Name": "string", "Kind": "String", "IsNullable": false @@ -1065,14 +1107,14 @@ "Kind": "Method" }, { - "$id": "113", + "$id": "119", "Name": "contentType", "NameInRequest": "content-type", "Type": { - "$id": "114", + "$id": "120", "Name": "Literal", "LiteralValueType": { - "$id": "115", + "$id": "121", "Name": "String", "Kind": "String", "IsNullable": false @@ -1082,9 +1124,9 @@ }, "Location": "Header", "DefaultValue": { - "$id": "116", + "$id": "122", "Type": { - "$ref": "114" + "$ref": "120" }, "Value": "text/plain" }, @@ -1098,11 +1140,11 @@ "Kind": "Constant" }, { - "$id": "117", + "$id": "123", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "118", + "$id": "124", "Name": "string", "Kind": "String", "IsNullable": false @@ -1118,11 +1160,11 @@ "Kind": "Method" }, { - "$id": "119", + "$id": "125", "Name": "action", "NameInRequest": "action", "Type": { - "$ref": "33" + "$ref": "39" }, "Location": "Body", "IsRequired": true, @@ -1135,11 +1177,11 @@ "Kind": "Method" }, { - "$id": "120", + "$id": "126", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "121", + "$id": "127", "Name": "String", "Kind": "String", "IsNullable": false @@ -1154,20 +1196,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "122", + "$id": "128", "Type": { - "$ref": "121" + "$ref": "127" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "123", + "$id": "129", "StatusCodes": [ 200 ], @@ -1190,20 +1232,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "124", + "$id": "130", "Name": "noContentType", "ResourceName": "Demo2", "Description": "Return hi again", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "125", + "$id": "131", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "126", + "$id": "132", "Name": "string", "Kind": "String", "IsNullable": false @@ -1219,11 +1261,11 @@ "Kind": "Method" }, { - "$id": "127", + "$id": "133", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "128", + "$id": "134", "Name": "string", "Kind": "String", "IsNullable": false @@ -1239,11 +1281,11 @@ "Kind": "Method" }, { - "$id": "129", + "$id": "135", "Name": "action", "NameInRequest": "action", "Type": { - "$ref": "33" + "$ref": "39" }, "Location": "Body", "IsRequired": true, @@ -1256,11 +1298,11 @@ "Kind": "Method" }, { - "$id": "130", + "$id": "136", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "131", + "$id": "137", "Name": "String", "Kind": "String", "IsNullable": false @@ -1275,19 +1317,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "132", + "$id": "138", "Type": { - "$ref": "131" + "$ref": "137" }, "Value": "application/json" } }, { - "$id": "133", + "$id": "139", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "134", + "$id": "140", "Name": "String", "Kind": "String", "IsNullable": false @@ -1302,20 +1344,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "135", + "$id": "141", "Type": { - "$ref": "134" + "$ref": "140" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "136", + "$id": "142", "StatusCodes": [ 200 ], @@ -1338,20 +1380,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "137", + "$id": "143", "Name": "helloDemo2", "ResourceName": "Demo2", "Description": "Return hi in demo2", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "138", + "$id": "144", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "139", + "$id": "145", "Name": "String", "Kind": "String", "IsNullable": false @@ -1366,20 +1408,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "140", + "$id": "146", "Type": { - "$ref": "139" + "$ref": "145" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "141", + "$id": "147", "StatusCodes": [ 200 ], @@ -1399,20 +1441,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "142", + "$id": "148", "Name": "getUnknownValue", "ResourceName": "EnumTest", "Description": "get extensible enum", "Parameters": [ { - "$ref": "46" + "$ref": "52" }, { - "$id": "143", + "$id": "149", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "144", + "$id": "150", "Name": "String", "Kind": "String", "IsNullable": false @@ -1427,20 +1469,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "145", + "$id": "151", "Type": { - "$ref": "144" + "$ref": "150" }, "Value": "application/json" } }, { - "$ref": "55" + "$ref": "61" } ], "Responses": [ { - "$id": "146", + "$id": "152", "StatusCodes": [ 200 ], @@ -1461,19 +1503,19 @@ } ], "Protocol": { - "$id": "147" + "$id": "153" }, "Creatable": true } ], "Auth": { - "$id": "148", + "$id": "154", "ApiKey": { - "$id": "149", + "$id": "155", "Name": "x-ms-api-key" }, "OAuth2": { - "$id": "150", + "$id": "156", "Scopes": [ "https://api.example.com/.default" ] From e39b60c3406e490dce880b0c14100539d905c497 Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 31 Jan 2023 11:25:53 +0800 Subject: [PATCH 02/10] add initial support to differentiate integer from float - add simple value type check for `number` literal value in emitter, we only support int32 and float64 - change converter to support float64 which will be mapped to double in csharp - drop converter support for int64 since it's not emitted in the emitter - update test cases - minor refactoring --- .../Input/CadlInputLiteralTypeConverter.cs | 12 +- .../Emitter.Csharp/src/lib/model.ts | 6 +- .../FirstTest-Cadl/FirstTest-Cadl.cadl | 11 +- .../Generated/CadlFirstTestClient.cs | 97 ++++ .../Generated/Docs/CadlFirstTestClient.xml | 122 +++++ .../Generated/Models/Thing.Serialization.cs | 8 + .../FirstTest-Cadl/Generated/Models/Thing.cs | 3 + .../FirstTest-Cadl/Generated/cadl.json | 507 ++++++++++++------ 8 files changed, 593 insertions(+), 173 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Input/CadlInputLiteralTypeConverter.cs b/src/AutoRest.CSharp/Common/Input/CadlInputLiteralTypeConverter.cs index 04d1ddb78d4..f57a2271f45 100644 --- a/src/AutoRest.CSharp/Common/Input/CadlInputLiteralTypeConverter.cs +++ b/src/AutoRest.CSharp/Common/Input/CadlInputLiteralTypeConverter.cs @@ -85,8 +85,8 @@ public static object ReadLiteralValue(ref Utf8JsonReader reader, string property Object? value = null; switch (type) { - case InputPrimitiveType primitype: - switch (primitype.Kind) + case InputPrimitiveType primitiveType: + switch (primitiveType.Kind) { case InputTypeKind.String: value = reader.GetString() ?? throw new JsonException(); @@ -94,19 +94,19 @@ public static object ReadLiteralValue(ref Utf8JsonReader reader, string property case InputTypeKind.Int32: value = reader.GetInt32(); break; - case InputTypeKind.Int64: - value = reader.GetInt64(); + case InputTypeKind.Float64: + value = reader.GetDouble(); break; case InputTypeKind.Boolean: value = reader.GetBoolean(); break; default: - throw new JsonException($"Not supported litreal type {primitype.Kind}."); + throw new JsonException($"Not supported literal type {primitiveType.Kind}."); } break; default: - throw new JsonException($"Not supported litreal type {type.Name}."); + throw new JsonException($"Not supported literal type {type.Name}."); } reader.Read(); return value; diff --git a/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts b/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts index da3f2d78a7e..387a3a3e98b 100644 --- a/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts +++ b/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts @@ -70,7 +70,11 @@ export function mapCadlTypeToCSharpInputTypeKind( case "Enum": return InputTypeKind.Enum; case "Number": - return InputTypeKind.Int32; + let nubmerValue = cadlType.value; + if (nubmerValue % 1 === 0) { + return InputTypeKind.Int32; + }; + return InputTypeKind.Float64; case "Boolean": return InputTypeKind.Boolean; case "String": diff --git a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl index 56ced2c6134..398c550f85b 100644 --- a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl +++ b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl @@ -53,9 +53,8 @@ model Thing { @doc("required literal int") requiredLiteralInt: 123; - // TODO: how do we identify if the number literal is a float or double? -// @doc("required literal float") -// requiredLiteralFloat: 1.23; + @doc("required literal double") + requiredLiteralDouble: 1.23; @doc("required literal bool") requiredLiteralBool: false; @@ -119,6 +118,12 @@ namespace Hello.Demo2 { @doc("Return hi in demo2") @get op helloDemo2(): Thing; + + @route("/helloLiteral") + @convenienceAPI + @doc("Send literal parameters") + @get + op helloLiteral(@header p1: "test", @path p2: 123, @query p3: true): Thing; } @route("/top") diff --git a/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs b/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs index 7d38f85705c..833581c51df 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs @@ -766,6 +766,86 @@ public virtual Response HelloDemo2(RequestContext context = null) } } + /// Send literal parameters. + /// The cancellation token to use. + public virtual async Task> HelloLiteralValueAsync(CancellationToken cancellationToken = default) + { + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteralValue"); + scope.Start(); + try + { + RequestContext context = FromCancellationToken(cancellationToken); + Response response = await HelloLiteralAsync(context).ConfigureAwait(false); + return Response.FromValue(Thing.FromResponse(response), response); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// Send literal parameters. + /// The cancellation token to use. + public virtual Response HelloLiteralValue(CancellationToken cancellationToken = default) + { + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteralValue"); + scope.Start(); + try + { + RequestContext context = FromCancellationToken(cancellationToken); + Response response = HelloLiteral(context); + return Response.FromValue(Thing.FromResponse(response), response); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// Send literal parameters. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// Service returned a non-success status code. + /// The response returned from the service. Details of the response body schema are in the Remarks section below. + /// + public virtual async Task HelloLiteralAsync(RequestContext context = null) + { + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteral"); + scope.Start(); + try + { + using HttpMessage message = CreateHelloLiteralRequest(context); + return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// Send literal parameters. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// Service returned a non-success status code. + /// The response returned from the service. Details of the response body schema are in the Remarks section below. + /// + public virtual Response HelloLiteral(RequestContext context = null) + { + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteral"); + scope.Start(); + try + { + using HttpMessage message = CreateHelloLiteralRequest(context); + return _pipeline.ProcessMessage(message, context); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + /// get extensible enum. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// Service returned a non-success status code. @@ -973,6 +1053,23 @@ internal HttpMessage CreateHelloDemo2Request(RequestContext context) return message; } + internal HttpMessage CreateHelloLiteralRequest(RequestContext context) + { + var message = _pipeline.CreateMessage(context, ResponseClassifier200); + var request = message.Request; + request.Method = RequestMethod.Get; + var uri = new RawRequestUriBuilder(); + uri.Reset(_endpoint); + uri.AppendPath("/helloLiteral/", false); + uri.AppendPath(123, true); + uri.AppendQuery("p3", true, true); + uri.AppendQuery("api-version", _apiVersion, true); + request.Uri = uri; + request.Headers.Add("p1", "test"); + request.Headers.Add("Accept", "application/json"); + return message; + } + internal HttpMessage CreateGetUnknownValueRequest(RequestContext context) { var message = _pipeline.CreateMessage(context, ResponseClassifier200); diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml index 8d12a1b1247..a8ab069025e 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml +++ b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml @@ -15,6 +15,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -29,6 +30,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -49,6 +51,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -63,6 +66,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -83,6 +87,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -97,6 +102,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -117,6 +123,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -131,6 +138,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -149,6 +157,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }; @@ -159,6 +168,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -173,6 +183,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -185,6 +196,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -203,6 +215,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }; @@ -213,6 +226,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -227,6 +241,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -239,6 +254,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -257,6 +273,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }; @@ -267,6 +284,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -281,6 +299,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -293,6 +312,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -311,6 +331,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }; @@ -321,6 +342,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -335,6 +357,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -347,6 +370,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -483,6 +507,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> This sample shows how to call SayHiAsync with all parameters, and how to parse the result. @@ -497,6 +522,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -511,6 +537,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -531,6 +558,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> This sample shows how to call SayHi with all parameters, and how to parse the result. @@ -545,6 +573,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -559,6 +588,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -586,6 +616,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }, }; @@ -597,6 +628,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -616,6 +648,7 @@ Schema for RoundTripModel: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. }, # Required. } @@ -629,6 +662,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -656,6 +690,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }, }; @@ -667,6 +702,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -686,6 +722,7 @@ Schema for RoundTripModel: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. }, # Required. } @@ -699,6 +736,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -726,6 +764,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }, }; @@ -737,6 +776,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -756,6 +796,7 @@ Schema for RoundTripModel: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. }, # Required. } @@ -769,6 +810,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -796,6 +838,7 @@ var data = new { requiredUnion = new {}, requiredLiteralString = new {}, requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, requiredLiteralBool = new {}, }, }; @@ -807,6 +850,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -826,6 +870,7 @@ Schema for RoundTripModel: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. }, # Required. } @@ -839,6 +884,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -859,6 +905,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -873,6 +920,7 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } @@ -893,6 +941,7 @@ Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); ]]> @@ -907,6 +956,79 @@ Schema for Thing: requiredUnion: Union, # Required. requiredLiteralString: Literal, # Required. requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. + requiredLiteralBool: Literal, # Required. +} + + + + + + +This sample shows how to call HelloLiteralAsync and parse the result. +"); +var client = new CadlFirstTestClient(credential); + +Response response = await client.HelloLiteralAsync(); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +]]> + + +Below is the JSON schema for the response payload. + +Response Body: + +Schema for Thing: +{ + name: string, # Required. + requiredUnion: Union, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. + requiredLiteralBool: Literal, # Required. +} + + + + + + +This sample shows how to call HelloLiteral and parse the result. +"); +var client = new CadlFirstTestClient(credential); + +Response response = client.HelloLiteral(); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +]]> + + +Below is the JSON schema for the response payload. + +Response Body: + +Schema for Thing: +{ + name: string, # Required. + requiredUnion: Union, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs index e10fd687308..232dd5bf4cd 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs @@ -24,6 +24,8 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) writer.WriteStringValue(RequiredLiteralString); writer.WritePropertyName("requiredLiteralInt"); writer.WriteNumberValue(RequiredLiteralInt); + writer.WritePropertyName("requiredLiteralDouble"); + writer.WriteNumberValue(RequiredLiteralDouble); writer.WritePropertyName("requiredLiteralBool"); writer.WriteBooleanValue(RequiredLiteralBool); writer.WriteEndObject(); @@ -35,6 +37,7 @@ internal static Thing DeserializeThing(JsonElement element) string requiredUnion = default; string requiredLiteralString = default; int requiredLiteralInt = default; + double requiredLiteralDouble = default; bool requiredLiteralBool = default; foreach (var property in element.EnumerateObject()) { @@ -58,6 +61,11 @@ internal static Thing DeserializeThing(JsonElement element) requiredLiteralInt = property.Value.GetInt32(); continue; } + if (property.NameEquals("requiredLiteralDouble")) + { + requiredLiteralDouble = property.Value.GetDouble(); + continue; + } if (property.NameEquals("requiredLiteralBool")) { requiredLiteralBool = property.Value.GetBoolean(); diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs index 2d9475f2709..e973c8f48ff 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs @@ -36,6 +36,9 @@ public Thing(string name, string requiredUnion) /// Gets the required literal int. internal int RequiredLiteralInt { get; } = 123; + /// Gets the required literal double. + internal double RequiredLiteralDouble { get; } = 1.23; + /// Gets the required literal bool. internal bool RequiredLiteralBool { get; } = false; } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json index 223a33cf238..a9e7128054c 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json +++ b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json @@ -214,14 +214,35 @@ }, { "$id": "33", - "Name": "requiredLiteralBool", - "SerializedName": "requiredLiteralBool", + "Name": "requiredLiteralDouble", + "SerializedName": "requiredLiteralDouble", "Description": "", "Type": { "$id": "34", "Name": "Literal", "LiteralValueType": { "$id": "35", + "Name": "Number", + "Kind": "Float64", + "IsNullable": false + }, + "Value": 1.23, + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false + }, + { + "$id": "36", + "Name": "requiredLiteralBool", + "SerializedName": "requiredLiteralBool", + "Description": "", + "Type": { + "$id": "37", + "Name": "Literal", + "LiteralValueType": { + "$id": "38", "Name": "Boolean", "Kind": "Boolean", "IsNullable": false @@ -236,19 +257,19 @@ ] }, { - "$id": "36", + "$id": "39", "Name": "Friend", "Namespace": "CadlFirstTest", "IsNullable": false, "Usage": "RoundTrip", "Properties": [ { - "$id": "37", + "$id": "40", "Name": "name", "SerializedName": "name", "Description": "", "Type": { - "$id": "38", + "$id": "41", "Name": "string", "Kind": "String", "IsNullable": false @@ -260,19 +281,19 @@ ] }, { - "$id": "39", + "$id": "42", "Name": "RoundTripModel", "Namespace": "CadlFirstTest", "IsNullable": false, "Usage": "Input", "Properties": [ { - "$id": "40", + "$id": "43", "Name": "requiredString", "SerializedName": "requiredString", "Description": "", "Type": { - "$id": "41", + "$id": "44", "Name": "string", "Kind": "String", "IsNullable": false @@ -282,12 +303,12 @@ "IsDiscriminator": false }, { - "$id": "42", + "$id": "45", "Name": "requiredInt", "SerializedName": "requiredInt", "Description": "", "Type": { - "$id": "43", + "$id": "46", "Name": "int32", "Kind": "Int32", "IsNullable": false @@ -297,12 +318,12 @@ "IsDiscriminator": false }, { - "$id": "44", + "$id": "47", "Name": "requiredCollection", "SerializedName": "requiredCollection", "Description": "", "Type": { - "$id": "45", + "$id": "48", "Name": "Array", "ElementType": { "$ref": "2" @@ -314,15 +335,15 @@ "IsDiscriminator": false }, { - "$id": "46", + "$id": "49", "Name": "requiredDictionary", "SerializedName": "requiredDictionary", "Description": "", "Type": { - "$id": "47", + "$id": "50", "Name": "Dictionary", "KeyType": { - "$id": "48", + "$id": "51", "Name": "string", "Kind": "String", "IsNullable": false @@ -337,7 +358,7 @@ "IsDiscriminator": false }, { - "$id": "49", + "$id": "52", "Name": "requiredModel", "SerializedName": "requiredModel", "Description": "", @@ -353,23 +374,23 @@ ], "Clients": [ { - "$id": "50", + "$id": "53", "Name": "CadlFirstTestClient", "Description": "This is a sample cadl project.", "Operations": [ { - "$id": "51", + "$id": "54", "Name": "topAction", "ResourceName": "CadlFirstTest", "Description": "top level method", "Parameters": [ { - "$id": "52", + "$id": "55", "Name": "host", "NameInRequest": "host", "Description": "Endpoint Service", "Type": { - "$id": "53", + "$id": "56", "Name": "String", "Kind": "String", "IsNullable": false @@ -384,9 +405,9 @@ "Explode": false, "Kind": "Client", "DefaultValue": { - "$id": "54", + "$id": "57", "Type": { - "$id": "55", + "$id": "58", "Name": "String", "Kind": "String", "IsNullable": false @@ -395,11 +416,11 @@ } }, { - "$id": "56", + "$id": "59", "Name": "action", "NameInRequest": "action", "Type": { - "$id": "57", + "$id": "60", "Name": "string", "Kind": "String", "IsNullable": false @@ -415,11 +436,11 @@ "Kind": "Method" }, { - "$id": "58", + "$id": "61", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "59", + "$id": "62", "Name": "String", "Kind": "String", "IsNullable": false @@ -434,20 +455,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "60", + "$id": "63", "Type": { - "$ref": "59" + "$ref": "62" }, "Value": "application/json" } }, { - "$id": "61", + "$id": "64", "Name": "apiVersion", "NameInRequest": "api-version", "Description": "", "Type": { - "$id": "62", + "$id": "65", "Name": "String", "Kind": "String", "IsNullable": false @@ -462,9 +483,9 @@ "Explode": false, "Kind": "Client", "DefaultValue": { - "$id": "63", + "$id": "66", "Type": { - "$id": "64", + "$id": "67", "Name": "String", "Kind": "String", "IsNullable": false @@ -475,7 +496,7 @@ ], "Responses": [ { - "$id": "65", + "$id": "68", "StatusCodes": [ 200 ], @@ -495,20 +516,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "66", + "$id": "69", "Name": "topAction2", "ResourceName": "CadlFirstTest", "Description": "top level method2", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "67", + "$id": "70", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "68", + "$id": "71", "Name": "String", "Kind": "String", "IsNullable": false @@ -523,20 +544,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "69", + "$id": "72", "Type": { - "$ref": "68" + "$ref": "71" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "70", + "$id": "73", "StatusCodes": [ 200 ], @@ -556,16 +577,16 @@ "GenerateConvenienceMethod": false }, { - "$id": "71", + "$id": "74", "Name": "patchAction", "ResourceName": "CadlFirstTest", "Description": "top level patch", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "72", + "$id": "75", "Name": "body", "NameInRequest": "body", "Type": { @@ -582,11 +603,11 @@ "Kind": "Method" }, { - "$id": "73", + "$id": "76", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "74", + "$id": "77", "Name": "String", "Kind": "String", "IsNullable": false @@ -601,19 +622,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "75", + "$id": "78", "Type": { - "$ref": "74" + "$ref": "77" }, "Value": "application/json" } }, { - "$id": "76", + "$id": "79", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "77", + "$id": "80", "Name": "String", "Kind": "String", "IsNullable": false @@ -628,20 +649,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "78", + "$id": "81", "Type": { - "$ref": "77" + "$ref": "80" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "79", + "$id": "82", "StatusCodes": [ 200 ], @@ -664,16 +685,16 @@ "GenerateConvenienceMethod": false }, { - "$id": "80", + "$id": "83", "Name": "anonymousBody", "ResourceName": "CadlFirstTest", "Description": "body parameter without body decorator", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "81", + "$id": "84", "Name": "Thing", "NameInRequest": "Thing", "Type": { @@ -690,11 +711,11 @@ "Kind": "Method" }, { - "$id": "82", + "$id": "85", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "83", + "$id": "86", "Name": "String", "Kind": "String", "IsNullable": false @@ -709,19 +730,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "84", + "$id": "87", "Type": { - "$ref": "83" + "$ref": "86" }, "Value": "application/json" } }, { - "$id": "85", + "$id": "88", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "86", + "$id": "89", "Name": "String", "Kind": "String", "IsNullable": false @@ -736,20 +757,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "87", + "$id": "90", "Type": { - "$ref": "86" + "$ref": "89" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "88", + "$id": "91", "StatusCodes": [ 200 ], @@ -772,20 +793,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "89", + "$id": "92", "Name": "friendlyModel", "ResourceName": "CadlFirstTest", "Description": "Model can have its friendly name", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "90", + "$id": "93", "Name": "NotFriend", "NameInRequest": "NotFriend", "Type": { - "$ref": "36" + "$ref": "39" }, "Location": "Body", "IsRequired": true, @@ -798,11 +819,11 @@ "Kind": "Method" }, { - "$id": "91", + "$id": "94", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "92", + "$id": "95", "Name": "String", "Kind": "String", "IsNullable": false @@ -817,19 +838,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "93", + "$id": "96", "Type": { - "$ref": "92" + "$ref": "95" }, "Value": "application/json" } }, { - "$id": "94", + "$id": "97", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "95", + "$id": "98", "Name": "String", "Kind": "String", "IsNullable": false @@ -844,25 +865,25 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "96", + "$id": "99", "Type": { - "$ref": "95" + "$ref": "98" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "97", + "$id": "100", "StatusCodes": [ 200 ], "BodyType": { - "$ref": "36" + "$ref": "39" }, "BodyMediaType": "Json", "Headers": [], @@ -880,19 +901,19 @@ "GenerateConvenienceMethod": true }, { - "$id": "98", + "$id": "101", "Name": "addTimeHeader", "ResourceName": "CadlFirstTest", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "99", + "$id": "102", "Name": "repeatabilityFirstSent", "NameInRequest": "Repeatability-First-Sent", "Type": { - "$id": "100", + "$id": "103", "Name": "zonedDateTime", "Kind": "DateTime", "IsNullable": false @@ -908,11 +929,11 @@ "Kind": "Method" }, { - "$id": "101", + "$id": "104", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "102", + "$id": "105", "Name": "String", "Kind": "String", "IsNullable": false @@ -927,20 +948,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "103", + "$id": "106", "Type": { - "$ref": "102" + "$ref": "105" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "104", + "$id": "107", "StatusCodes": [ 204 ], @@ -957,20 +978,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "105", + "$id": "108", "Name": "sayHi", "ResourceName": "Demo", "Description": "Return hi", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "106", + "$id": "109", "Name": "headParameter", "NameInRequest": "head-parameter", "Type": { - "$id": "107", + "$id": "110", "Name": "string", "Kind": "String", "IsNullable": false @@ -986,11 +1007,11 @@ "Kind": "Method" }, { - "$id": "108", + "$id": "111", "Name": "queryParameter", "NameInRequest": "queryParameter", "Type": { - "$id": "109", + "$id": "112", "Name": "string", "Kind": "String", "IsNullable": false @@ -1006,11 +1027,11 @@ "Kind": "Method" }, { - "$id": "110", + "$id": "113", "Name": "optionalQuery", "NameInRequest": "optionalQuery", "Type": { - "$id": "111", + "$id": "114", "Name": "string", "Kind": "String", "IsNullable": false @@ -1026,11 +1047,11 @@ "Kind": "Method" }, { - "$id": "112", + "$id": "115", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "113", + "$id": "116", "Name": "String", "Kind": "String", "IsNullable": false @@ -1045,20 +1066,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "114", + "$id": "117", "Type": { - "$ref": "113" + "$ref": "116" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "115", + "$id": "118", "StatusCodes": [ 200 ], @@ -1078,20 +1099,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "116", + "$id": "119", "Name": "helloAgain", "ResourceName": "Demo2", "Description": "Return hi again", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "117", + "$id": "120", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "118", + "$id": "121", "Name": "string", "Kind": "String", "IsNullable": false @@ -1107,14 +1128,14 @@ "Kind": "Method" }, { - "$id": "119", + "$id": "122", "Name": "contentType", "NameInRequest": "content-type", "Type": { - "$id": "120", + "$id": "123", "Name": "Literal", "LiteralValueType": { - "$id": "121", + "$id": "124", "Name": "String", "Kind": "String", "IsNullable": false @@ -1124,9 +1145,9 @@ }, "Location": "Header", "DefaultValue": { - "$id": "122", + "$id": "125", "Type": { - "$ref": "120" + "$ref": "123" }, "Value": "text/plain" }, @@ -1140,11 +1161,11 @@ "Kind": "Constant" }, { - "$id": "123", + "$id": "126", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "124", + "$id": "127", "Name": "string", "Kind": "String", "IsNullable": false @@ -1160,11 +1181,11 @@ "Kind": "Method" }, { - "$id": "125", + "$id": "128", "Name": "action", "NameInRequest": "action", "Type": { - "$ref": "39" + "$ref": "42" }, "Location": "Body", "IsRequired": true, @@ -1177,11 +1198,11 @@ "Kind": "Method" }, { - "$id": "126", + "$id": "129", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "127", + "$id": "130", "Name": "String", "Kind": "String", "IsNullable": false @@ -1196,20 +1217,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "128", + "$id": "131", "Type": { - "$ref": "127" + "$ref": "130" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "129", + "$id": "132", "StatusCodes": [ 200 ], @@ -1232,20 +1253,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "130", + "$id": "133", "Name": "noContentType", "ResourceName": "Demo2", "Description": "Return hi again", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "131", + "$id": "134", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "132", + "$id": "135", "Name": "string", "Kind": "String", "IsNullable": false @@ -1261,11 +1282,11 @@ "Kind": "Method" }, { - "$id": "133", + "$id": "136", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "134", + "$id": "137", "Name": "string", "Kind": "String", "IsNullable": false @@ -1281,11 +1302,11 @@ "Kind": "Method" }, { - "$id": "135", + "$id": "138", "Name": "action", "NameInRequest": "action", "Type": { - "$ref": "39" + "$ref": "42" }, "Location": "Body", "IsRequired": true, @@ -1298,11 +1319,11 @@ "Kind": "Method" }, { - "$id": "136", + "$id": "139", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "137", + "$id": "140", "Name": "String", "Kind": "String", "IsNullable": false @@ -1317,19 +1338,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "138", + "$id": "141", "Type": { - "$ref": "137" + "$ref": "140" }, "Value": "application/json" } }, { - "$id": "139", + "$id": "142", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "140", + "$id": "143", "Name": "String", "Kind": "String", "IsNullable": false @@ -1344,20 +1365,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "141", + "$id": "144", "Type": { - "$ref": "140" + "$ref": "143" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "142", + "$id": "145", "StatusCodes": [ 200 ], @@ -1380,20 +1401,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "143", + "$id": "146", "Name": "helloDemo2", "ResourceName": "Demo2", "Description": "Return hi in demo2", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "144", + "$id": "147", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "145", + "$id": "148", "Name": "String", "Kind": "String", "IsNullable": false @@ -1408,20 +1429,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "146", + "$id": "149", "Type": { - "$ref": "145" + "$ref": "148" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "147", + "$id": "150", "StatusCodes": [ 200 ], @@ -1441,20 +1462,180 @@ "GenerateConvenienceMethod": true }, { - "$id": "148", + "$id": "151", + "Name": "helloLiteral", + "ResourceName": "Demo2", + "Description": "Send literal parameters", + "Parameters": [ + { + "$ref": "55" + }, + { + "$id": "152", + "Name": "p1", + "NameInRequest": "p1", + "Type": { + "$id": "153", + "Name": "Literal", + "LiteralValueType": { + "$id": "154", + "Name": "String", + "Kind": "String", + "IsNullable": false + }, + "Value": "test", + "IsNullable": false + }, + "Location": "Header", + "DefaultValue": { + "$id": "155", + "Type": { + "$ref": "153" + }, + "Value": "test" + }, + "IsRequired": true, + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": false, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Constant" + }, + { + "$id": "156", + "Name": "p2", + "NameInRequest": "p2", + "Type": { + "$id": "157", + "Name": "Literal", + "LiteralValueType": { + "$id": "158", + "Name": "Number", + "Kind": "Int32", + "IsNullable": false + }, + "Value": 123, + "IsNullable": false + }, + "Location": "Path", + "DefaultValue": { + "$id": "159", + "Type": { + "$ref": "157" + }, + "Value": 123 + }, + "IsRequired": true, + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": false, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Constant" + }, + { + "$id": "160", + "Name": "p3", + "NameInRequest": "p3", + "Type": { + "$id": "161", + "Name": "Literal", + "LiteralValueType": { + "$id": "162", + "Name": "Boolean", + "Kind": "Boolean", + "IsNullable": false + }, + "Value": true, + "IsNullable": false + }, + "Location": "Query", + "DefaultValue": { + "$id": "163", + "Type": { + "$ref": "161" + }, + "Value": true + }, + "IsRequired": true, + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": false, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Constant" + }, + { + "$id": "164", + "Name": "accept", + "NameInRequest": "Accept", + "Type": { + "$id": "165", + "Name": "String", + "Kind": "String", + "IsNullable": false + }, + "Location": "Header", + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": false, + "IsRequired": true, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Constant", + "DefaultValue": { + "$id": "166", + "Type": { + "$ref": "165" + }, + "Value": "application/json" + } + }, + { + "$ref": "64" + } + ], + "Responses": [ + { + "$id": "167", + "StatusCodes": [ + 200 + ], + "BodyType": { + "$ref": "18" + }, + "BodyMediaType": "Json", + "Headers": [], + "IsErrorResponse": false + } + ], + "HttpMethod": "GET", + "RequestBodyMediaType": "Json", + "Uri": "{host}", + "Path": "/helloLiteral/{p2}", + "BufferResponse": true, + "GenerateConvenienceMethod": true + }, + { + "$id": "168", "Name": "getUnknownValue", "ResourceName": "EnumTest", "Description": "get extensible enum", "Parameters": [ { - "$ref": "52" + "$ref": "55" }, { - "$id": "149", + "$id": "169", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "150", + "$id": "170", "Name": "String", "Kind": "String", "IsNullable": false @@ -1469,20 +1650,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "151", + "$id": "171", "Type": { - "$ref": "150" + "$ref": "170" }, "Value": "application/json" } }, { - "$ref": "61" + "$ref": "64" } ], "Responses": [ { - "$id": "152", + "$id": "172", "StatusCodes": [ 200 ], @@ -1503,19 +1684,19 @@ } ], "Protocol": { - "$id": "153" + "$id": "173" }, "Creatable": true } ], "Auth": { - "$id": "154", + "$id": "174", "ApiKey": { - "$id": "155", + "$id": "175", "Name": "x-ms-api-key" }, "OAuth2": { - "$id": "156", + "$id": "176", "Scopes": [ "https://api.example.com/.default" ] From f43f3df6761c0a2e6d59c5829b03b28b4502b39c Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 31 Jan 2023 16:29:19 +0800 Subject: [PATCH 03/10] add e2e test - modify cadl definition to add test case for literal model property - add a customization constructor of `CadlFirstTestClient` for test purpose - add mock api --- test/CadlRanchMockApis/src/FirstTest-Cadl.ts | 31 ++ .../LiteralValueTypeTests.cs | 39 ++ .../Customization/CadlFirstTestClient.cs | 30 ++ .../FirstTest-Cadl/FirstTest-Cadl.cadl | 22 +- .../Generated/CadlFirstTestClient.cs | 170 ++++++- .../Generated/Docs/CadlFirstTestClient.xml | 238 +++++++-- .../FirstTest-Cadl/Generated/cadl.json | 457 +++++++++++------- 7 files changed, 748 insertions(+), 239 deletions(-) create mode 100644 test/CadlRanchMockApis/src/FirstTest-Cadl.ts create mode 100644 test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs create mode 100644 test/TestProjects/FirstTest-Cadl/Customization/CadlFirstTestClient.cs diff --git a/test/CadlRanchMockApis/src/FirstTest-Cadl.ts b/test/CadlRanchMockApis/src/FirstTest-Cadl.ts new file mode 100644 index 00000000000..ad936b49110 --- /dev/null +++ b/test/CadlRanchMockApis/src/FirstTest-Cadl.ts @@ -0,0 +1,31 @@ +import { passOnSuccess, ScenarioMockApi, mockapi, json } from "@azure-tools/cadl-ranch-api"; + +/** + * Test mock server for `FirstTest-cadl` test project. + */ +export const Scenarios: Record = {}; + +Scenarios.FirstTestCadl_CreateLiteral = passOnSuccess([ + mockapi.post("/literal", (req) => { + req.expect.bodyEquals({ + name: "test", + requiredUnion: "test", + requiredLiteralString: "accept", + requiredLiteralInt: 123, + requiredLiteralDouble: 1.23, + requiredLiteralBool: false, + }); + return { + status: 200, + body: json({ + name: "literal", + requiredUnion: "union", + requiredLiteralString: "accept", + // below are useless + requiredLiteralInt: 12345, + requiredLiteralDouble: 123.45, + requiredLiteralBool: true, + }) + }; + }), +]); diff --git a/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs b/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs new file mode 100644 index 00000000000..8584f4db060 --- /dev/null +++ b/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using AutoRest.TestServer.Tests.Infrastructure; +using NUnit.Framework; +using CadlFirstTest.Models; +using CadlFirstTest; +using Azure.Identity; +using System.Threading.Tasks; +using static CadlRanchProjects.Tests.OAuth2Tests; +using Azure.Core; +using Authentication.OAuth2; +using Azure.Core.Pipeline; +using System; +using System.Collections.Generic; + +namespace CadlRanchProjects.Tests +{ + /// + /// E2E tests for literal value types. + /// + public class LiteralValueTypeTests : CadlRanchMockApiTestBase + { + [Test] + public async Task LiteralModelProperties() => await Test(async (host) => + { + Thing result = await new CadlFirstTestClient(host).CreateLiteralAsync(new Thing("test", "test")); + Assert.AreEqual(result.Name, "literal"); + Assert.AreEqual(result.RequiredUnion, "union"); + // the following are stick to literal value, despite the mock server returns different value + Assert.AreEqual(result.RequiredLiteralString, "accept"); + Assert.AreEqual(result.RequiredLiteralInt, 123); + Assert.AreEqual(result.RequiredLiteralDouble, 1.23); + Assert.AreEqual(result.RequiredLiteralBool, false); + }); + + } + +} diff --git a/test/TestProjects/FirstTest-Cadl/Customization/CadlFirstTestClient.cs b/test/TestProjects/FirstTest-Cadl/Customization/CadlFirstTestClient.cs new file mode 100644 index 00000000000..fbe1c465bea --- /dev/null +++ b/test/TestProjects/FirstTest-Cadl/Customization/CadlFirstTestClient.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#nullable disable + +using System; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace CadlFirstTest +{ + + public partial class CadlFirstTestClient + { + /// + /// Constructor for testing purpose. Bearer token check policy is removed. + /// + /// + public CadlFirstTestClient(Uri endpoint) + { + Argument.AssertNotNull(endpoint, nameof(endpoint)); + var options = new CadlFirstTestClientOptions(); + + ClientDiagnostics = new ClientDiagnostics(options, true); + _pipeline = HttpPipelineBuilder.Build(options, Array.Empty(), new HttpPipelinePolicy[] { }, new ResponseClassifier()); + _endpoint = endpoint; + _apiVersion = options.Version; + } + } +} diff --git a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl index 398c550f85b..50ae4f44db5 100644 --- a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl +++ b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl @@ -3,10 +3,20 @@ import "@cadl-lang/openapi"; import "@azure-tools/cadl-dpg"; import "@azure-tools/cadl-azure-core"; -@serviceTitle("Hello world") -@serviceVersion("0.1.0") +@service( + { + title: "hello world", + version: "0.1.0" + } +) @doc("This is a sample cadl project.") -@server("http://localhost:300","Endpoint Service") +@server( + "{firstTestCadlUrl}", + "Endpoint Service", + { + firstTestCadlUrl: string, + } +) @useAuth(OAuth2Auth<[AuthFlow]> | ApiKeyAuth) @Cadl.Rest.produces("application/json") @Cadl.Rest.consumes("application/json") @@ -119,6 +129,12 @@ namespace Hello.Demo2 { @get op helloDemo2(): Thing; + @route("/literal") + @convenienceAPI + @doc("Create with literal value") + @post + op createLiteral(@body body: Thing): Thing; + @route("/helloLiteral") @convenienceAPI @doc("Send literal parameters") diff --git a/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs b/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs index 833581c51df..78bbfacb746 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs @@ -39,28 +39,30 @@ protected CadlFirstTestClient() } /// Initializes a new instance of CadlFirstTestClient. + /// The Uri to use. /// A credential used to authenticate to an Azure Service. - /// is null. - public CadlFirstTestClient(AzureKeyCredential credential) : this(credential, new Uri("http://localhost:300"), new CadlFirstTestClientOptions()) + /// or is null. + public CadlFirstTestClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new CadlFirstTestClientOptions()) { } /// Initializes a new instance of CadlFirstTestClient. + /// The Uri to use. /// A credential used to authenticate to an Azure Service. - /// is null. - public CadlFirstTestClient(TokenCredential credential) : this(credential, new Uri("http://localhost:300"), new CadlFirstTestClientOptions()) + /// or is null. + public CadlFirstTestClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, new CadlFirstTestClientOptions()) { } /// Initializes a new instance of CadlFirstTestClient. + /// The Uri to use. /// A credential used to authenticate to an Azure Service. - /// Endpoint Service. /// The options for configuring the client. - /// or is null. - public CadlFirstTestClient(AzureKeyCredential credential, Uri endpoint, CadlFirstTestClientOptions options) + /// or is null. + public CadlFirstTestClient(Uri endpoint, AzureKeyCredential credential, CadlFirstTestClientOptions options) { - Argument.AssertNotNull(credential, nameof(credential)); Argument.AssertNotNull(endpoint, nameof(endpoint)); + Argument.AssertNotNull(credential, nameof(credential)); options ??= new CadlFirstTestClientOptions(); ClientDiagnostics = new ClientDiagnostics(options, true); @@ -71,14 +73,14 @@ public CadlFirstTestClient(AzureKeyCredential credential, Uri endpoint, CadlFirs } /// Initializes a new instance of CadlFirstTestClient. + /// The Uri to use. /// A credential used to authenticate to an Azure Service. - /// Endpoint Service. /// The options for configuring the client. - /// or is null. - public CadlFirstTestClient(TokenCredential credential, Uri endpoint, CadlFirstTestClientOptions options) + /// or is null. + public CadlFirstTestClient(Uri endpoint, TokenCredential credential, CadlFirstTestClientOptions options) { - Argument.AssertNotNull(credential, nameof(credential)); Argument.AssertNotNull(endpoint, nameof(endpoint)); + Argument.AssertNotNull(credential, nameof(credential)); options ??= new CadlFirstTestClientOptions(); ClientDiagnostics = new ClientDiagnostics(options, true); @@ -766,16 +768,98 @@ public virtual Response HelloDemo2(RequestContext context = null) } } + /// Create with literal value. + /// The Thing to use. + /// The cancellation token to use. + /// is null. + public virtual async Task> CreateLiteralAsync(Thing body, CancellationToken cancellationToken = default) + { + Argument.AssertNotNull(body, nameof(body)); + + RequestContext context = FromCancellationToken(cancellationToken); + Response response = await CreateLiteralAsync(body.ToRequestContent(), context).ConfigureAwait(false); + return Response.FromValue(Thing.FromResponse(response), response); + } + + /// Create with literal value. + /// The Thing to use. + /// The cancellation token to use. + /// is null. + public virtual Response CreateLiteral(Thing body, CancellationToken cancellationToken = default) + { + Argument.AssertNotNull(body, nameof(body)); + + RequestContext context = FromCancellationToken(cancellationToken); + Response response = CreateLiteral(body.ToRequestContent(), context); + return Response.FromValue(Thing.FromResponse(response), response); + } + + /// Create with literal value. + /// The content to send as the body of the request. Details of the request body schema are in the Remarks section below. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. Details of the response body schema are in the Remarks section below. + /// + public virtual async Task CreateLiteralAsync(RequestContent content, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.CreateLiteral"); + scope.Start(); + try + { + using HttpMessage message = CreateCreateLiteralRequest(content, context); + return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// Create with literal value. + /// The content to send as the body of the request. Details of the request body schema are in the Remarks section below. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. Details of the response body schema are in the Remarks section below. + /// + public virtual Response CreateLiteral(RequestContent content, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.CreateLiteral"); + scope.Start(); + try + { + using HttpMessage message = CreateCreateLiteralRequest(content, context); + return _pipeline.ProcessMessage(message, context); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + /// Send literal parameters. + /// The Literal to use. + /// The Literal to use. + /// The Literal to use. /// The cancellation token to use. - public virtual async Task> HelloLiteralValueAsync(CancellationToken cancellationToken = default) + /// is null. + public virtual async Task> HelloLiteralValueAsync(int p2 = 123, string p1 = "test", bool p3 = true, CancellationToken cancellationToken = default) { + Argument.AssertNotNull(p1, nameof(p1)); + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteralValue"); scope.Start(); try { RequestContext context = FromCancellationToken(cancellationToken); - Response response = await HelloLiteralAsync(context).ConfigureAwait(false); + Response response = await HelloLiteralAsync(p2, p1, p3, context).ConfigureAwait(false); return Response.FromValue(Thing.FromResponse(response), response); } catch (Exception e) @@ -786,15 +870,21 @@ public virtual async Task> HelloLiteralValueAsync(CancellationTo } /// Send literal parameters. + /// The Literal to use. + /// The Literal to use. + /// The Literal to use. /// The cancellation token to use. - public virtual Response HelloLiteralValue(CancellationToken cancellationToken = default) + /// is null. + public virtual Response HelloLiteralValue(int p2 = 123, string p1 = "test", bool p3 = true, CancellationToken cancellationToken = default) { + Argument.AssertNotNull(p1, nameof(p1)); + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteralValue"); scope.Start(); try { RequestContext context = FromCancellationToken(cancellationToken); - Response response = HelloLiteral(context); + Response response = HelloLiteral(p2, p1, p3, context); return Response.FromValue(Thing.FromResponse(response), response); } catch (Exception e) @@ -805,17 +895,23 @@ public virtual Response HelloLiteralValue(CancellationToken cancellationT } /// Send literal parameters. + /// The Literal to use. + /// The Literal to use. + /// The Literal to use. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. /// Service returned a non-success status code. /// The response returned from the service. Details of the response body schema are in the Remarks section below. - /// - public virtual async Task HelloLiteralAsync(RequestContext context = null) + /// + public virtual async Task HelloLiteralAsync(int p2 = 123, string p1 = "test", bool p3 = true, RequestContext context = null) { + Argument.AssertNotNull(p1, nameof(p1)); + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteral"); scope.Start(); try { - using HttpMessage message = CreateHelloLiteralRequest(context); + using HttpMessage message = CreateHelloLiteralRequest(p2, p1, p3, context); return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); } catch (Exception e) @@ -826,17 +922,23 @@ public virtual async Task HelloLiteralAsync(RequestContext context = n } /// Send literal parameters. + /// The Literal to use. + /// The Literal to use. + /// The Literal to use. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. /// Service returned a non-success status code. /// The response returned from the service. Details of the response body schema are in the Remarks section below. - /// - public virtual Response HelloLiteral(RequestContext context = null) + /// + public virtual Response HelloLiteral(int p2 = 123, string p1 = "test", bool p3 = true, RequestContext context = null) { + Argument.AssertNotNull(p1, nameof(p1)); + using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteral"); scope.Start(); try { - using HttpMessage message = CreateHelloLiteralRequest(context); + using HttpMessage message = CreateHelloLiteralRequest(p2, p1, p3, context); return _pipeline.ProcessMessage(message, context); } catch (Exception e) @@ -1053,7 +1155,23 @@ internal HttpMessage CreateHelloDemo2Request(RequestContext context) return message; } - internal HttpMessage CreateHelloLiteralRequest(RequestContext context) + internal HttpMessage CreateCreateLiteralRequest(RequestContent content, RequestContext context) + { + var message = _pipeline.CreateMessage(context, ResponseClassifier200); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.Reset(_endpoint); + uri.AppendPath("/literal", false); + uri.AppendQuery("api-version", _apiVersion, true); + request.Uri = uri; + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("Content-Type", "application/json"); + request.Content = content; + return message; + } + + internal HttpMessage CreateHelloLiteralRequest(int p2, string p1, bool p3, RequestContext context) { var message = _pipeline.CreateMessage(context, ResponseClassifier200); var request = message.Request; @@ -1061,11 +1179,11 @@ internal HttpMessage CreateHelloLiteralRequest(RequestContext context) var uri = new RawRequestUriBuilder(); uri.Reset(_endpoint); uri.AppendPath("/helloLiteral/", false); - uri.AppendPath(123, true); - uri.AppendQuery("p3", true, true); + uri.AppendPath(p2, true); + uri.AppendQuery("p3", p3, true); uri.AppendQuery("api-version", _apiVersion, true); request.Uri = uri; - request.Headers.Add("p1", "test"); + request.Headers.Add("p1", p1); request.Headers.Add("Accept", "application/json"); return message; } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml index a8ab069025e..dcbcda70f6d 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml +++ b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml @@ -6,7 +6,8 @@ This sample shows how to call TopActionAsync with required parameters and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.TopActionAsync(""); @@ -42,7 +43,8 @@ Schema for Thing: This sample shows how to call TopAction with required parameters and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.TopAction(""); @@ -78,7 +80,8 @@ Schema for Thing: This sample shows how to call TopAction2Async and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.TopAction2Async(); @@ -114,7 +117,8 @@ Schema for Thing: This sample shows how to call TopAction2 and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.TopAction2(); @@ -150,7 +154,8 @@ Schema for Thing: This sample shows how to call PatchActionAsync with required request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { name = "", @@ -208,7 +213,8 @@ Schema for Thing: This sample shows how to call PatchAction with required request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { name = "", @@ -266,7 +272,8 @@ Schema for Thing: This sample shows how to call AnonymousBodyAsync with required request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { name = "", @@ -324,7 +331,8 @@ Schema for Thing: This sample shows how to call AnonymousBody with required request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { name = "", @@ -382,7 +390,8 @@ Schema for Thing: This sample shows how to call FriendlyModelAsync with required request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { name = "", @@ -420,7 +429,8 @@ Schema for Friend: This sample shows how to call FriendlyModel with required request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { name = "", @@ -458,7 +468,8 @@ Schema for Friend: This sample shows how to call AddTimeHeaderAsync. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.AddTimeHeaderAsync(); Console.WriteLine(response.Status); @@ -466,7 +477,8 @@ Console.WriteLine(response.Status); This sample shows how to call AddTimeHeaderAsync with all parameters. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.AddTimeHeaderAsync(DateTimeOffset.UtcNow); Console.WriteLine(response.Status); @@ -478,7 +490,8 @@ Console.WriteLine(response.Status); This sample shows how to call AddTimeHeader. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.AddTimeHeader(); Console.WriteLine(response.Status); @@ -486,7 +499,8 @@ Console.WriteLine(response.Status); This sample shows how to call AddTimeHeader with all parameters. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.AddTimeHeader(DateTimeOffset.UtcNow); Console.WriteLine(response.Status); @@ -498,7 +512,8 @@ Console.WriteLine(response.Status); This sample shows how to call SayHiAsync with required parameters and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.SayHiAsync("", ""); @@ -513,7 +528,8 @@ Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); This sample shows how to call SayHiAsync with all parameters, and how to parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.SayHiAsync("", "", ""); @@ -549,7 +565,8 @@ Schema for Thing: This sample shows how to call SayHi with required parameters and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.SayHi("", ""); @@ -564,7 +581,8 @@ Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); This sample shows how to call SayHi with all parameters, and how to parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.SayHi("", "", ""); @@ -600,7 +618,8 @@ Schema for Thing: This sample shows how to call HelloAgainAsync with required parameters and request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { requiredString = "", @@ -674,7 +693,8 @@ Schema for Thing: This sample shows how to call HelloAgain with required parameters and request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { requiredString = "", @@ -748,7 +768,8 @@ Schema for Thing: This sample shows how to call NoContentTypeAsync with required parameters and request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { requiredString = "", @@ -822,7 +843,8 @@ Schema for Thing: This sample shows how to call NoContentType with required parameters and request content and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); var data = new { requiredString = "", @@ -896,7 +918,8 @@ Schema for Thing: This sample shows how to call HelloDemo2Async and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.HelloDemo2Async(); @@ -932,7 +955,8 @@ Schema for Thing: This sample shows how to call HelloDemo2 and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.HelloDemo2(); @@ -963,15 +987,150 @@ Schema for Thing: - + + +This sample shows how to call CreateLiteralAsync with required request content and parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, +}; + +Response response = await client.CreateLiteralAsync(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +]]> + + +Below is the JSON schema for the request and response payloads. + +Request Body: + +Schema for Thing: +{ + name: string, # Required. + requiredUnion: Union, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. + requiredLiteralBool: Literal, # Required. +} + + +Response Body: + +Schema for Thing: +{ + name: string, # Required. + requiredUnion: Union, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. + requiredLiteralBool: Literal, # Required. +} + + + + + + +This sample shows how to call CreateLiteral with required request content and parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, +}; + +Response response = client.CreateLiteral(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +]]> + + +Below is the JSON schema for the request and response payloads. + +Request Body: + +Schema for Thing: +{ + name: string, # Required. + requiredUnion: Union, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. + requiredLiteralBool: Literal, # Required. +} + + +Response Body: + +Schema for Thing: +{ + name: string, # Required. + requiredUnion: Union, # Required. + requiredLiteralString: Literal, # Required. + requiredLiteralInt: Literal, # Required. + requiredLiteralDouble: Literal, # Required. + requiredLiteralBool: Literal, # Required. +} + + + + + This sample shows how to call HelloLiteralAsync and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.HelloLiteralAsync(); +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +]]> +This sample shows how to call HelloLiteralAsync with all parameters, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +Response response = await client.HelloLiteralAsync(123, , true); + JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); @@ -999,15 +1158,32 @@ Schema for Thing: - + This sample shows how to call HelloLiteral and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.HelloLiteral(); +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +]]> +This sample shows how to call HelloLiteral with all parameters, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +Response response = client.HelloLiteral(123, , true); + JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); @@ -1040,7 +1216,8 @@ Schema for Thing: This sample shows how to call GetUnknownValueAsync and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = await client.GetUnknownValueAsync(); @@ -1054,7 +1231,8 @@ Console.WriteLine(result.ToString()); This sample shows how to call GetUnknownValue and parse the result. "); -var client = new CadlFirstTestClient(credential); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); Response response = client.GetUnknownValue(); diff --git a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json index a9e7128054c..7214d3016dd 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json +++ b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json @@ -386,13 +386,12 @@ "Parameters": [ { "$id": "55", - "Name": "host", - "NameInRequest": "host", - "Description": "Endpoint Service", + "Name": "firstTestCadlUrl", + "NameInRequest": "firstTestCadlUrl", "Type": { "$id": "56", - "Name": "String", - "Kind": "String", + "Name": "Uri", + "Kind": "Uri", "IsNullable": false }, "Location": "Uri", @@ -403,24 +402,14 @@ "IsEndpoint": true, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Client", - "DefaultValue": { - "$id": "57", - "Type": { - "$id": "58", - "Name": "String", - "Kind": "String", - "IsNullable": false - }, - "Value": "http://localhost:300" - } + "Kind": "Client" }, { - "$id": "59", + "$id": "57", "Name": "action", "NameInRequest": "action", "Type": { - "$id": "60", + "$id": "58", "Name": "string", "Kind": "String", "IsNullable": false @@ -436,11 +425,11 @@ "Kind": "Method" }, { - "$id": "61", + "$id": "59", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "62", + "$id": "60", "Name": "String", "Kind": "String", "IsNullable": false @@ -455,20 +444,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "63", + "$id": "61", "Type": { - "$ref": "62" + "$ref": "60" }, "Value": "application/json" } }, { - "$id": "64", + "$id": "62", "Name": "apiVersion", "NameInRequest": "api-version", "Description": "", "Type": { - "$id": "65", + "$id": "63", "Name": "String", "Kind": "String", "IsNullable": false @@ -483,9 +472,9 @@ "Explode": false, "Kind": "Client", "DefaultValue": { - "$id": "66", + "$id": "64", "Type": { - "$id": "67", + "$id": "65", "Name": "String", "Kind": "String", "IsNullable": false @@ -496,7 +485,7 @@ ], "Responses": [ { - "$id": "68", + "$id": "66", "StatusCodes": [ 200 ], @@ -510,13 +499,13 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/top/{action}", "BufferResponse": true, "GenerateConvenienceMethod": true }, { - "$id": "69", + "$id": "67", "Name": "topAction2", "ResourceName": "CadlFirstTest", "Description": "top level method2", @@ -525,11 +514,11 @@ "$ref": "55" }, { - "$id": "70", + "$id": "68", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "71", + "$id": "69", "Name": "String", "Kind": "String", "IsNullable": false @@ -544,20 +533,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "72", + "$id": "70", "Type": { - "$ref": "71" + "$ref": "69" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "73", + "$id": "71", "StatusCodes": [ 200 ], @@ -571,13 +560,13 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/top2", "BufferResponse": true, "GenerateConvenienceMethod": false }, { - "$id": "74", + "$id": "72", "Name": "patchAction", "ResourceName": "CadlFirstTest", "Description": "top level patch", @@ -586,7 +575,7 @@ "$ref": "55" }, { - "$id": "75", + "$id": "73", "Name": "body", "NameInRequest": "body", "Type": { @@ -603,11 +592,11 @@ "Kind": "Method" }, { - "$id": "76", + "$id": "74", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "77", + "$id": "75", "Name": "String", "Kind": "String", "IsNullable": false @@ -622,19 +611,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "78", + "$id": "76", "Type": { - "$ref": "77" + "$ref": "75" }, "Value": "application/json" } }, { - "$id": "79", + "$id": "77", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "80", + "$id": "78", "Name": "String", "Kind": "String", "IsNullable": false @@ -649,20 +638,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "81", + "$id": "79", "Type": { - "$ref": "80" + "$ref": "78" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "82", + "$id": "80", "StatusCodes": [ 200 ], @@ -676,7 +665,7 @@ ], "HttpMethod": "PATCH", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/patch", "RequestMediaTypes": [ "application/json" @@ -685,7 +674,7 @@ "GenerateConvenienceMethod": false }, { - "$id": "83", + "$id": "81", "Name": "anonymousBody", "ResourceName": "CadlFirstTest", "Description": "body parameter without body decorator", @@ -694,7 +683,7 @@ "$ref": "55" }, { - "$id": "84", + "$id": "82", "Name": "Thing", "NameInRequest": "Thing", "Type": { @@ -711,11 +700,11 @@ "Kind": "Method" }, { - "$id": "85", + "$id": "83", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "86", + "$id": "84", "Name": "String", "Kind": "String", "IsNullable": false @@ -730,19 +719,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "87", + "$id": "85", "Type": { - "$ref": "86" + "$ref": "84" }, "Value": "application/json" } }, { - "$id": "88", + "$id": "86", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "89", + "$id": "87", "Name": "String", "Kind": "String", "IsNullable": false @@ -757,20 +746,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "90", + "$id": "88", "Type": { - "$ref": "89" + "$ref": "87" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "91", + "$id": "89", "StatusCodes": [ 200 ], @@ -784,7 +773,7 @@ ], "HttpMethod": "POST", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/anonymousBody", "RequestMediaTypes": [ "application/json" @@ -793,7 +782,7 @@ "GenerateConvenienceMethod": true }, { - "$id": "92", + "$id": "90", "Name": "friendlyModel", "ResourceName": "CadlFirstTest", "Description": "Model can have its friendly name", @@ -802,7 +791,7 @@ "$ref": "55" }, { - "$id": "93", + "$id": "91", "Name": "NotFriend", "NameInRequest": "NotFriend", "Type": { @@ -819,11 +808,11 @@ "Kind": "Method" }, { - "$id": "94", + "$id": "92", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "95", + "$id": "93", "Name": "String", "Kind": "String", "IsNullable": false @@ -838,19 +827,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "96", + "$id": "94", "Type": { - "$ref": "95" + "$ref": "93" }, "Value": "application/json" } }, { - "$id": "97", + "$id": "95", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "98", + "$id": "96", "Name": "String", "Kind": "String", "IsNullable": false @@ -865,20 +854,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "99", + "$id": "97", "Type": { - "$ref": "98" + "$ref": "96" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "100", + "$id": "98", "StatusCodes": [ 200 ], @@ -892,7 +881,7 @@ ], "HttpMethod": "POST", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/friendlyName", "RequestMediaTypes": [ "application/json" @@ -901,7 +890,7 @@ "GenerateConvenienceMethod": true }, { - "$id": "101", + "$id": "99", "Name": "addTimeHeader", "ResourceName": "CadlFirstTest", "Parameters": [ @@ -909,11 +898,11 @@ "$ref": "55" }, { - "$id": "102", + "$id": "100", "Name": "repeatabilityFirstSent", "NameInRequest": "Repeatability-First-Sent", "Type": { - "$id": "103", + "$id": "101", "Name": "zonedDateTime", "Kind": "DateTime", "IsNullable": false @@ -929,11 +918,11 @@ "Kind": "Method" }, { - "$id": "104", + "$id": "102", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "105", + "$id": "103", "Name": "String", "Kind": "String", "IsNullable": false @@ -948,20 +937,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "106", + "$id": "104", "Type": { - "$ref": "105" + "$ref": "103" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "107", + "$id": "105", "StatusCodes": [ 204 ], @@ -972,13 +961,13 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/", "BufferResponse": true, "GenerateConvenienceMethod": false }, { - "$id": "108", + "$id": "106", "Name": "sayHi", "ResourceName": "Demo", "Description": "Return hi", @@ -987,11 +976,11 @@ "$ref": "55" }, { - "$id": "109", + "$id": "107", "Name": "headParameter", "NameInRequest": "head-parameter", "Type": { - "$id": "110", + "$id": "108", "Name": "string", "Kind": "String", "IsNullable": false @@ -1007,11 +996,11 @@ "Kind": "Method" }, { - "$id": "111", + "$id": "109", "Name": "queryParameter", "NameInRequest": "queryParameter", "Type": { - "$id": "112", + "$id": "110", "Name": "string", "Kind": "String", "IsNullable": false @@ -1027,11 +1016,11 @@ "Kind": "Method" }, { - "$id": "113", + "$id": "111", "Name": "optionalQuery", "NameInRequest": "optionalQuery", "Type": { - "$id": "114", + "$id": "112", "Name": "string", "Kind": "String", "IsNullable": false @@ -1047,11 +1036,11 @@ "Kind": "Method" }, { - "$id": "115", + "$id": "113", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "116", + "$id": "114", "Name": "String", "Kind": "String", "IsNullable": false @@ -1066,20 +1055,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "117", + "$id": "115", "Type": { - "$ref": "116" + "$ref": "114" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "118", + "$id": "116", "StatusCodes": [ 200 ], @@ -1093,13 +1082,13 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/hello", "BufferResponse": true, "GenerateConvenienceMethod": false }, { - "$id": "119", + "$id": "117", "Name": "helloAgain", "ResourceName": "Demo2", "Description": "Return hi again", @@ -1108,11 +1097,11 @@ "$ref": "55" }, { - "$id": "120", + "$id": "118", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "121", + "$id": "119", "Name": "string", "Kind": "String", "IsNullable": false @@ -1128,14 +1117,14 @@ "Kind": "Method" }, { - "$id": "122", + "$id": "120", "Name": "contentType", "NameInRequest": "content-type", "Type": { - "$id": "123", + "$id": "121", "Name": "Literal", "LiteralValueType": { - "$id": "124", + "$id": "122", "Name": "String", "Kind": "String", "IsNullable": false @@ -1145,9 +1134,9 @@ }, "Location": "Header", "DefaultValue": { - "$id": "125", + "$id": "123", "Type": { - "$ref": "123" + "$ref": "121" }, "Value": "text/plain" }, @@ -1161,11 +1150,11 @@ "Kind": "Constant" }, { - "$id": "126", + "$id": "124", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "127", + "$id": "125", "Name": "string", "Kind": "String", "IsNullable": false @@ -1181,7 +1170,7 @@ "Kind": "Method" }, { - "$id": "128", + "$id": "126", "Name": "action", "NameInRequest": "action", "Type": { @@ -1198,11 +1187,11 @@ "Kind": "Method" }, { - "$id": "129", + "$id": "127", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "130", + "$id": "128", "Name": "String", "Kind": "String", "IsNullable": false @@ -1217,20 +1206,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "131", + "$id": "129", "Type": { - "$ref": "130" + "$ref": "128" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "132", + "$id": "130", "StatusCodes": [ 200 ], @@ -1244,7 +1233,7 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/againHi/{p2}", "RequestMediaTypes": [ "text/plain" @@ -1253,7 +1242,7 @@ "GenerateConvenienceMethod": true }, { - "$id": "133", + "$id": "131", "Name": "noContentType", "ResourceName": "Demo2", "Description": "Return hi again", @@ -1262,11 +1251,11 @@ "$ref": "55" }, { - "$id": "134", + "$id": "132", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "135", + "$id": "133", "Name": "string", "Kind": "String", "IsNullable": false @@ -1282,11 +1271,11 @@ "Kind": "Method" }, { - "$id": "136", + "$id": "134", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "137", + "$id": "135", "Name": "string", "Kind": "String", "IsNullable": false @@ -1302,7 +1291,7 @@ "Kind": "Method" }, { - "$id": "138", + "$id": "136", "Name": "action", "NameInRequest": "action", "Type": { @@ -1319,11 +1308,11 @@ "Kind": "Method" }, { - "$id": "139", + "$id": "137", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "140", + "$id": "138", "Name": "String", "Kind": "String", "IsNullable": false @@ -1338,19 +1327,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "141", + "$id": "139", "Type": { - "$ref": "140" + "$ref": "138" }, "Value": "application/json" } }, { - "$id": "142", + "$id": "140", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "143", + "$id": "141", "Name": "String", "Kind": "String", "IsNullable": false @@ -1365,20 +1354,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "144", + "$id": "142", "Type": { - "$ref": "143" + "$ref": "141" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "145", + "$id": "143", "StatusCodes": [ 200 ], @@ -1392,7 +1381,7 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/noContentType/{p2}", "RequestMediaTypes": [ "application/json" @@ -1401,7 +1390,7 @@ "GenerateConvenienceMethod": false }, { - "$id": "146", + "$id": "144", "Name": "helloDemo2", "ResourceName": "Demo2", "Description": "Return hi in demo2", @@ -1410,11 +1399,11 @@ "$ref": "55" }, { - "$id": "147", + "$id": "145", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "148", + "$id": "146", "Name": "String", "Kind": "String", "IsNullable": false @@ -1429,20 +1418,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "149", + "$id": "147", "Type": { - "$ref": "148" + "$ref": "146" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "150", + "$id": "148", "StatusCodes": [ 200 ], @@ -1456,13 +1445,121 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/demoHi", "BufferResponse": true, "GenerateConvenienceMethod": true }, { - "$id": "151", + "$id": "149", + "Name": "createLiteral", + "ResourceName": "Demo2", + "Description": "Create with literal value", + "Parameters": [ + { + "$ref": "55" + }, + { + "$id": "150", + "Name": "body", + "NameInRequest": "body", + "Type": { + "$ref": "18" + }, + "Location": "Body", + "IsRequired": true, + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": false, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Method" + }, + { + "$id": "151", + "Name": "contentType", + "NameInRequest": "Content-Type", + "Type": { + "$id": "152", + "Name": "String", + "Kind": "String", + "IsNullable": false + }, + "Location": "Header", + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": true, + "IsRequired": true, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Constant", + "DefaultValue": { + "$id": "153", + "Type": { + "$ref": "152" + }, + "Value": "application/json" + } + }, + { + "$id": "154", + "Name": "accept", + "NameInRequest": "Accept", + "Type": { + "$id": "155", + "Name": "String", + "Kind": "String", + "IsNullable": false + }, + "Location": "Header", + "IsApiVersion": false, + "IsResourceParameter": false, + "IsContentType": false, + "IsRequired": true, + "IsEndpoint": false, + "SkipUrlEncoding": false, + "Explode": false, + "Kind": "Constant", + "DefaultValue": { + "$id": "156", + "Type": { + "$ref": "155" + }, + "Value": "application/json" + } + }, + { + "$ref": "62" + } + ], + "Responses": [ + { + "$id": "157", + "StatusCodes": [ + 200 + ], + "BodyType": { + "$ref": "18" + }, + "BodyMediaType": "Json", + "Headers": [], + "IsErrorResponse": false + } + ], + "HttpMethod": "POST", + "RequestBodyMediaType": "Json", + "Uri": "{firstTestCadlUrl}", + "Path": "/literal", + "RequestMediaTypes": [ + "application/json" + ], + "BufferResponse": true, + "GenerateConvenienceMethod": true + }, + { + "$id": "158", "Name": "helloLiteral", "ResourceName": "Demo2", "Description": "Send literal parameters", @@ -1471,14 +1568,14 @@ "$ref": "55" }, { - "$id": "152", + "$id": "159", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "153", + "$id": "160", "Name": "Literal", "LiteralValueType": { - "$id": "154", + "$id": "161", "Name": "String", "Kind": "String", "IsNullable": false @@ -1488,9 +1585,9 @@ }, "Location": "Header", "DefaultValue": { - "$id": "155", + "$id": "162", "Type": { - "$ref": "153" + "$ref": "160" }, "Value": "test" }, @@ -1501,17 +1598,17 @@ "IsEndpoint": false, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Constant" + "Kind": "Method" }, { - "$id": "156", + "$id": "163", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "157", + "$id": "164", "Name": "Literal", "LiteralValueType": { - "$id": "158", + "$id": "165", "Name": "Number", "Kind": "Int32", "IsNullable": false @@ -1521,9 +1618,9 @@ }, "Location": "Path", "DefaultValue": { - "$id": "159", + "$id": "166", "Type": { - "$ref": "157" + "$ref": "164" }, "Value": 123 }, @@ -1534,17 +1631,17 @@ "IsEndpoint": false, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Constant" + "Kind": "Method" }, { - "$id": "160", + "$id": "167", "Name": "p3", "NameInRequest": "p3", "Type": { - "$id": "161", + "$id": "168", "Name": "Literal", "LiteralValueType": { - "$id": "162", + "$id": "169", "Name": "Boolean", "Kind": "Boolean", "IsNullable": false @@ -1554,9 +1651,9 @@ }, "Location": "Query", "DefaultValue": { - "$id": "163", + "$id": "170", "Type": { - "$ref": "161" + "$ref": "168" }, "Value": true }, @@ -1567,14 +1664,14 @@ "IsEndpoint": false, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Constant" + "Kind": "Method" }, { - "$id": "164", + "$id": "171", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "165", + "$id": "172", "Name": "String", "Kind": "String", "IsNullable": false @@ -1589,20 +1686,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "166", + "$id": "173", "Type": { - "$ref": "165" + "$ref": "172" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "167", + "$id": "174", "StatusCodes": [ 200 ], @@ -1616,13 +1713,13 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/helloLiteral/{p2}", "BufferResponse": true, "GenerateConvenienceMethod": true }, { - "$id": "168", + "$id": "175", "Name": "getUnknownValue", "ResourceName": "EnumTest", "Description": "get extensible enum", @@ -1631,11 +1728,11 @@ "$ref": "55" }, { - "$id": "169", + "$id": "176", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "170", + "$id": "177", "Name": "String", "Kind": "String", "IsNullable": false @@ -1650,20 +1747,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "171", + "$id": "178", "Type": { - "$ref": "170" + "$ref": "177" }, "Value": "application/json" } }, { - "$ref": "64" + "$ref": "62" } ], "Responses": [ { - "$id": "172", + "$id": "179", "StatusCodes": [ 200 ], @@ -1677,26 +1774,26 @@ ], "HttpMethod": "GET", "RequestBodyMediaType": "Json", - "Uri": "{host}", + "Uri": "{firstTestCadlUrl}", "Path": "/unknown-value", "BufferResponse": true, "GenerateConvenienceMethod": false } ], "Protocol": { - "$id": "173" + "$id": "180" }, "Creatable": true } ], "Auth": { - "$id": "174", + "$id": "181", "ApiKey": { - "$id": "175", + "$id": "182", "Name": "x-ms-api-key" }, "OAuth2": { - "$id": "176", + "$id": "183", "Scopes": [ "https://api.example.com/.default" ] From 28b97ade944faa3de9a8e1551265bee25e17667b Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 31 Jan 2023 17:10:52 +0800 Subject: [PATCH 04/10] do not generate deserialization codes for literal model properties - add a property `ShouldSkipDeserialization` in `PropertySerliaization` - update writer to skip generating deserialization codes for literal type properties --- .../Writers/JsonCodeWriterExtensions.cs | 13 +- .../Output/Builders/SerializationBuilder.cs | 1 + .../JsonAdditionalPropertiesSerialization.cs | 2 +- .../Json/JsonPropertySerialization.cs | 4 +- .../Serialization/PropertySerialization.cs | 9 +- .../Output/Models/Types/ModelTypeProvider.cs | 1 + .../Output/Models/Types/ObjectTypeProperty.cs | 2 + .../Emitter.Csharp/src/lib/model.ts | 2 +- .../FirstTest-Cadl/FirstTest-Cadl.cadl | 12 + .../Generated/Docs/CadlFirstTestClient.xml | 256 ++++++++++ .../Generated/Models/Thing.Serialization.cs | 32 +- .../FirstTest-Cadl/Generated/Models/Thing.cs | 12 + .../FirstTest-Cadl/Generated/cadl.json | 474 +++++++++++------- 13 files changed, 592 insertions(+), 228 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs b/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs index 4bc69cb46d3..f0efbe935f3 100644 --- a/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs +++ b/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs @@ -288,6 +288,11 @@ private static void DeserializeIntoObjectProperties(this CodeWriter writer, IEnu { foreach (JsonPropertySerialization property in propertySerializations) { + if (property.ShouldSkipDeserialization) + { + continue; + } + writer.Append($"if({itemVariable}.NameEquals({property.SerializedName:L}))"); using (writer.Scope()) { @@ -379,9 +384,9 @@ private static FormattableString GetOptionalFormattable(JsonPropertySerializatio } /// Collects a list of properties being read from all level of object hierarchy - private static void CollectProperties(Dictionary propertyVariables, IEnumerable jsonProperties) + private static void CollectPropertiesForDeserialization(Dictionary propertyVariables, IEnumerable jsonProperties) { - foreach (JsonPropertySerialization jsonProperty in jsonProperties) + foreach (JsonPropertySerialization jsonProperty in jsonProperties.Where(p => !p.ShouldSkipDeserialization)) { if (jsonProperty.ValueType != null) { @@ -399,7 +404,7 @@ private static void CollectProperties(Dictionary(); - CollectProperties(propertyVariables, serialization.Properties); + CollectPropertiesForDeserialization(propertyVariables, serialization.Properties); var additionalProperties = serialization.AdditionalProperties; if (additionalProperties != null) diff --git a/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs b/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs index 4eb523fef7b..0bf93d96042 100644 --- a/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs +++ b/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs @@ -267,6 +267,7 @@ private IEnumerable GetPropertySerializationsFromBag( BuildSerialization(property.Schema, objectProperty.ValueType), property.IsRequired, property.IsReadOnly, + objectProperty.ShouldSkipDeserialization, objectProperty.OptionalViaNullability); } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs b/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs index 67d4a9bac9a..2882b7bb4b5 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs @@ -12,7 +12,7 @@ internal class JsonAdditionalPropertiesSerialization : JsonPropertySerialization public CSharpType Type { get; } public JsonAdditionalPropertiesSerialization(ObjectTypeProperty property, JsonSerialization valueSerialization, CSharpType type) - : base(property.Declaration.Name.ToVariableName(), property.Declaration.Name, property.Declaration.Name, property.Declaration.Type, property.ValueType, valueSerialization, true, property.IsReadOnly, property.OptionalViaNullability) + : base(property.Declaration.Name.ToVariableName(), property.Declaration.Name, property.Declaration.Name, property.Declaration.Type, property.ValueType, valueSerialization, true, property.IsReadOnly, property.ShouldSkipDeserialization, property.OptionalViaNullability) { Type = type; } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonPropertySerialization.cs b/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonPropertySerialization.cs index d24ce41db18..d988f2bf943 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonPropertySerialization.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonPropertySerialization.cs @@ -8,8 +8,8 @@ namespace AutoRest.CSharp.Output.Models.Serialization.Json { internal class JsonPropertySerialization : PropertySerialization { - public JsonPropertySerialization(string parameterName, string propertyName, string serializedName, CSharpType propertyType, CSharpType? valueType, JsonSerialization valueSerialization, bool isRequired, bool shouldSkipSerialization, bool optionalViaNullability) - : base(propertyName, serializedName, propertyType, valueType, isRequired, shouldSkipSerialization) + public JsonPropertySerialization(string parameterName, string propertyName, string serializedName, CSharpType propertyType, CSharpType? valueType, JsonSerialization valueSerialization, bool isRequired, bool shouldSkipSerialization, bool shouldSkipDeserialization, bool optionalViaNullability) + : base(propertyName, serializedName, propertyType, valueType, isRequired, shouldSkipSerialization, shouldSkipDeserialization) { ParameterName = parameterName; OptionalViaNullability = optionalViaNullability; diff --git a/src/AutoRest.CSharp/Common/Output/Models/Serialization/PropertySerialization.cs b/src/AutoRest.CSharp/Common/Output/Models/Serialization/PropertySerialization.cs index 2e821833eaa..6bb5a4e6ac3 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Serialization/PropertySerialization.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Serialization/PropertySerialization.cs @@ -13,8 +13,14 @@ internal abstract class PropertySerialization public CSharpType? ValueType { get; } public bool IsRequired { get; } public bool ShouldSkipSerialization { get; } + public bool ShouldSkipDeserialization { get; } - protected PropertySerialization(string propertyName, string serializedName, CSharpType propertyType, CSharpType? valueType, bool isRequired, bool shouldSkipSerialization) + protected PropertySerialization(string propertyName, string serializedName, CSharpType propertyType, CSharpType? valueType, bool isRequired, bool shouldSkipSerialization) : + this(propertyName, serializedName, propertyType, valueType, isRequired, shouldSkipSerialization, false) + { + } + + protected PropertySerialization(string propertyName, string serializedName, CSharpType propertyType, CSharpType? valueType, bool isRequired, bool shouldSkipSerialization, bool shouldSkipDeserialization) { PropertyName = propertyName; SerializedName = serializedName; @@ -22,6 +28,7 @@ protected PropertySerialization(string propertyName, string serializedName, CSha ValueType = valueType; IsRequired = isRequired; ShouldSkipSerialization = shouldSkipSerialization; + ShouldSkipDeserialization = shouldSkipDeserialization; } } } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs index 2363b97d92d..dccfa607771 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs @@ -147,6 +147,7 @@ private IEnumerable CreatePropertySerializations() valueSerialization, property.IsRequired, shouldSkipSerialization, + property.ShouldSkipDeserialization, optionalViaNullability)); } } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs index 3949aef6c4d..49ea0046d23 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs @@ -168,5 +168,7 @@ private static string CreateExtraPropertyDiscriminatorSummary(CSharpType valueTy } return updatedDescription; } + + public bool ShouldSkipDeserialization => InputModelProperty?.Type is InputLiteralType; } } diff --git a/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts b/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts index 387a3a3e98b..24058fa57f0 100644 --- a/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts +++ b/src/CADL.Extension/Emitter.Csharp/src/lib/model.ts @@ -73,7 +73,7 @@ export function mapCadlTypeToCSharpInputTypeKind( let nubmerValue = cadlType.value; if (nubmerValue % 1 === 0) { return InputTypeKind.Int32; - }; + } return InputTypeKind.Float64; case "Boolean": return InputTypeKind.Boolean; diff --git a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl index 50ae4f44db5..4c680d3858a 100644 --- a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl +++ b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl @@ -68,6 +68,18 @@ model Thing { @doc("required literal bool") requiredLiteralBool: false; + + @doc("optional literal string") + optionalLiteralString: "reject"; + + @doc("optional literal int") + optionalLiteralInt: 456; + + @doc("optional literal double") + optionalLiteralDouble: 4.56; + + @doc("optional literal bool") + optionalLiteralBool: true; } @friendlyName("Friend") diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml index dcbcda70f6d..4b74b76da0c 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml +++ b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml @@ -18,6 +18,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -33,6 +37,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -55,6 +63,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -70,6 +82,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -92,6 +108,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -107,6 +127,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -129,6 +153,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -144,6 +172,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -164,6 +196,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }; Response response = await client.PatchActionAsync(RequestContent.Create(data)); @@ -175,6 +211,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -190,6 +230,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -203,6 +247,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -223,6 +271,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }; Response response = client.PatchAction(RequestContent.Create(data)); @@ -234,6 +286,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -249,6 +305,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -262,6 +322,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -282,6 +346,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }; Response response = await client.AnonymousBodyAsync(RequestContent.Create(data)); @@ -293,6 +361,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -308,6 +380,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -321,6 +397,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -341,6 +421,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }; Response response = client.AnonymousBody(RequestContent.Create(data)); @@ -352,6 +436,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -367,6 +455,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -380,6 +472,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -524,6 +620,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> This sample shows how to call SayHiAsync with all parameters, and how to parse the result. @@ -555,6 +659,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -577,6 +685,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> This sample shows how to call SayHi with all parameters, and how to parse the result. @@ -608,6 +724,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -637,6 +757,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }, }; @@ -649,6 +773,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -669,6 +797,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. }, # Required. } @@ -683,6 +815,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -712,6 +848,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }, }; @@ -724,6 +864,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -744,6 +888,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. }, # Required. } @@ -758,6 +906,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -787,6 +939,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }, }; @@ -799,6 +955,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -819,6 +979,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. }, # Required. } @@ -833,6 +997,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -862,6 +1030,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }, }; @@ -874,6 +1046,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -894,6 +1070,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. }, # Required. } @@ -908,6 +1088,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -930,6 +1114,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -945,6 +1133,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -967,6 +1159,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -982,6 +1178,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -1002,6 +1202,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }; Response response = await client.CreateLiteralAsync(RequestContent.Create(data)); @@ -1013,6 +1217,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -1028,6 +1236,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -1041,6 +1253,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -1061,6 +1277,10 @@ var data = new { requiredLiteralInt = new {}, requiredLiteralDouble = new {}, requiredLiteralBool = new {}, + optionalLiteralString = new {}, + optionalLiteralInt = new {}, + optionalLiteralDouble = new {}, + optionalLiteralBool = new {}, }; Response response = client.CreateLiteral(RequestContent.Create(data)); @@ -1072,6 +1292,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> @@ -1087,6 +1311,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -1100,6 +1328,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -1122,6 +1354,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> This sample shows how to call HelloLiteralAsync with all parameters, and how to parse the result. @@ -1153,6 +1393,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } @@ -1175,6 +1419,10 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); ]]> This sample shows how to call HelloLiteral with all parameters, and how to parse the result. @@ -1206,6 +1458,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. requiredLiteralDouble: Literal, # Required. requiredLiteralBool: Literal, # Required. + optionalLiteralString: Literal, # Required. + optionalLiteralInt: Literal, # Required. + optionalLiteralDouble: Literal, # Required. + optionalLiteralBool: Literal, # Required. } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs index 232dd5bf4cd..60508f201b5 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs @@ -28,6 +28,14 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) writer.WriteNumberValue(RequiredLiteralDouble); writer.WritePropertyName("requiredLiteralBool"); writer.WriteBooleanValue(RequiredLiteralBool); + writer.WritePropertyName("optionalLiteralString"); + writer.WriteStringValue(OptionalLiteralString); + writer.WritePropertyName("optionalLiteralInt"); + writer.WriteNumberValue(OptionalLiteralInt); + writer.WritePropertyName("optionalLiteralDouble"); + writer.WriteNumberValue(OptionalLiteralDouble); + writer.WritePropertyName("optionalLiteralBool"); + writer.WriteBooleanValue(OptionalLiteralBool); writer.WriteEndObject(); } @@ -35,10 +43,6 @@ internal static Thing DeserializeThing(JsonElement element) { string name = default; string requiredUnion = default; - string requiredLiteralString = default; - int requiredLiteralInt = default; - double requiredLiteralDouble = default; - bool requiredLiteralBool = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("name")) @@ -51,26 +55,6 @@ internal static Thing DeserializeThing(JsonElement element) requiredUnion = property.Value.GetString(); continue; } - if (property.NameEquals("requiredLiteralString")) - { - requiredLiteralString = property.Value.GetString(); - continue; - } - if (property.NameEquals("requiredLiteralInt")) - { - requiredLiteralInt = property.Value.GetInt32(); - continue; - } - if (property.NameEquals("requiredLiteralDouble")) - { - requiredLiteralDouble = property.Value.GetDouble(); - continue; - } - if (property.NameEquals("requiredLiteralBool")) - { - requiredLiteralBool = property.Value.GetBoolean(); - continue; - } } return new Thing(name, requiredUnion); } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs index e973c8f48ff..3b4fd4dd832 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs @@ -41,5 +41,17 @@ public Thing(string name, string requiredUnion) /// Gets the required literal bool. internal bool RequiredLiteralBool { get; } = false; + + /// Gets the optional literal string. + internal string OptionalLiteralString { get; } = "reject"; + + /// Gets the optional literal int. + internal int OptionalLiteralInt { get; } = 456; + + /// Gets the optional literal double. + internal double OptionalLiteralDouble { get; } = 4.56; + + /// Gets the optional literal bool. + internal bool OptionalLiteralBool { get; } = true; } } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json index 7214d3016dd..3ebdb93ea1d 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json +++ b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json @@ -253,23 +253,107 @@ "IsRequired": true, "IsReadOnly": false, "IsDiscriminator": false + }, + { + "$id": "39", + "Name": "optionalLiteralString", + "SerializedName": "optionalLiteralString", + "Description": "", + "Type": { + "$id": "40", + "Name": "Literal", + "LiteralValueType": { + "$id": "41", + "Name": "String", + "Kind": "String", + "IsNullable": false + }, + "Value": "reject", + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false + }, + { + "$id": "42", + "Name": "optionalLiteralInt", + "SerializedName": "optionalLiteralInt", + "Description": "", + "Type": { + "$id": "43", + "Name": "Literal", + "LiteralValueType": { + "$id": "44", + "Name": "Number", + "Kind": "Int32", + "IsNullable": false + }, + "Value": 456, + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false + }, + { + "$id": "45", + "Name": "optionalLiteralDouble", + "SerializedName": "optionalLiteralDouble", + "Description": "", + "Type": { + "$id": "46", + "Name": "Literal", + "LiteralValueType": { + "$id": "47", + "Name": "Number", + "Kind": "Float64", + "IsNullable": false + }, + "Value": 4.56, + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false + }, + { + "$id": "48", + "Name": "optionalLiteralBool", + "SerializedName": "optionalLiteralBool", + "Description": "", + "Type": { + "$id": "49", + "Name": "Literal", + "LiteralValueType": { + "$id": "50", + "Name": "Boolean", + "Kind": "Boolean", + "IsNullable": false + }, + "Value": true, + "IsNullable": false + }, + "IsRequired": true, + "IsReadOnly": false, + "IsDiscriminator": false } ] }, { - "$id": "39", + "$id": "51", "Name": "Friend", "Namespace": "CadlFirstTest", "IsNullable": false, "Usage": "RoundTrip", "Properties": [ { - "$id": "40", + "$id": "52", "Name": "name", "SerializedName": "name", "Description": "", "Type": { - "$id": "41", + "$id": "53", "Name": "string", "Kind": "String", "IsNullable": false @@ -281,19 +365,19 @@ ] }, { - "$id": "42", + "$id": "54", "Name": "RoundTripModel", "Namespace": "CadlFirstTest", "IsNullable": false, "Usage": "Input", "Properties": [ { - "$id": "43", + "$id": "55", "Name": "requiredString", "SerializedName": "requiredString", "Description": "", "Type": { - "$id": "44", + "$id": "56", "Name": "string", "Kind": "String", "IsNullable": false @@ -303,12 +387,12 @@ "IsDiscriminator": false }, { - "$id": "45", + "$id": "57", "Name": "requiredInt", "SerializedName": "requiredInt", "Description": "", "Type": { - "$id": "46", + "$id": "58", "Name": "int32", "Kind": "Int32", "IsNullable": false @@ -318,12 +402,12 @@ "IsDiscriminator": false }, { - "$id": "47", + "$id": "59", "Name": "requiredCollection", "SerializedName": "requiredCollection", "Description": "", "Type": { - "$id": "48", + "$id": "60", "Name": "Array", "ElementType": { "$ref": "2" @@ -335,15 +419,15 @@ "IsDiscriminator": false }, { - "$id": "49", + "$id": "61", "Name": "requiredDictionary", "SerializedName": "requiredDictionary", "Description": "", "Type": { - "$id": "50", + "$id": "62", "Name": "Dictionary", "KeyType": { - "$id": "51", + "$id": "63", "Name": "string", "Kind": "String", "IsNullable": false @@ -358,7 +442,7 @@ "IsDiscriminator": false }, { - "$id": "52", + "$id": "64", "Name": "requiredModel", "SerializedName": "requiredModel", "Description": "", @@ -374,22 +458,22 @@ ], "Clients": [ { - "$id": "53", + "$id": "65", "Name": "CadlFirstTestClient", "Description": "This is a sample cadl project.", "Operations": [ { - "$id": "54", + "$id": "66", "Name": "topAction", "ResourceName": "CadlFirstTest", "Description": "top level method", "Parameters": [ { - "$id": "55", + "$id": "67", "Name": "firstTestCadlUrl", "NameInRequest": "firstTestCadlUrl", "Type": { - "$id": "56", + "$id": "68", "Name": "Uri", "Kind": "Uri", "IsNullable": false @@ -405,11 +489,11 @@ "Kind": "Client" }, { - "$id": "57", + "$id": "69", "Name": "action", "NameInRequest": "action", "Type": { - "$id": "58", + "$id": "70", "Name": "string", "Kind": "String", "IsNullable": false @@ -425,11 +509,11 @@ "Kind": "Method" }, { - "$id": "59", + "$id": "71", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "60", + "$id": "72", "Name": "String", "Kind": "String", "IsNullable": false @@ -444,20 +528,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "61", + "$id": "73", "Type": { - "$ref": "60" + "$ref": "72" }, "Value": "application/json" } }, { - "$id": "62", + "$id": "74", "Name": "apiVersion", "NameInRequest": "api-version", "Description": "", "Type": { - "$id": "63", + "$id": "75", "Name": "String", "Kind": "String", "IsNullable": false @@ -472,9 +556,9 @@ "Explode": false, "Kind": "Client", "DefaultValue": { - "$id": "64", + "$id": "76", "Type": { - "$id": "65", + "$id": "77", "Name": "String", "Kind": "String", "IsNullable": false @@ -485,7 +569,7 @@ ], "Responses": [ { - "$id": "66", + "$id": "78", "StatusCodes": [ 200 ], @@ -505,20 +589,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "67", + "$id": "79", "Name": "topAction2", "ResourceName": "CadlFirstTest", "Description": "top level method2", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "68", + "$id": "80", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "69", + "$id": "81", "Name": "String", "Kind": "String", "IsNullable": false @@ -533,20 +617,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "70", + "$id": "82", "Type": { - "$ref": "69" + "$ref": "81" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "71", + "$id": "83", "StatusCodes": [ 200 ], @@ -566,16 +650,16 @@ "GenerateConvenienceMethod": false }, { - "$id": "72", + "$id": "84", "Name": "patchAction", "ResourceName": "CadlFirstTest", "Description": "top level patch", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "73", + "$id": "85", "Name": "body", "NameInRequest": "body", "Type": { @@ -592,11 +676,11 @@ "Kind": "Method" }, { - "$id": "74", + "$id": "86", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "75", + "$id": "87", "Name": "String", "Kind": "String", "IsNullable": false @@ -611,19 +695,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "76", + "$id": "88", "Type": { - "$ref": "75" + "$ref": "87" }, "Value": "application/json" } }, { - "$id": "77", + "$id": "89", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "78", + "$id": "90", "Name": "String", "Kind": "String", "IsNullable": false @@ -638,20 +722,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "79", + "$id": "91", "Type": { - "$ref": "78" + "$ref": "90" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "80", + "$id": "92", "StatusCodes": [ 200 ], @@ -674,16 +758,16 @@ "GenerateConvenienceMethod": false }, { - "$id": "81", + "$id": "93", "Name": "anonymousBody", "ResourceName": "CadlFirstTest", "Description": "body parameter without body decorator", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "82", + "$id": "94", "Name": "Thing", "NameInRequest": "Thing", "Type": { @@ -700,11 +784,11 @@ "Kind": "Method" }, { - "$id": "83", + "$id": "95", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "84", + "$id": "96", "Name": "String", "Kind": "String", "IsNullable": false @@ -719,19 +803,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "85", + "$id": "97", "Type": { - "$ref": "84" + "$ref": "96" }, "Value": "application/json" } }, { - "$id": "86", + "$id": "98", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "87", + "$id": "99", "Name": "String", "Kind": "String", "IsNullable": false @@ -746,20 +830,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "88", + "$id": "100", "Type": { - "$ref": "87" + "$ref": "99" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "89", + "$id": "101", "StatusCodes": [ 200 ], @@ -782,20 +866,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "90", + "$id": "102", "Name": "friendlyModel", "ResourceName": "CadlFirstTest", "Description": "Model can have its friendly name", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "91", + "$id": "103", "Name": "NotFriend", "NameInRequest": "NotFriend", "Type": { - "$ref": "39" + "$ref": "51" }, "Location": "Body", "IsRequired": true, @@ -808,11 +892,11 @@ "Kind": "Method" }, { - "$id": "92", + "$id": "104", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "93", + "$id": "105", "Name": "String", "Kind": "String", "IsNullable": false @@ -827,19 +911,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "94", + "$id": "106", "Type": { - "$ref": "93" + "$ref": "105" }, "Value": "application/json" } }, { - "$id": "95", + "$id": "107", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "96", + "$id": "108", "Name": "String", "Kind": "String", "IsNullable": false @@ -854,25 +938,25 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "97", + "$id": "109", "Type": { - "$ref": "96" + "$ref": "108" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "98", + "$id": "110", "StatusCodes": [ 200 ], "BodyType": { - "$ref": "39" + "$ref": "51" }, "BodyMediaType": "Json", "Headers": [], @@ -890,19 +974,19 @@ "GenerateConvenienceMethod": true }, { - "$id": "99", + "$id": "111", "Name": "addTimeHeader", "ResourceName": "CadlFirstTest", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "100", + "$id": "112", "Name": "repeatabilityFirstSent", "NameInRequest": "Repeatability-First-Sent", "Type": { - "$id": "101", + "$id": "113", "Name": "zonedDateTime", "Kind": "DateTime", "IsNullable": false @@ -918,11 +1002,11 @@ "Kind": "Method" }, { - "$id": "102", + "$id": "114", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "103", + "$id": "115", "Name": "String", "Kind": "String", "IsNullable": false @@ -937,20 +1021,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "104", + "$id": "116", "Type": { - "$ref": "103" + "$ref": "115" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "105", + "$id": "117", "StatusCodes": [ 204 ], @@ -967,20 +1051,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "106", + "$id": "118", "Name": "sayHi", "ResourceName": "Demo", "Description": "Return hi", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "107", + "$id": "119", "Name": "headParameter", "NameInRequest": "head-parameter", "Type": { - "$id": "108", + "$id": "120", "Name": "string", "Kind": "String", "IsNullable": false @@ -996,11 +1080,11 @@ "Kind": "Method" }, { - "$id": "109", + "$id": "121", "Name": "queryParameter", "NameInRequest": "queryParameter", "Type": { - "$id": "110", + "$id": "122", "Name": "string", "Kind": "String", "IsNullable": false @@ -1016,11 +1100,11 @@ "Kind": "Method" }, { - "$id": "111", + "$id": "123", "Name": "optionalQuery", "NameInRequest": "optionalQuery", "Type": { - "$id": "112", + "$id": "124", "Name": "string", "Kind": "String", "IsNullable": false @@ -1036,11 +1120,11 @@ "Kind": "Method" }, { - "$id": "113", + "$id": "125", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "114", + "$id": "126", "Name": "String", "Kind": "String", "IsNullable": false @@ -1055,20 +1139,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "115", + "$id": "127", "Type": { - "$ref": "114" + "$ref": "126" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "116", + "$id": "128", "StatusCodes": [ 200 ], @@ -1088,20 +1172,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "117", + "$id": "129", "Name": "helloAgain", "ResourceName": "Demo2", "Description": "Return hi again", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "118", + "$id": "130", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "119", + "$id": "131", "Name": "string", "Kind": "String", "IsNullable": false @@ -1117,14 +1201,14 @@ "Kind": "Method" }, { - "$id": "120", + "$id": "132", "Name": "contentType", "NameInRequest": "content-type", "Type": { - "$id": "121", + "$id": "133", "Name": "Literal", "LiteralValueType": { - "$id": "122", + "$id": "134", "Name": "String", "Kind": "String", "IsNullable": false @@ -1134,9 +1218,9 @@ }, "Location": "Header", "DefaultValue": { - "$id": "123", + "$id": "135", "Type": { - "$ref": "121" + "$ref": "133" }, "Value": "text/plain" }, @@ -1150,11 +1234,11 @@ "Kind": "Constant" }, { - "$id": "124", + "$id": "136", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "125", + "$id": "137", "Name": "string", "Kind": "String", "IsNullable": false @@ -1170,11 +1254,11 @@ "Kind": "Method" }, { - "$id": "126", + "$id": "138", "Name": "action", "NameInRequest": "action", "Type": { - "$ref": "42" + "$ref": "54" }, "Location": "Body", "IsRequired": true, @@ -1187,11 +1271,11 @@ "Kind": "Method" }, { - "$id": "127", + "$id": "139", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "128", + "$id": "140", "Name": "String", "Kind": "String", "IsNullable": false @@ -1206,20 +1290,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "129", + "$id": "141", "Type": { - "$ref": "128" + "$ref": "140" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "130", + "$id": "142", "StatusCodes": [ 200 ], @@ -1242,20 +1326,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "131", + "$id": "143", "Name": "noContentType", "ResourceName": "Demo2", "Description": "Return hi again", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "132", + "$id": "144", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "133", + "$id": "145", "Name": "string", "Kind": "String", "IsNullable": false @@ -1271,11 +1355,11 @@ "Kind": "Method" }, { - "$id": "134", + "$id": "146", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "135", + "$id": "147", "Name": "string", "Kind": "String", "IsNullable": false @@ -1291,11 +1375,11 @@ "Kind": "Method" }, { - "$id": "136", + "$id": "148", "Name": "action", "NameInRequest": "action", "Type": { - "$ref": "42" + "$ref": "54" }, "Location": "Body", "IsRequired": true, @@ -1308,11 +1392,11 @@ "Kind": "Method" }, { - "$id": "137", + "$id": "149", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "138", + "$id": "150", "Name": "String", "Kind": "String", "IsNullable": false @@ -1327,19 +1411,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "139", + "$id": "151", "Type": { - "$ref": "138" + "$ref": "150" }, "Value": "application/json" } }, { - "$id": "140", + "$id": "152", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "141", + "$id": "153", "Name": "String", "Kind": "String", "IsNullable": false @@ -1354,20 +1438,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "142", + "$id": "154", "Type": { - "$ref": "141" + "$ref": "153" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "143", + "$id": "155", "StatusCodes": [ 200 ], @@ -1390,20 +1474,20 @@ "GenerateConvenienceMethod": false }, { - "$id": "144", + "$id": "156", "Name": "helloDemo2", "ResourceName": "Demo2", "Description": "Return hi in demo2", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "145", + "$id": "157", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "146", + "$id": "158", "Name": "String", "Kind": "String", "IsNullable": false @@ -1418,20 +1502,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "147", + "$id": "159", "Type": { - "$ref": "146" + "$ref": "158" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "148", + "$id": "160", "StatusCodes": [ 200 ], @@ -1451,16 +1535,16 @@ "GenerateConvenienceMethod": true }, { - "$id": "149", + "$id": "161", "Name": "createLiteral", "ResourceName": "Demo2", "Description": "Create with literal value", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "150", + "$id": "162", "Name": "body", "NameInRequest": "body", "Type": { @@ -1477,11 +1561,11 @@ "Kind": "Method" }, { - "$id": "151", + "$id": "163", "Name": "contentType", "NameInRequest": "Content-Type", "Type": { - "$id": "152", + "$id": "164", "Name": "String", "Kind": "String", "IsNullable": false @@ -1496,19 +1580,19 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "153", + "$id": "165", "Type": { - "$ref": "152" + "$ref": "164" }, "Value": "application/json" } }, { - "$id": "154", + "$id": "166", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "155", + "$id": "167", "Name": "String", "Kind": "String", "IsNullable": false @@ -1523,20 +1607,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "156", + "$id": "168", "Type": { - "$ref": "155" + "$ref": "167" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "157", + "$id": "169", "StatusCodes": [ 200 ], @@ -1559,23 +1643,23 @@ "GenerateConvenienceMethod": true }, { - "$id": "158", + "$id": "170", "Name": "helloLiteral", "ResourceName": "Demo2", "Description": "Send literal parameters", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "159", + "$id": "171", "Name": "p1", "NameInRequest": "p1", "Type": { - "$id": "160", + "$id": "172", "Name": "Literal", "LiteralValueType": { - "$id": "161", + "$id": "173", "Name": "String", "Kind": "String", "IsNullable": false @@ -1585,9 +1669,9 @@ }, "Location": "Header", "DefaultValue": { - "$id": "162", + "$id": "174", "Type": { - "$ref": "160" + "$ref": "172" }, "Value": "test" }, @@ -1601,14 +1685,14 @@ "Kind": "Method" }, { - "$id": "163", + "$id": "175", "Name": "p2", "NameInRequest": "p2", "Type": { - "$id": "164", + "$id": "176", "Name": "Literal", "LiteralValueType": { - "$id": "165", + "$id": "177", "Name": "Number", "Kind": "Int32", "IsNullable": false @@ -1618,9 +1702,9 @@ }, "Location": "Path", "DefaultValue": { - "$id": "166", + "$id": "178", "Type": { - "$ref": "164" + "$ref": "176" }, "Value": 123 }, @@ -1634,14 +1718,14 @@ "Kind": "Method" }, { - "$id": "167", + "$id": "179", "Name": "p3", "NameInRequest": "p3", "Type": { - "$id": "168", + "$id": "180", "Name": "Literal", "LiteralValueType": { - "$id": "169", + "$id": "181", "Name": "Boolean", "Kind": "Boolean", "IsNullable": false @@ -1651,9 +1735,9 @@ }, "Location": "Query", "DefaultValue": { - "$id": "170", + "$id": "182", "Type": { - "$ref": "168" + "$ref": "180" }, "Value": true }, @@ -1667,11 +1751,11 @@ "Kind": "Method" }, { - "$id": "171", + "$id": "183", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "172", + "$id": "184", "Name": "String", "Kind": "String", "IsNullable": false @@ -1686,20 +1770,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "173", + "$id": "185", "Type": { - "$ref": "172" + "$ref": "184" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "174", + "$id": "186", "StatusCodes": [ 200 ], @@ -1719,20 +1803,20 @@ "GenerateConvenienceMethod": true }, { - "$id": "175", + "$id": "187", "Name": "getUnknownValue", "ResourceName": "EnumTest", "Description": "get extensible enum", "Parameters": [ { - "$ref": "55" + "$ref": "67" }, { - "$id": "176", + "$id": "188", "Name": "accept", "NameInRequest": "Accept", "Type": { - "$id": "177", + "$id": "189", "Name": "String", "Kind": "String", "IsNullable": false @@ -1747,20 +1831,20 @@ "Explode": false, "Kind": "Constant", "DefaultValue": { - "$id": "178", + "$id": "190", "Type": { - "$ref": "177" + "$ref": "189" }, "Value": "application/json" } }, { - "$ref": "62" + "$ref": "74" } ], "Responses": [ { - "$id": "179", + "$id": "191", "StatusCodes": [ 200 ], @@ -1781,19 +1865,19 @@ } ], "Protocol": { - "$id": "180" + "$id": "192" }, "Creatable": true } ], "Auth": { - "$id": "181", + "$id": "193", "ApiKey": { - "$id": "182", + "$id": "194", "Name": "x-ms-api-key" }, "OAuth2": { - "$id": "183", + "$id": "195", "Scopes": [ "https://api.example.com/.default" ] From aebde91e715fc483ba2570eec28226cac20215b1 Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 31 Jan 2023 20:30:49 +0800 Subject: [PATCH 05/10] fix test --- test/CadlRanchMockApis/src/FirstTest-Cadl.ts | 10 +++++++++- test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/test/CadlRanchMockApis/src/FirstTest-Cadl.ts b/test/CadlRanchMockApis/src/FirstTest-Cadl.ts index ad936b49110..566480666f3 100644 --- a/test/CadlRanchMockApis/src/FirstTest-Cadl.ts +++ b/test/CadlRanchMockApis/src/FirstTest-Cadl.ts @@ -14,17 +14,25 @@ Scenarios.FirstTestCadl_CreateLiteral = passOnSuccess([ requiredLiteralInt: 123, requiredLiteralDouble: 1.23, requiredLiteralBool: false, + optionalLiteralString: "reject", + optionalLiteralInt: 456, + optionalLiteralDouble: 4.56, + optionalLiteralBool: true, }); return { status: 200, body: json({ name: "literal", requiredUnion: "union", - requiredLiteralString: "accept", // below are useless + requiredLiteralString: "reject", requiredLiteralInt: 12345, requiredLiteralDouble: 123.45, requiredLiteralBool: true, + optionalLiteralString: "accept", + optionalLiteralInt: 12345, + optionalLiteralDouble: 123.45, + optionalLiteralBool: false, }) }; }), diff --git a/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs b/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs index 8584f4db060..4c6682ce045 100644 --- a/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs +++ b/test/CadlRanchProjects.Tests/LiteralValueTypeTests.cs @@ -32,6 +32,10 @@ public async Task LiteralModelProperties() => await Test(async (host) => Assert.AreEqual(result.RequiredLiteralInt, 123); Assert.AreEqual(result.RequiredLiteralDouble, 1.23); Assert.AreEqual(result.RequiredLiteralBool, false); + Assert.AreEqual(result.OptionalLiteralString, "reject"); + Assert.AreEqual(result.OptionalLiteralInt, 456); + Assert.AreEqual(result.OptionalLiteralDouble, 4.56); + Assert.AreEqual(result.OptionalLiteralBool, true); }); } From f6f82ecd59355a20ea49662bf6a3682c0a446a61 Mon Sep 17 00:00:00 2001 From: archerzz Date: Wed, 1 Feb 2023 16:05:41 +0800 Subject: [PATCH 06/10] move skip deserialization check into ModelTypeProvider --- .../Common/Output/Builders/SerializationBuilder.cs | 2 +- .../Json/JsonAdditionalPropertiesSerialization.cs | 2 +- .../Common/Output/Models/Types/ModelTypeProvider.cs | 4 +++- .../Common/Output/Models/Types/ObjectTypeProperty.cs | 2 -- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs b/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs index 0bf93d96042..fd9417a8027 100644 --- a/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs +++ b/src/AutoRest.CSharp/Common/Output/Builders/SerializationBuilder.cs @@ -267,7 +267,7 @@ private IEnumerable GetPropertySerializationsFromBag( BuildSerialization(property.Schema, objectProperty.ValueType), property.IsRequired, property.IsReadOnly, - objectProperty.ShouldSkipDeserialization, + false, objectProperty.OptionalViaNullability); } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs b/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs index 2882b7bb4b5..d52a40c24fc 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Serialization/Json/JsonAdditionalPropertiesSerialization.cs @@ -12,7 +12,7 @@ internal class JsonAdditionalPropertiesSerialization : JsonPropertySerialization public CSharpType Type { get; } public JsonAdditionalPropertiesSerialization(ObjectTypeProperty property, JsonSerialization valueSerialization, CSharpType type) - : base(property.Declaration.Name.ToVariableName(), property.Declaration.Name, property.Declaration.Name, property.Declaration.Type, property.ValueType, valueSerialization, true, property.IsReadOnly, property.ShouldSkipDeserialization, property.OptionalViaNullability) + : base(property.Declaration.Name.ToVariableName(), property.Declaration.Name, property.Declaration.Name, property.Declaration.Type, property.ValueType, valueSerialization, true, property.IsReadOnly, false, property.OptionalViaNullability) { Type = type; } diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs index dccfa607771..d3adf957618 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ModelTypeProvider.cs @@ -147,7 +147,7 @@ private IEnumerable CreatePropertySerializations() valueSerialization, property.IsRequired, shouldSkipSerialization, - property.ShouldSkipDeserialization, + ShouldSkipDeserialization(property), optionalViaNullability)); } } @@ -179,6 +179,8 @@ private bool ShouldSkipSerialization(ObjectTypeProperty property) return property.IsReadOnly && _inputModel.Usage is not InputModelTypeUsage.Input; } + private bool ShouldSkipDeserialization(ObjectTypeProperty property) => property.InputModelProperty?.Type is InputLiteralType; + private ConstructorSignature? CreateSerializationConstructorSignature(string name, IReadOnlyList publicParameters, IReadOnlyList serializationParameters) { if (!serializationParameters.Any(p => TypeFactory.IsList(p.Type)) && publicParameters.SequenceEqual(serializationParameters)) diff --git a/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs b/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs index 49ea0046d23..3949aef6c4d 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/Types/ObjectTypeProperty.cs @@ -168,7 +168,5 @@ private static string CreateExtraPropertyDiscriminatorSummary(CSharpType valueTy } return updatedDescription; } - - public bool ShouldSkipDeserialization => InputModelProperty?.Type is InputLiteralType; } } From c0b82f904e291b4e775faf01807d40274e2f22c0 Mon Sep 17 00:00:00 2001 From: archerzz Date: Thu, 2 Feb 2023 17:06:42 +0800 Subject: [PATCH 07/10] treat literal operation parameters as constant parameters --- .../Common/Output/Models/RestClientBuilder.cs | 2 +- .../Output/OperationMethodChainBuilder.cs | 8 +-- .../Generated/CadlFirstTestClient.cs | 52 +++++-------------- .../Generated/Docs/CadlFirstTestClient.xml | 44 +--------------- 4 files changed, 21 insertions(+), 85 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs b/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs index 3da12984b1f..0fa7141bb69 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs @@ -351,7 +351,7 @@ private ReferenceOrConstant CreateReference(InputParameter operationParameter, P return (ReferenceOrConstant)_parameters[operationParameter.Name]; } - if (operationParameter.Kind == InputOperationParameterKind.Constant && parameter.DefaultValue != null) + if ((operationParameter is { Kind:InputOperationParameterKind.Constant } or {Type: InputLiteralType }) && parameter.DefaultValue is not null) { return (ReferenceOrConstant)parameter.DefaultValue; } diff --git a/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs b/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs index 914b29d02fc..3a9bb7ffc5a 100644 --- a/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs +++ b/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs @@ -400,7 +400,7 @@ private void AddParameter(string name, InputParameter inputParameter, CSharpType var protocolMethodParameter = BuildParameter(inputParameter, frameworkParameterType ?? ChangeTypeForProtocolMethod(inputParameter.Type)); AddReference(name, inputParameter, protocolMethodParameter, SerializationBuilder.GetSerializationFormat(inputParameter.Type)); - if (inputParameter.Kind is InputOperationParameterKind.Client or InputOperationParameterKind.Constant) + if (inputParameter is { Kind: InputOperationParameterKind.Client or InputOperationParameterKind.Constant } or { Type: InputLiteralType }) { return; } @@ -434,9 +434,9 @@ private void AddReference(string nameInRequest, InputParameter? operationParamet _requestParts.Add(new RequestPartSource(nameInRequest, operationParameter, reference, serializationFormat)); } - private ReferenceOrConstant CreateReference(InputParameter? operationParameter, Parameter parameter) + private ReferenceOrConstant CreateReference(InputParameter operationParameter, Parameter parameter) { - if (operationParameter?.Kind == InputOperationParameterKind.Client) + if (operationParameter.Kind == InputOperationParameterKind.Client) { var field = operationParameter.IsEndpoint ? _fields.EndpointField : _fields.GetFieldByParameter(parameter); if (field == null) @@ -447,7 +447,7 @@ private ReferenceOrConstant CreateReference(InputParameter? operationParameter, return new Reference(field.Name, field.Type); } - if (operationParameter?.Kind == InputOperationParameterKind.Constant && parameter.DefaultValue != null) + if ((operationParameter is { Kind: InputOperationParameterKind.Constant } or { Type: InputLiteralType }) && parameter.DefaultValue is not null) { return (ReferenceOrConstant)parameter.DefaultValue; } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs b/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs index 78bbfacb746..fcd70f60adc 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/CadlFirstTestClient.cs @@ -845,21 +845,15 @@ public virtual Response CreateLiteral(RequestContent content, RequestContext con } /// Send literal parameters. - /// The Literal to use. - /// The Literal to use. - /// The Literal to use. /// The cancellation token to use. - /// is null. - public virtual async Task> HelloLiteralValueAsync(int p2 = 123, string p1 = "test", bool p3 = true, CancellationToken cancellationToken = default) + public virtual async Task> HelloLiteralValueAsync(CancellationToken cancellationToken = default) { - Argument.AssertNotNull(p1, nameof(p1)); - using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteralValue"); scope.Start(); try { RequestContext context = FromCancellationToken(cancellationToken); - Response response = await HelloLiteralAsync(p2, p1, p3, context).ConfigureAwait(false); + Response response = await HelloLiteralAsync(context).ConfigureAwait(false); return Response.FromValue(Thing.FromResponse(response), response); } catch (Exception e) @@ -870,21 +864,15 @@ public virtual async Task> HelloLiteralValueAsync(int p2 = 123, } /// Send literal parameters. - /// The Literal to use. - /// The Literal to use. - /// The Literal to use. /// The cancellation token to use. - /// is null. - public virtual Response HelloLiteralValue(int p2 = 123, string p1 = "test", bool p3 = true, CancellationToken cancellationToken = default) + public virtual Response HelloLiteralValue(CancellationToken cancellationToken = default) { - Argument.AssertNotNull(p1, nameof(p1)); - using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteralValue"); scope.Start(); try { RequestContext context = FromCancellationToken(cancellationToken); - Response response = HelloLiteral(p2, p1, p3, context); + Response response = HelloLiteral(context); return Response.FromValue(Thing.FromResponse(response), response); } catch (Exception e) @@ -895,23 +883,17 @@ public virtual Response HelloLiteralValue(int p2 = 123, string p1 = "test } /// Send literal parameters. - /// The Literal to use. - /// The Literal to use. - /// The Literal to use. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. - /// is null. /// Service returned a non-success status code. /// The response returned from the service. Details of the response body schema are in the Remarks section below. - /// - public virtual async Task HelloLiteralAsync(int p2 = 123, string p1 = "test", bool p3 = true, RequestContext context = null) + /// + public virtual async Task HelloLiteralAsync(RequestContext context = null) { - Argument.AssertNotNull(p1, nameof(p1)); - using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteral"); scope.Start(); try { - using HttpMessage message = CreateHelloLiteralRequest(p2, p1, p3, context); + using HttpMessage message = CreateHelloLiteralRequest(context); return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); } catch (Exception e) @@ -922,23 +904,17 @@ public virtual async Task HelloLiteralAsync(int p2 = 123, string p1 = } /// Send literal parameters. - /// The Literal to use. - /// The Literal to use. - /// The Literal to use. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. - /// is null. /// Service returned a non-success status code. /// The response returned from the service. Details of the response body schema are in the Remarks section below. - /// - public virtual Response HelloLiteral(int p2 = 123, string p1 = "test", bool p3 = true, RequestContext context = null) + /// + public virtual Response HelloLiteral(RequestContext context = null) { - Argument.AssertNotNull(p1, nameof(p1)); - using var scope = ClientDiagnostics.CreateScope("CadlFirstTestClient.HelloLiteral"); scope.Start(); try { - using HttpMessage message = CreateHelloLiteralRequest(p2, p1, p3, context); + using HttpMessage message = CreateHelloLiteralRequest(context); return _pipeline.ProcessMessage(message, context); } catch (Exception e) @@ -1171,7 +1147,7 @@ internal HttpMessage CreateCreateLiteralRequest(RequestContent content, RequestC return message; } - internal HttpMessage CreateHelloLiteralRequest(int p2, string p1, bool p3, RequestContext context) + internal HttpMessage CreateHelloLiteralRequest(RequestContext context) { var message = _pipeline.CreateMessage(context, ResponseClassifier200); var request = message.Request; @@ -1179,11 +1155,11 @@ internal HttpMessage CreateHelloLiteralRequest(int p2, string p1, bool p3, Reque var uri = new RawRequestUriBuilder(); uri.Reset(_endpoint); uri.AppendPath("/helloLiteral/", false); - uri.AppendPath(p2, true); - uri.AppendQuery("p3", p3, true); + uri.AppendPath(123, true); + uri.AppendQuery("p3", true, true); uri.AppendQuery("api-version", _apiVersion, true); request.Uri = uri; - request.Headers.Add("p1", p1); + request.Headers.Add("p1", "test"); request.Headers.Add("Accept", "application/json"); return message; } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml index 326e7e8b4af..f49274dd010 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml +++ b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml @@ -1337,7 +1337,7 @@ Schema for Thing: - + This sample shows how to call HelloLiteralAsync and parse the result. -This sample shows how to call HelloLiteralAsync with all parameters, and how to parse the result. -"); -var endpoint = new Uri(""); -var client = new CadlFirstTestClient(endpoint, credential); - -Response response = await client.HelloLiteralAsync(123, , true); - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); @@ -1402,7 +1382,7 @@ Schema for Thing: - + This sample shows how to call HelloLiteral and parse the result. -This sample shows how to call HelloLiteral with all parameters, and how to parse the result. -"); -var endpoint = new Uri(""); -var client = new CadlFirstTestClient(endpoint, credential); - -Response response = client.HelloLiteral(123, , true); - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; Console.WriteLine(result.GetProperty("name").ToString()); Console.WriteLine(result.GetProperty("requiredUnion").ToString()); From a3633ca85e0bd76fff2a13ed93e0cf6b5e7bde4b Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 7 Feb 2023 10:29:50 +0800 Subject: [PATCH 08/10] set the parameter type to constant for literal type in emitter --- .../Common/Generation/Writers/JsonCodeWriterExtensions.cs | 7 +------ .../Common/Output/Models/RestClientBuilder.cs | 2 +- .../LowLevel/Output/OperationMethodChainBuilder.cs | 4 ++-- src/CADL.Extension/Emitter.Csharp/src/emitter.ts | 2 +- test/TestProjects/FirstTest-Cadl/Generated/cadl.json | 6 +++--- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs b/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs index f0efbe935f3..d68ee6e2608 100644 --- a/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs +++ b/src/AutoRest.CSharp/Common/Generation/Writers/JsonCodeWriterExtensions.cs @@ -286,13 +286,8 @@ private static void ToSerializeCall(this CodeWriter writer, FormattableString wr private static void DeserializeIntoObjectProperties(this CodeWriter writer, IEnumerable propertySerializations, FormattableString itemVariable, Dictionary propertyVariables) { - foreach (JsonPropertySerialization property in propertySerializations) + foreach (JsonPropertySerialization property in propertySerializations.Where(p => !p.ShouldSkipDeserialization)) { - if (property.ShouldSkipDeserialization) - { - continue; - } - writer.Append($"if({itemVariable}.NameEquals({property.SerializedName:L}))"); using (writer.Scope()) { diff --git a/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs b/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs index 0fa7141bb69..c9e7700428c 100644 --- a/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs +++ b/src/AutoRest.CSharp/Common/Output/Models/RestClientBuilder.cs @@ -351,7 +351,7 @@ private ReferenceOrConstant CreateReference(InputParameter operationParameter, P return (ReferenceOrConstant)_parameters[operationParameter.Name]; } - if ((operationParameter is { Kind:InputOperationParameterKind.Constant } or {Type: InputLiteralType }) && parameter.DefaultValue is not null) + if (operationParameter is { Kind:InputOperationParameterKind.Constant } && parameter.DefaultValue is not null) { return (ReferenceOrConstant)parameter.DefaultValue; } diff --git a/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs b/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs index 7d66dc90353..10a0f5aa0f5 100644 --- a/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs +++ b/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs @@ -402,7 +402,7 @@ private void AddParameter(string name, InputParameter inputParameter, CSharpType var protocolMethodParameter = BuildParameter(inputParameter, frameworkParameterType ?? ChangeTypeForProtocolMethod(inputParameter.Type)); AddReference(name, inputParameter, protocolMethodParameter, SerializationBuilder.GetSerializationFormat(inputParameter.Type)); - if (inputParameter is { Kind: InputOperationParameterKind.Client or InputOperationParameterKind.Constant } or { Type: InputLiteralType }) + if (inputParameter.Kind is InputOperationParameterKind.Client or InputOperationParameterKind.Constant) { return; } @@ -449,7 +449,7 @@ private ReferenceOrConstant CreateReference(InputParameter operationParameter, P return new Reference(field.Name, field.Type); } - if ((operationParameter is { Kind: InputOperationParameterKind.Constant } or { Type: InputLiteralType }) && parameter.DefaultValue is not null) + if (operationParameter.Kind is InputOperationParameterKind.Constant && parameter.DefaultValue is not null) { return (ReferenceOrConstant)parameter.DefaultValue; } diff --git a/src/CADL.Extension/Emitter.Csharp/src/emitter.ts b/src/CADL.Extension/Emitter.Csharp/src/emitter.ts index 79ef28d759c..13f9f3e4ea3 100644 --- a/src/CADL.Extension/Emitter.Csharp/src/emitter.ts +++ b/src/CADL.Extension/Emitter.Csharp/src/emitter.ts @@ -846,7 +846,7 @@ function loadOperation( const isContentType: boolean = requestLocation === RequestLocation.Header && name.toLowerCase() === "content-type"; - const kind: InputOperationParameterKind = isContentType + const kind: InputOperationParameterKind = isContentType || inputType.Name === "Literal" ? InputOperationParameterKind.Constant : isApiVer ? defaultValue diff --git a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json index c3f599c171c..5ddf283e834 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json +++ b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json @@ -1708,7 +1708,7 @@ "IsEndpoint": false, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Method" + "Kind": "Constant" }, { "$id": "177", @@ -1741,7 +1741,7 @@ "IsEndpoint": false, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Method" + "Kind": "Constant" }, { "$id": "181", @@ -1774,7 +1774,7 @@ "IsEndpoint": false, "SkipUrlEncoding": false, "Explode": false, - "Kind": "Method" + "Kind": "Constant" }, { "$id": "185", From 7c472e6b778afc53fbc2f356a020c47ab2c4a2b8 Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 7 Feb 2023 10:36:08 +0800 Subject: [PATCH 09/10] fix typescript format --- src/CADL.Extension/Emitter.Csharp/src/emitter.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/CADL.Extension/Emitter.Csharp/src/emitter.ts b/src/CADL.Extension/Emitter.Csharp/src/emitter.ts index 13f9f3e4ea3..89d038cbae7 100644 --- a/src/CADL.Extension/Emitter.Csharp/src/emitter.ts +++ b/src/CADL.Extension/Emitter.Csharp/src/emitter.ts @@ -846,13 +846,14 @@ function loadOperation( const isContentType: boolean = requestLocation === RequestLocation.Header && name.toLowerCase() === "content-type"; - const kind: InputOperationParameterKind = isContentType || inputType.Name === "Literal" - ? InputOperationParameterKind.Constant - : isApiVer - ? defaultValue + const kind: InputOperationParameterKind = + isContentType || inputType.Name === "Literal" ? InputOperationParameterKind.Constant - : InputOperationParameterKind.Client - : InputOperationParameterKind.Method; + : isApiVer + ? defaultValue + ? InputOperationParameterKind.Constant + : InputOperationParameterKind.Client + : InputOperationParameterKind.Method; return { Name: param.name, NameInRequest: name, From 6ca9a26931eeb0c0e15237364119c7b57069db70 Mon Sep 17 00:00:00 2001 From: archerzz Date: Tue, 7 Feb 2023 11:53:04 +0800 Subject: [PATCH 10/10] add back optional literal model properties --- .../FirstTest-Cadl/FirstTest-Cadl.cadl | 8 +- .../Generated/Docs/CadlFirstTestClient.xml | 668 ++++++++++++++---- .../Generated/Models/Thing.Serialization.cs | 49 +- .../FirstTest-Cadl/Generated/Models/Thing.cs | 6 +- .../FirstTest-Cadl/Generated/cadl.json | 8 +- 5 files changed, 564 insertions(+), 175 deletions(-) diff --git a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl index bb72003d4a1..1466e669aea 100644 --- a/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl +++ b/test/TestProjects/FirstTest-Cadl/FirstTest-Cadl.cadl @@ -70,16 +70,16 @@ model Thing { requiredLiteralBool: false; @doc("optional literal string") - optionalLiteralString: "reject"; + optionalLiteralString?: "reject"; @doc("optional literal int") - optionalLiteralInt: 456; + optionalLiteralInt?: 456; @doc("optional literal double") - optionalLiteralDouble: 4.56; + optionalLiteralDouble?: 4.56; @doc("optional literal bool") - optionalLiteralBool: true; + optionalLiteralBool?: true; @doc("description with xml <|endoftext|>") requiredBadDescription: string diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml index dd9846c2d56..e10bf630118 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml +++ b/test/TestProjects/FirstTest-Cadl/Generated/Docs/CadlFirstTestClient.xml @@ -38,10 +38,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -85,10 +85,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -132,10 +132,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -179,10 +179,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -191,7 +191,34 @@ Schema for Thing: -This sample shows how to call PatchActionAsync with required request content and parse the result. +This sample shows how to call PatchActionAsync with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = await client.PatchActionAsync(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call PatchActionAsync with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -240,10 +267,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -258,10 +285,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -270,7 +297,34 @@ Schema for Thing: -This sample shows how to call PatchAction with required request content and parse the result. +This sample shows how to call PatchAction with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = client.PatchAction(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call PatchAction with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -319,10 +373,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -337,10 +391,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -349,7 +403,34 @@ Schema for Thing: -This sample shows how to call AnonymousBodyAsync with required request content and parse the result. +This sample shows how to call AnonymousBodyAsync with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = await client.AnonymousBodyAsync(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call AnonymousBodyAsync with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -398,10 +479,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -416,10 +497,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -428,7 +509,34 @@ Schema for Thing: -This sample shows how to call AnonymousBody with required request content and parse the result. +This sample shows how to call AnonymousBody with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = client.AnonymousBody(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call AnonymousBody with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -477,10 +585,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -495,10 +603,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -644,10 +752,6 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); ]]> This sample shows how to call SayHiAsync with all parameters, and how to parse the result. @@ -685,10 +789,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -712,10 +816,6 @@ Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralString").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralInt").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralDouble").ToString()); -Console.WriteLine(result.GetProperty("optionalLiteralBool").ToString()); Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); ]]> This sample shows how to call SayHi with all parameters, and how to parse the result. @@ -753,10 +853,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -765,7 +865,44 @@ Schema for Thing: -This sample shows how to call HelloAgainAsync with required parameters and request content and parse the result. +This sample shows how to call HelloAgainAsync with required parameters and request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + requiredString = "", + requiredInt = 1234, + requiredCollection = new[] { + "1" + }, + requiredDictionary = new { + key = "1", + }, + requiredModel = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", + }, +}; + +Response response = await client.HelloAgainAsync("", "", RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call HelloAgainAsync with all parameters and request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -829,10 +966,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; }, # Required. Required model } @@ -848,10 +985,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -860,7 +997,44 @@ Schema for Thing: -This sample shows how to call HelloAgain with required parameters and request content and parse the result. +This sample shows how to call HelloAgain with required parameters and request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + requiredString = "", + requiredInt = 1234, + requiredCollection = new[] { + "1" + }, + requiredDictionary = new { + key = "1", + }, + requiredModel = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", + }, +}; + +Response response = client.HelloAgain("", "", RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call HelloAgain with all parameters and request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -924,10 +1098,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; }, # Required. Required model } @@ -943,10 +1117,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -955,7 +1129,44 @@ Schema for Thing: -This sample shows how to call NoContentTypeAsync with required parameters and request content and parse the result. +This sample shows how to call NoContentTypeAsync with required parameters and request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + requiredString = "", + requiredInt = 1234, + requiredCollection = new[] { + "1" + }, + requiredDictionary = new { + key = "1", + }, + requiredModel = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", + }, +}; + +Response response = await client.NoContentTypeAsync("", "", RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call NoContentTypeAsync with all parameters and request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -1019,10 +1230,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; }, # Required. Required model } @@ -1038,10 +1249,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1050,7 +1261,44 @@ Schema for Thing: -This sample shows how to call NoContentType with required parameters and request content and parse the result. +This sample shows how to call NoContentType with required parameters and request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + requiredString = "", + requiredInt = 1234, + requiredCollection = new[] { + "1" + }, + requiredDictionary = new { + key = "1", + }, + requiredModel = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", + }, +}; + +Response response = client.NoContentType("", "", RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call NoContentType with all parameters and request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -1114,10 +1362,10 @@ Schema for RoundTripModel: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; }, # Required. Required model } @@ -1133,10 +1381,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1180,10 +1428,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1227,10 +1475,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1239,7 +1487,34 @@ Schema for Thing: -This sample shows how to call CreateLiteralAsync with required request content and parse the result. +This sample shows how to call CreateLiteralAsync with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = await client.CreateLiteralAsync(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call CreateLiteralAsync with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -1288,10 +1563,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1306,10 +1581,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1318,7 +1593,34 @@ Schema for Thing: -This sample shows how to call CreateLiteral with required request content and parse the result. +This sample shows how to call CreateLiteral with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = client.CreateLiteral(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call CreateLiteral with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -1367,10 +1669,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1385,10 +1687,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1432,10 +1734,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1479,10 +1781,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1521,7 +1823,34 @@ Console.WriteLine(result.ToString()); -This sample shows how to call InternalProtocolAsync with required request content and parse the result. +This sample shows how to call InternalProtocolAsync with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = await client.InternalProtocolAsync(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call InternalProtocolAsync with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -1570,10 +1899,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1588,10 +1917,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1600,7 +1929,34 @@ Schema for Thing: -This sample shows how to call InternalProtocol with required request content and parse the result. +This sample shows how to call InternalProtocol with required request content, and how to parse the result. +"); +var endpoint = new Uri(""); +var client = new CadlFirstTestClient(endpoint, credential); + +var data = new { + name = "", + requiredUnion = new {}, + requiredLiteralString = new {}, + requiredLiteralInt = new {}, + requiredLiteralDouble = new {}, + requiredLiteralBool = new {}, + requiredBadDescription = "", +}; + +Response response = client.InternalProtocol(RequestContent.Create(data)); + +JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; +Console.WriteLine(result.GetProperty("name").ToString()); +Console.WriteLine(result.GetProperty("requiredUnion").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralString").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralInt").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralDouble").ToString()); +Console.WriteLine(result.GetProperty("requiredLiteralBool").ToString()); +Console.WriteLine(result.GetProperty("requiredBadDescription").ToString()); +]]> +This sample shows how to call InternalProtocol with all request content, and how to parse the result. "); var endpoint = new Uri(""); @@ -1649,10 +2005,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } @@ -1667,10 +2023,10 @@ Schema for Thing: requiredLiteralInt: Literal, # Required. required literal int requiredLiteralDouble: Literal, # Required. required literal double requiredLiteralBool: Literal, # Required. required literal bool - optionalLiteralString: Literal, # Required. optional literal string - optionalLiteralInt: Literal, # Required. optional literal int - optionalLiteralDouble: Literal, # Required. optional literal double - optionalLiteralBool: Literal, # Required. optional literal bool + optionalLiteralString: Literal, # Optional. optional literal string + optionalLiteralInt: Literal, # Optional. optional literal int + optionalLiteralDouble: Literal, # Optional. optional literal double + optionalLiteralBool: Literal, # Optional. optional literal bool requiredBadDescription: string, # Required. description with xml &lt;|endoftext|&gt; } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs index c661f2a7526..5ddce115df7 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.Serialization.cs @@ -28,14 +28,47 @@ void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) writer.WriteNumberValue(RequiredLiteralDouble); writer.WritePropertyName("requiredLiteralBool"); writer.WriteBooleanValue(RequiredLiteralBool); - writer.WritePropertyName("optionalLiteralString"); - writer.WriteStringValue(OptionalLiteralString); - writer.WritePropertyName("optionalLiteralInt"); - writer.WriteNumberValue(OptionalLiteralInt); - writer.WritePropertyName("optionalLiteralDouble"); - writer.WriteNumberValue(OptionalLiteralDouble); - writer.WritePropertyName("optionalLiteralBool"); - writer.WriteBooleanValue(OptionalLiteralBool); + if (Optional.IsDefined(OptionalLiteralString)) + { + writer.WritePropertyName("optionalLiteralString"); + writer.WriteStringValue(OptionalLiteralString); + } + if (Optional.IsDefined(OptionalLiteralInt)) + { + if (OptionalLiteralInt != null) + { + writer.WritePropertyName("optionalLiteralInt"); + writer.WriteNumberValue(OptionalLiteralInt.Value); + } + else + { + writer.WriteNull("optionalLiteralInt"); + } + } + if (Optional.IsDefined(OptionalLiteralDouble)) + { + if (OptionalLiteralDouble != null) + { + writer.WritePropertyName("optionalLiteralDouble"); + writer.WriteNumberValue(OptionalLiteralDouble.Value); + } + else + { + writer.WriteNull("optionalLiteralDouble"); + } + } + if (Optional.IsDefined(OptionalLiteralBool)) + { + if (OptionalLiteralBool != null) + { + writer.WritePropertyName("optionalLiteralBool"); + writer.WriteBooleanValue(OptionalLiteralBool.Value); + } + else + { + writer.WriteNull("optionalLiteralBool"); + } + } writer.WritePropertyName("requiredBadDescription"); writer.WriteStringValue(RequiredBadDescription); writer.WriteEndObject(); diff --git a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs index 1c6c12a0dae..e997022c02f 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs +++ b/test/TestProjects/FirstTest-Cadl/Generated/Models/Thing.cs @@ -49,13 +49,13 @@ public Thing(string name, string requiredUnion, string requiredBadDescription) internal string OptionalLiteralString { get; } = "reject"; /// optional literal int. - internal int OptionalLiteralInt { get; } = 456; + internal int? OptionalLiteralInt { get; } = 456; /// optional literal double. - internal double OptionalLiteralDouble { get; } = 4.56; + internal double? OptionalLiteralDouble { get; } = 4.56; /// optional literal bool. - internal bool OptionalLiteralBool { get; } = true; + internal bool? OptionalLiteralBool { get; } = true; /// description with xml <|endoftext|>. public string RequiredBadDescription { get; set; } diff --git a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json index 5ddf283e834..56ebc7ab9f6 100644 --- a/test/TestProjects/FirstTest-Cadl/Generated/cadl.json +++ b/test/TestProjects/FirstTest-Cadl/Generated/cadl.json @@ -271,7 +271,7 @@ "Value": "reject", "IsNullable": false }, - "IsRequired": true, + "IsRequired": false, "IsReadOnly": false, "IsDiscriminator": false }, @@ -292,7 +292,7 @@ "Value": 456, "IsNullable": false }, - "IsRequired": true, + "IsRequired": false, "IsReadOnly": false, "IsDiscriminator": false }, @@ -313,7 +313,7 @@ "Value": 4.56, "IsNullable": false }, - "IsRequired": true, + "IsRequired": false, "IsReadOnly": false, "IsDiscriminator": false }, @@ -334,7 +334,7 @@ "Value": true, "IsNullable": false }, - "IsRequired": true, + "IsRequired": false, "IsReadOnly": false, "IsDiscriminator": false },