Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong swagger schema generation when response_body is set. #459

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,40 @@ private static ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint,
};
apiDescription.RelativePath = pattern.TrimStart('/');
apiDescription.SupportedRequestFormats.Add(new ApiRequestFormat { MediaType = "application/json" });

var modelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(methodDescriptor.OutputType.ClrType));
var responseBody = httpRule.ResponseBody;

if (!string.IsNullOrEmpty(responseBody))
{
var responseBodyDescriptor = ServiceDescriptorHelpers.ResolveResponseBodyDescriptor(responseBody, methodDescriptor);
if (responseBodyDescriptor.IsRepeated)
{
if (responseBodyDescriptor.IsMap)
{
var key = responseBodyDescriptor.MessageType.FindFieldByName("key");
var value = responseBodyDescriptor.MessageType.FindFieldByName("value");

var keyType = MessageDescriptorHelpers.ResolveFieldType(key);
var valueType = MessageDescriptorHelpers.ResolveFieldType(value);

modelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(typeof(Dictionary<,>).MakeGenericType(keyType, valueType)));
}
else
{
modelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(typeof(List<>).MakeGenericType(MessageDescriptorHelpers.ResolveFieldType(responseBodyDescriptor))));
}
}
else
{
modelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(MessageDescriptorHelpers.ResolveFieldType(responseBodyDescriptor)));
}
}

apiDescription.SupportedResponseTypes.Add(new ApiResponseType
{
ApiResponseFormats = { new ApiResponseFormat { MediaType = "application/json" } },
ModelMetadata = new GrpcModelMetadata(ModelMetadataIdentity.ForType(methodDescriptor.OutputType.ClrType)),
ModelMetadata = modelMetadata,
StatusCode = 200
});
var explorerSettings = routeEndpoint.Metadata.GetMetadata<ApiExplorerSettingsAttribute>();
Expand Down
12 changes: 12 additions & 0 deletions src/GrpcHttpApi/src/Shared/ServiceDescriptorHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ public static Dictionary<string, List<FieldDescriptor>> ResolveRouteParameterDes
return null;
}

public static FieldDescriptor ResolveResponseBodyDescriptor(string responseBody, MethodDescriptor methodDescriptor)
{
if (!TryResolveDescriptors(methodDescriptor.OutputType, responseBody, out var responseBodyFieldDescriptors))
{
throw new InvalidOperationException($"Couldn't find matching field for response-body '{responseBody}' on {methodDescriptor.OutputType.Name}.");
}

var fieldDescriptor = responseBodyFieldDescriptors.Last();
return fieldDescriptor;

}

public record BodyDescriptorInfo(
MessageDescriptor Descriptor,
List<FieldDescriptor>? FieldDescriptors,
Expand Down