diff --git a/src/Facility.CodeGen.CSharp/CSharpGenerator.cs b/src/Facility.CodeGen.CSharp/CSharpGenerator.cs index 0610e73..bc076d1 100644 --- a/src/Facility.CodeGen.CSharp/CSharpGenerator.cs +++ b/src/Facility.CodeGen.CSharp/CSharpGenerator.cs @@ -910,7 +910,8 @@ private CodeGenFile GenerateHttpMapping(HttpServiceInfo httpServiceInfo, Context foreach (var field in httpMethodInfo.RequestNormalFields) { var fieldName = context.GetFieldPropertyName(field.ServiceField); - code.WriteLine($"{fieldName} = (({requestTypeName}?) body)?.{fieldName},"); + var fieldType = context.GetFieldType(field.ServiceField); + code.WriteLine($"{fieldName} = (({requestTypeName}?) body)?.{fieldName}{(fieldType.Kind == ServiceTypeKind.Nullable ? " ?? default" : "")},"); } } } diff --git a/tools/EdgeCases.fsd b/tools/EdgeCases.fsd index c8226f3..5eb4e10 100644 --- a/tools/EdgeCases.fsd +++ b/tools/EdgeCases.fsd @@ -106,4 +106,14 @@ service EdgeCases foo: ExternalData; bar: ExternalEnum; } + + method misc + { + [http(from: "query")] + q: string; + + f: nullable; + }: + { + } } diff --git a/tools/EdgeCases/DelegatingEdgeCases.g.cs b/tools/EdgeCases/DelegatingEdgeCases.g.cs index 7a0bad5..3c1c363 100644 --- a/tools/EdgeCases/DelegatingEdgeCases.g.cs +++ b/tools/EdgeCases/DelegatingEdgeCases.g.cs @@ -44,6 +44,9 @@ public virtual async Task> CustomHttpAsync( public virtual async Task> SnakeMethodAsync(SnakeMethodRequestDto request, CancellationToken cancellationToken = default) => (await m_serviceDelegate.InvokeMethodAsync(EdgeCasesMethods.SnakeMethod, request, cancellationToken).ConfigureAwait(false)).Cast(); + public virtual async Task> MiscAsync(MiscRequestDto request, CancellationToken cancellationToken = default) => + (await m_serviceDelegate.InvokeMethodAsync(EdgeCasesMethods.Misc, request, cancellationToken).ConfigureAwait(false)).Cast(); + private readonly ServiceDelegate m_serviceDelegate; } } diff --git a/tools/EdgeCases/EdgeCasesJsonSerializerContext.g.cs b/tools/EdgeCases/EdgeCasesJsonSerializerContext.g.cs index cd602f1..af6775a 100644 --- a/tools/EdgeCases/EdgeCasesJsonSerializerContext.g.cs +++ b/tools/EdgeCases/EdgeCasesJsonSerializerContext.g.cs @@ -14,6 +14,8 @@ namespace EdgeCases [JsonSerializable(typeof(CustomHttpResponseDto))] [JsonSerializable(typeof(DataDto))] [JsonSerializable(typeof(DataWithExternalTypesDto))] + [JsonSerializable(typeof(MiscRequestDto))] + [JsonSerializable(typeof(MiscResponseDto))] [JsonSerializable(typeof(OldDataDto))] [JsonSerializable(typeof(OldEmptyDataDto))] [JsonSerializable(typeof(OldMethodRequestDto))] diff --git a/tools/EdgeCases/EdgeCasesMethods.g.cs b/tools/EdgeCases/EdgeCasesMethods.g.cs index 9deb0c0..8e367b8 100644 --- a/tools/EdgeCases/EdgeCasesMethods.g.cs +++ b/tools/EdgeCases/EdgeCasesMethods.g.cs @@ -22,5 +22,9 @@ internal static class EdgeCasesMethods public static readonly IServiceMethodInfo SnakeMethod = ServiceMethodInfo.Create( "snake_method", "EdgeCases", x => x.SnakeMethodAsync); + + public static readonly IServiceMethodInfo Misc = + ServiceMethodInfo.Create( + "misc", "EdgeCases", x => x.MiscAsync); } } diff --git a/tools/EdgeCases/Http/EdgeCasesHttpHandler.g.cs b/tools/EdgeCases/Http/EdgeCasesHttpHandler.g.cs index 8cbfaad..755809b 100644 --- a/tools/EdgeCases/Http/EdgeCasesHttpHandler.g.cs +++ b/tools/EdgeCases/Http/EdgeCasesHttpHandler.g.cs @@ -41,6 +41,7 @@ public EdgeCasesHttpHandler(Func getService, Ser public override async Task TryHandleHttpRequestAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) { return await AdaptTask(TryHandleCustomHttpAsync(httpRequest, cancellationToken)).ConfigureAwait(true) ?? + await AdaptTask(TryHandleMiscAsync(httpRequest, cancellationToken)).ConfigureAwait(true) ?? await AdaptTask(TryHandleOldMethodAsync(httpRequest, cancellationToken)).ConfigureAwait(true) ?? await AdaptTask(TryHandleSnakeMethodAsync(httpRequest, cancellationToken)).ConfigureAwait(true); } @@ -61,6 +62,9 @@ await AdaptTask(TryHandleOldMethodAsync(httpRequest, cancellationToken)).Configu public Task TryHandleSnakeMethodAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) => TryHandleServiceMethodAsync(EdgeCasesHttpMapping.SnakeMethodMapping, httpRequest, GetService(httpRequest).SnakeMethodAsync, cancellationToken); + public Task TryHandleMiscAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken = default) => + TryHandleServiceMethodAsync(EdgeCasesHttpMapping.MiscMapping, httpRequest, GetService(httpRequest).MiscAsync, cancellationToken); + /// /// Returns the HTTP status code for a custom error code. /// diff --git a/tools/EdgeCases/Http/EdgeCasesHttpMapping.g.cs b/tools/EdgeCases/Http/EdgeCasesHttpMapping.g.cs index e4546f1..9d1d2fd 100644 --- a/tools/EdgeCases/Http/EdgeCasesHttpMapping.g.cs +++ b/tools/EdgeCases/Http/EdgeCasesHttpMapping.g.cs @@ -90,5 +90,41 @@ public static partial class EdgeCasesHttpMapping }.Build(), }, }.Build(); + + public static readonly HttpMethodMapping MiscMapping = + new HttpMethodMapping.Builder + { + HttpMethod = HttpMethod.Post, + Path = "/misc", + GetUriParameters = request => + new Dictionary + { + { "q", request.Q }, + }, + SetUriParameters = (request, parameters) => + { + parameters.TryGetValue("q", out var queryParameterQ); + request.Q = queryParameterQ; + return request; + }, + RequestBodyType = typeof(MiscRequestDto), + GetRequestBody = request => + new MiscRequestDto + { + F = request.F, + }, + CreateRequest = body => + new MiscRequestDto + { + F = ((MiscRequestDto?) body)?.F ?? default, + }, + ResponseMappings = + { + new HttpResponseMapping.Builder + { + StatusCode = (HttpStatusCode) 200, + }.Build(), + }, + }.Build(); } } diff --git a/tools/EdgeCases/Http/HttpClientEdgeCases.g.cs b/tools/EdgeCases/Http/HttpClientEdgeCases.g.cs index 3279885..721e2cb 100644 --- a/tools/EdgeCases/Http/HttpClientEdgeCases.g.cs +++ b/tools/EdgeCases/Http/HttpClientEdgeCases.g.cs @@ -37,6 +37,9 @@ public Task> CustomHttpAsync(CustomHttpRequ public Task> SnakeMethodAsync(SnakeMethodRequestDto request, CancellationToken cancellationToken = default) => TrySendRequestAsync(EdgeCasesHttpMapping.SnakeMethodMapping, request, cancellationToken); + public Task> MiscAsync(MiscRequestDto request, CancellationToken cancellationToken = default) => + TrySendRequestAsync(EdgeCasesHttpMapping.MiscMapping, request, cancellationToken); + private static readonly HttpClientServiceDefaults s_defaults = new HttpClientServiceDefaults { #if NET8_0_OR_GREATER diff --git a/tools/EdgeCases/IEdgeCases.g.cs b/tools/EdgeCases/IEdgeCases.g.cs index 0ae2e73..95810d3 100644 --- a/tools/EdgeCases/IEdgeCases.g.cs +++ b/tools/EdgeCases/IEdgeCases.g.cs @@ -24,5 +24,7 @@ public partial interface IEdgeCases Task> CustomHttpAsync(CustomHttpRequestDto request, CancellationToken cancellationToken = default); Task> SnakeMethodAsync(SnakeMethodRequestDto request, CancellationToken cancellationToken = default); + + Task> MiscAsync(MiscRequestDto request, CancellationToken cancellationToken = default); } } diff --git a/tools/EdgeCases/MiscRequestDto.g.cs b/tools/EdgeCases/MiscRequestDto.g.cs new file mode 100644 index 0000000..313a7d2 --- /dev/null +++ b/tools/EdgeCases/MiscRequestDto.g.cs @@ -0,0 +1,50 @@ +// +// DO NOT EDIT: generated by fsdgencsharp +// +#nullable enable +using System; +using System.Collections.Generic; +using Facility.Core; +using Facility.Core.MessagePack; + +namespace EdgeCases +{ + /// + /// Request for Misc. + /// + [System.CodeDom.Compiler.GeneratedCode("fsdgencsharp", "")] + [MessagePack.MessagePackObject] + public sealed partial class MiscRequestDto : ServiceDto + { + /// + /// Creates an instance. + /// + public MiscRequestDto() + { + } + + [MessagePack.Key("q")] + public string? Q { get; set; } + + [Newtonsoft.Json.JsonProperty(DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Include)] + [ServiceNullableDefaultValueAttribute(typeof(ServiceNullable))] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] + [MessagePack.Key("f")] + public ServiceNullable F { get; set; } + + /// + /// The JSON serializer. + /// + protected override JsonServiceSerializer JsonSerializer => SystemTextJsonServiceSerializer.Instance; + + /// + /// Determines if two DTOs are equivalent. + /// + public override bool IsEquivalentTo(MiscRequestDto? other) + { + return other != null && + Q == other.Q && + F == other.F; + } + } +} diff --git a/tools/EdgeCases/MiscResponseDto.g.cs b/tools/EdgeCases/MiscResponseDto.g.cs new file mode 100644 index 0000000..000d7df --- /dev/null +++ b/tools/EdgeCases/MiscResponseDto.g.cs @@ -0,0 +1,39 @@ +// +// DO NOT EDIT: generated by fsdgencsharp +// +#nullable enable +using System; +using System.Collections.Generic; +using Facility.Core; +using Facility.Core.MessagePack; + +namespace EdgeCases +{ + /// + /// Response for Misc. + /// + [System.CodeDom.Compiler.GeneratedCode("fsdgencsharp", "")] + [MessagePack.MessagePackObject] + public sealed partial class MiscResponseDto : ServiceDto + { + /// + /// Creates an instance. + /// + public MiscResponseDto() + { + } + + /// + /// The JSON serializer. + /// + protected override JsonServiceSerializer JsonSerializer => SystemTextJsonServiceSerializer.Instance; + + /// + /// Determines if two DTOs are equivalent. + /// + public override bool IsEquivalentTo(MiscResponseDto? other) + { + return other != null; + } + } +}