Skip to content

Commit 5a41e4d

Browse files
Fixes issue in generating TypeSpec for responses without a body. Closes #1260 (#1264)
1 parent 305be57 commit 5a41e4d

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

DevProxy.Plugins/Generation/TypeSpecGeneratorPlugin.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -399,25 +399,32 @@ private async Task ProcessResponseAsync(Response? httpResponse, TypeSpecFile doc
399399
.ToDictionary(h => h.Name.ToCamelCase(), h => h.Value.GetType().Name)
400400
};
401401

402-
var models = await GetModelsFromStringAsync(httpResponse.BodyString, lastSegment.ToPascalCase(), httpResponse.StatusCode >= 400);
403-
if (models.Length > 0)
402+
if (httpResponse.HasBody)
404403
{
405-
foreach (var model in models)
404+
var models = await GetModelsFromStringAsync(httpResponse.BodyString, lastSegment.ToPascalCase(), httpResponse.StatusCode >= 400);
405+
if (models.Length > 0)
406406
{
407-
_ = doc.Service.Namespace.MergeModel(model);
408-
}
407+
foreach (var model in models)
408+
{
409+
_ = doc.Service.Namespace.MergeModel(model);
410+
}
409411

410-
var rootModel = models.Last();
411-
if (rootModel.IsArray)
412-
{
413-
res.BodyType = $"{rootModel.Name}[]";
414-
op.Name = await GetOperationNameAsync("list", url);
415-
}
416-
else
417-
{
418-
res.BodyType = rootModel.Name;
412+
var rootModel = models.Last();
413+
if (rootModel.IsArray)
414+
{
415+
res.BodyType = $"{rootModel.Name}[]";
416+
op.Name = await GetOperationNameAsync("list", url);
417+
}
418+
else
419+
{
420+
res.BodyType = rootModel.Name;
421+
}
419422
}
420423
}
424+
else
425+
{
426+
Logger.LogDebug("Response has no body");
427+
}
421428
}
422429

423430
op.MergeResponse(res);

0 commit comments

Comments
 (0)