Skip to content

Commit f6dae14

Browse files
committed
Only check result.json when expecting response body
The test in the previous commit succeeds after this change.
1 parent e54edb0 commit f6dae14

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -271,39 +271,50 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service)
271271
code.WriteLine("const status = result.response.status;");
272272
var responseValueType = $"I{capMethodName}Response";
273273
code.WriteLine("let value" + IfTypeScript($": {responseValueType} | null") + " = null;");
274-
using (code.Block("if (result.json) {", "}"))
274+
var validResponses = httpMethodInfo.ValidResponses;
275+
var elsePrefix = "";
276+
foreach (var validResponse in validResponses)
275277
{
276-
var validResponses = httpMethodInfo.ValidResponses;
277-
var elsePrefix = "";
278-
foreach (var validResponse in validResponses)
279-
{
280-
var statusCodeAsString = ((int) validResponse.StatusCode).ToString(CultureInfo.InvariantCulture);
281-
code.WriteLine($"{elsePrefix}if (status === {statusCodeAsString}) {{");
282-
elsePrefix = "else ";
278+
var statusCodeAsString = ((int) validResponse.StatusCode).ToString(CultureInfo.InvariantCulture);
279+
code.WriteLine($"{elsePrefix}if (status === {statusCodeAsString}) {{");
280+
elsePrefix = "else ";
283281

284-
using (code.Indent())
282+
using (code.Indent())
283+
{
284+
var bodyField = validResponse.BodyField;
285+
if (bodyField != null)
285286
{
286-
var bodyField = validResponse.BodyField;
287-
if (bodyField != null)
288-
{
289-
var responseBodyFieldName = bodyField.ServiceField.Name;
287+
var responseBodyFieldName = bodyField.ServiceField.Name;
290288

291-
var bodyFieldType = service.GetFieldType(bodyField.ServiceField)!;
292-
if (bodyFieldType.Kind == ServiceTypeKind.Boolean)
293-
code.WriteLine($"value = {{ {responseBodyFieldName}: true }};");
294-
else
289+
var bodyFieldType = service.GetFieldType(bodyField.ServiceField)!;
290+
if (bodyFieldType.Kind == ServiceTypeKind.Boolean)
291+
{
292+
code.WriteLine($"value = {{ {responseBodyFieldName}: true }};");
293+
}
294+
else
295+
{
296+
using (code.Block("if (result.json) {", "}"))
297+
{
295298
code.WriteLine($"value = {{ {responseBodyFieldName}: result.json }}" + IfTypeScript($" as {responseValueType}") + ";");
299+
}
300+
}
301+
}
302+
else
303+
{
304+
if (validResponse.NormalFields!.Count == 0)
305+
{
306+
code.WriteLine("value = {};");
296307
}
297308
else
298309
{
299-
if (validResponse.NormalFields!.Count == 0)
300-
code.WriteLine("value = {};");
301-
else
310+
using (code.Block("if (result.json) {", "}"))
311+
{
302312
code.WriteLine("value = result.json" + IfTypeScript($" as {responseValueType} | null") + ";");
313+
}
303314
}
304315
}
305-
code.WriteLine("}");
306316
}
317+
code.WriteLine("}");
307318
}
308319

309320
using (code.Block("if (!value) {", "}"))

tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public void GenerateExampleApiTypeScript_DoesntRequireJsonWhenNoResponseBodyExpe
114114
// `createWidget` does expect response body
115115
const string expectedCreateWidgetLines = @"
116116
let value: ICreateWidgetResponse | null = null;
117-
if (result.json) {
118-
if (status === 201) {
117+
if (status === 201) {
118+
if (result.json) {
119119
value = { widget: result.json } as ICreateWidgetResponse;
120120
}
121121
}";

0 commit comments

Comments
 (0)