Skip to content

Commit c46c819

Browse files
committed
Fixing stupid error with nullable / default value
1 parent 8cc39c8 commit c46c819

File tree

5 files changed

+104
-5
lines changed

5 files changed

+104
-5
lines changed

GoLive.Generator.ApiClientGenerator.Tests.WebApi/Controllers/NonApiController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public async Task<ActionResult> TestWithAllowedValuesButNullable(string Id, [All
3030
{
3131
return Ok();
3232
}
33+
34+
[HttpGet]
35+
public async Task<ActionResult> TestWithAllowedValuesButNullableAndDefaultValue(string Id, [AllowedValues("start", "stop", "kill", "restart")]string DesiredState = "")
36+
{
37+
return Ok();
38+
}
3339
}

GoLive.Generator.ApiClientGenerator.Tests.WebApi/GeneratedApiClient.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ private TestWithAllowedValues_DesiredState(string value)
571571
public static TestWithAllowedValues_DesiredState restart = new TestWithAllowedValues_DesiredState("restart");
572572
}
573573

574-
public async Task<Response> TestWithAllowedValues(string Id, TestWithAllowedValues_DesiredState? DesiredState = default, QueryString queryString = default, CancellationToken _token = default)
574+
public async Task<Response> TestWithAllowedValues(string Id, TestWithAllowedValues_DesiredState DesiredState = default, QueryString queryString = default, CancellationToken _token = default)
575575
{
576576
queryString = queryString.Add("DesiredState", DesiredState.Value.ToString());
577577
using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/NonApi/TestWithAllowedValues/{Id}{queryString}");
@@ -618,6 +618,40 @@ public string TestWithAllowedValuesButNullable_Url(string Id, string DesiredStat
618618
queryString = queryString.Add("DesiredState", DesiredState.ToString());
619619
return $"/api/NonApi/TestWithAllowedValuesButNullable/{Id}{queryString}";
620620
}
621+
622+
public readonly struct TestWithAllowedValuesButNullableAndDefaultValue_DesiredState
623+
{
624+
private TestWithAllowedValuesButNullableAndDefaultValue_DesiredState(string value)
625+
{
626+
Value = value;
627+
}
628+
629+
public string Value { get; }
630+
631+
public override string ToString() => $"{Value}";
632+
public static TestWithAllowedValuesButNullableAndDefaultValue_DesiredState start = new TestWithAllowedValuesButNullableAndDefaultValue_DesiredState("start");
633+
public static TestWithAllowedValuesButNullableAndDefaultValue_DesiredState stop = new TestWithAllowedValuesButNullableAndDefaultValue_DesiredState("stop");
634+
public static TestWithAllowedValuesButNullableAndDefaultValue_DesiredState kill = new TestWithAllowedValuesButNullableAndDefaultValue_DesiredState("kill");
635+
public static TestWithAllowedValuesButNullableAndDefaultValue_DesiredState restart = new TestWithAllowedValuesButNullableAndDefaultValue_DesiredState("restart");
636+
}
637+
638+
public async Task<Response> TestWithAllowedValuesButNullableAndDefaultValue(string Id, TestWithAllowedValuesButNullableAndDefaultValue_DesiredState? DesiredState = default, QueryString queryString = default, CancellationToken _token = default)
639+
{
640+
if (DesiredState.HasValue)
641+
{
642+
queryString = queryString.Add("DesiredState", DesiredState.Value.ToString());
643+
}
644+
645+
using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/NonApi/TestWithAllowedValuesButNullableAndDefaultValue/{Id}{queryString}");
646+
using var result = await _client.SendAsync(request, _token);
647+
return new Response(result.StatusCode, result.Headers);
648+
}
649+
650+
public string TestWithAllowedValuesButNullableAndDefaultValue_Url(string Id, string DesiredState = default, QueryString queryString = default)
651+
{
652+
queryString = queryString.Add("DesiredState", DesiredState.ToString());
653+
return $"/api/NonApi/TestWithAllowedValuesButNullableAndDefaultValue/{Id}{queryString}";
654+
}
621655
}
622656

623657
public class TestIssueClient

GoLive.Generator.ApiClientGenerator.Tests.WebApi/GeneratedApiClient.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,59 @@
773773
"AllAttributes": [
774774
"Microsoft.AspNetCore.Mvc.HttpGetAttribute"
775775
]
776+
},
777+
{
778+
"Name": "TestWithAllowedValuesButNullableAndDefaultValue",
779+
"FullMethodName": "GoLive.Generator.ApiClientGenerator.Tests.WebApi.Controllers.NonApiController.TestWithAllowedValuesButNullableAndDefaultValue",
780+
"Method": {
781+
"Method": "GET"
782+
},
783+
"Route": "",
784+
"RouteSetByAttributes": false,
785+
"ReturnTypeName": null,
786+
"ReturnTypeStruct": true,
787+
"HasCustomFormatter": false,
788+
"Mapping": [
789+
{
790+
"Key": "Id",
791+
"Parameter": {
792+
"FullTypeName": "string",
793+
"GenericTypeName": null,
794+
"HasDefaultValue": false,
795+
"DefaultValue": null,
796+
"Nullable": false,
797+
"Attributes": [],
798+
"SpecialType": 20,
799+
"AllowedStringValues": null
800+
}
801+
},
802+
{
803+
"Key": "DesiredState",
804+
"Parameter": {
805+
"FullTypeName": "string",
806+
"GenericTypeName": null,
807+
"HasDefaultValue": true,
808+
"DefaultValue": "",
809+
"Nullable": false,
810+
"Attributes": [
811+
"System.ComponentModel.DataAnnotations.AllowedValuesAttribute"
812+
],
813+
"SpecialType": 20,
814+
"AllowedStringValues": [
815+
"start",
816+
"stop",
817+
"kill",
818+
"restart"
819+
]
820+
}
821+
}
822+
],
823+
"Body": [],
824+
"XmlComments": "",
825+
"CalculatedURL": "/api/NonApi/TestWithAllowedValuesButNullableAndDefaultValue/{Id}",
826+
"AllAttributes": [
827+
"Microsoft.AspNetCore.Mvc.HttpGetAttribute"
828+
]
776829
}
777830
],
778831
"XmlComments": "",

GoLive.Generator.ApiClientGenerator.Tests.WebApi/GeneratedURLs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ public static string NonApi_TestWithAllowedValuesButNullable(string Id, string D
107107
return $"/api/NonApi/TestWithAllowedValuesButNullable/{Id}{queryString}";
108108
}
109109

110+
public static string NonApi_TestWithAllowedValuesButNullableAndDefaultValue(string Id, string DesiredState = default, QueryString queryString = default)
111+
{
112+
queryString = queryString.Add("DesiredState", DesiredState.ToString());
113+
return $"/api/NonApi/TestWithAllowedValuesButNullableAndDefaultValue/{Id}{queryString}";
114+
}
115+
110116
public static string TestIssue_Get(string Id = "", QueryString queryString = default)
111117
{
112118
return $"/api/TestIssue/Get/{Id}{queryString}";

GoLive.Generator.ApiClientGenerator/ApiClientGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,13 @@ private static void SetUpSingleApi(RouteGeneratorSettings config, ControllerRout
386386
StringBuilder retr = new StringBuilder();
387387
retr.Append($"{action.Name}_{m.Key}");
388388

389-
if (m.Parameter.Nullable)
389+
if (m.Parameter.Nullable || m.Parameter.HasDefaultValue)
390390
{
391-
retr.Append("?");
391+
retr.Append("? ");
392392
}
393393
else
394394
{
395-
retr.Append($"{(!m.Parameter.HasDefaultValue ? "?" : "")} ");
395+
retr.Append(" ");
396396
}
397397

398398
retr.Append($"{m.Key} ");
@@ -557,7 +557,7 @@ private static void SetUpSingleApi(RouteGeneratorSettings config, ControllerRout
557557
}
558558
else if (parameterMapping.Parameter.SpecialType == SpecialType.System_String && parameterMapping.Parameter.AllowedStringValues is { Length: > 0 } )
559559
{
560-
if (parameterMapping.Parameter.Nullable)
560+
if (parameterMapping.Parameter.Nullable || parameterMapping.Parameter.HasDefaultValue)
561561
{
562562
source.AppendLine($"if ({parameterMapping.Key}.HasValue)");
563563
}

0 commit comments

Comments
 (0)