Skip to content

Commit

Permalink
gRPC JSON transcoding + Microsoft.AspNetCore.OpenApi
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jun 4, 2024
1 parent 83573c7 commit 0a019a1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
19 changes: 19 additions & 0 deletions AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetDocumentInsider.Tests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetDocumentSample", "src\Tools\GetDocumentInsider\sample\GetDocumentSample.csproj", "{D8F7091E-A2D1-4E81-BA7C-97EAE392D683}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Grpc.OpenApi", "src\Grpc\JsonTranscoding\src\Microsoft.AspNetCore.Grpc.OpenApi\Microsoft.AspNetCore.Grpc.OpenApi.csproj", "{DE901A71-3EBD-4D25-B59F-F47C3F149361}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -10977,6 +10979,22 @@ Global
{D8F7091E-A2D1-4E81-BA7C-97EAE392D683}.Release|x64.Build.0 = Release|Any CPU
{D8F7091E-A2D1-4E81-BA7C-97EAE392D683}.Release|x86.ActiveCfg = Release|Any CPU
{D8F7091E-A2D1-4E81-BA7C-97EAE392D683}.Release|x86.Build.0 = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|arm64.ActiveCfg = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|arm64.Build.0 = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|x64.ActiveCfg = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|x64.Build.0 = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|x86.ActiveCfg = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Debug|x86.Build.0 = Debug|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|Any CPU.Build.0 = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|arm64.ActiveCfg = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|arm64.Build.0 = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|x64.ActiveCfg = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|x64.Build.0 = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|x86.ActiveCfg = Release|Any CPU
{DE901A71-3EBD-4D25-B59F-F47C3F149361}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -11874,6 +11892,7 @@ Global
{9536C284-65B4-4884-BB50-06D629095C3E} = {274100A5-5B2D-4EA2-AC42-A62257FC6BDC}
{6A19D94D-2BC6-4198-BE2E-342688FDBA4B} = {A1B75FC7-A777-4412-A635-D0C9ED8FE7A0}
{D8F7091E-A2D1-4E81-BA7C-97EAE392D683} = {A1B75FC7-A777-4412-A635-D0C9ED8FE7A0}
{DE901A71-3EBD-4D25-B59F-F47C3F149361} = {151E6F9E-107B-4DDC-A2B1-95115801FD14}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}
Expand Down
16 changes: 16 additions & 0 deletions src/Grpc/Grpc.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"src\\Grpc\\Interop\\test\\testassets\\InteropWebsite\\InteropWebsite.csproj",
"src\\Grpc\\JsonTranscoding\\perf\\Microsoft.AspNetCore.Grpc.Microbenchmarks\\Microsoft.AspNetCore.Grpc.Microbenchmarks.csproj",
"src\\Grpc\\JsonTranscoding\\src\\Microsoft.AspNetCore.Grpc.JsonTranscoding\\Microsoft.AspNetCore.Grpc.JsonTranscoding.csproj",
"src\\Grpc\\JsonTranscoding\\src\\Microsoft.AspNetCore.Grpc.OpenApi\\Microsoft.AspNetCore.Grpc.OpenApi.csproj",
"src\\Grpc\\JsonTranscoding\\src\\Microsoft.AspNetCore.Grpc.Swagger\\Microsoft.AspNetCore.Grpc.Swagger.csproj",
"src\\Grpc\\JsonTranscoding\\test\\Microsoft.AspNetCore.Grpc.JsonTranscoding.IntegrationTests\\Microsoft.AspNetCore.Grpc.JsonTranscoding.IntegrationTests.csproj",
"src\\Grpc\\JsonTranscoding\\test\\Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests\\Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests.csproj",
Expand All @@ -18,13 +19,28 @@
"src\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
"src\\Hosting\\Server.Abstractions\\src\\Microsoft.AspNetCore.Hosting.Server.Abstractions.csproj",
"src\\Hosting\\TestHost\\src\\Microsoft.AspNetCore.TestHost.csproj",
"src\\Http\\Authentication.Abstractions\\src\\Microsoft.AspNetCore.Authentication.Abstractions.csproj",
"src\\Http\\Authentication.Core\\src\\Microsoft.AspNetCore.Authentication.Core.csproj",
"src\\Http\\Headers\\src\\Microsoft.Net.Http.Headers.csproj",
"src\\Http\\Http.Abstractions\\src\\Microsoft.AspNetCore.Http.Abstractions.csproj",
"src\\Http\\Http.Extensions\\gen\\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj",
"src\\Http\\Http.Extensions\\src\\Microsoft.AspNetCore.Http.Extensions.csproj",
"src\\Http\\Http.Features\\src\\Microsoft.AspNetCore.Http.Features.csproj",
"src\\Http\\Http.Results\\src\\Microsoft.AspNetCore.Http.Results.csproj",
"src\\Http\\Http\\src\\Microsoft.AspNetCore.Http.csproj",
"src\\Http\\Metadata\\src\\Microsoft.AspNetCore.Metadata.csproj",
"src\\Http\\Routing.Abstractions\\src\\Microsoft.AspNetCore.Routing.Abstractions.csproj",
"src\\Http\\Routing\\src\\Microsoft.AspNetCore.Routing.csproj",
"src\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj",
"src\\Middleware\\OutputCaching\\src\\Microsoft.AspNetCore.OutputCaching.csproj",
"src\\Middleware\\ResponseCaching.Abstractions\\src\\Microsoft.AspNetCore.ResponseCaching.Abstractions.csproj",
"src\\Mvc\\Mvc.Abstractions\\src\\Microsoft.AspNetCore.Mvc.Abstractions.csproj",
"src\\Mvc\\Mvc.ApiExplorer\\src\\Microsoft.AspNetCore.Mvc.ApiExplorer.csproj",
"src\\Mvc\\Mvc.Core\\src\\Microsoft.AspNetCore.Mvc.Core.csproj",
"src\\ObjectPool\\src\\Microsoft.Extensions.ObjectPool.csproj",
"src\\OpenApi\\src\\Microsoft.AspNetCore.OpenApi.csproj",
"src\\Security\\Authorization\\Core\\src\\Microsoft.AspNetCore.Authorization.csproj",
"src\\Security\\Authorization\\Policy\\src\\Microsoft.AspNetCore.Authorization.Policy.csproj",
"src\\Servers\\Connections.Abstractions\\src\\Microsoft.AspNetCore.Connections.Abstractions.csproj",
"src\\Testing\\src\\Microsoft.AspNetCore.InternalTesting.csproj"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,56 @@ private static ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint,
var field = routeParameter.Value.DescriptorsPath.Last();
var parameterName = ServiceDescriptorHelpers.FormatUnderscoreName(field.Name, pascalCase: true, preservePeriod: false);
var propertyInfo = field.ContainingType.ClrType.GetProperty(parameterName);
var fieldType = MessageDescriptorHelpers.ResolveFieldType(field);

// If from a property, create model as property to get its XML comments.
var identity = propertyInfo != null
? ModelMetadataIdentity.ForProperty(propertyInfo, MessageDescriptorHelpers.ResolveFieldType(field), field.ContainingType.ClrType)
: ModelMetadataIdentity.ForType(MessageDescriptorHelpers.ResolveFieldType(field));
? ModelMetadataIdentity.ForProperty(propertyInfo, fieldType, field.ContainingType.ClrType)
: ModelMetadataIdentity.ForType(fieldType);

apiDescription.ParameterDescriptions.Add(new ApiParameterDescription
{
Name = routeParameter.Value.JsonPath,
ModelMetadata = new GrpcModelMetadata(identity),
Source = BindingSource.Path,
DefaultValue = string.Empty
DefaultValue = string.Empty,
Type = fieldType
});
}

var bodyDescriptor = ServiceDescriptorHelpers.ResolveBodyDescriptor(httpRule.Body, methodMetadata.ServiceType, methodDescriptor);
if (bodyDescriptor != null)
{
// If from a property, create model as property to get its XML comments.
var identity = bodyDescriptor.PropertyInfo != null
? ModelMetadataIdentity.ForProperty(bodyDescriptor.PropertyInfo, bodyDescriptor.PropertyInfo.PropertyType, bodyDescriptor.PropertyInfo.DeclaringType!)
: ModelMetadataIdentity.ForType(bodyDescriptor.Descriptor.ClrType);
ModelMetadataIdentity identity;
Type type;
ControllerParameterDescriptor? parameterDescriptor = null;

// Or if from a parameter, create model as parameter to get its XML comments.
var parameterDescriptor = bodyDescriptor.ParameterInfo != null
? new ControllerParameterDescriptor { ParameterInfo = bodyDescriptor.ParameterInfo }
: null;
if (bodyDescriptor.PropertyInfo != null)
{
// If from a property, create model as property to get its XML comments.
identity = ModelMetadataIdentity.ForProperty(bodyDescriptor.PropertyInfo, bodyDescriptor.PropertyInfo.PropertyType, bodyDescriptor.PropertyInfo.DeclaringType!);
type = bodyDescriptor.PropertyInfo.PropertyType;
}
else if (bodyDescriptor.ParameterInfo != null)
{
// Or if from a parameter, create model as parameter to get its XML comments.
identity = ModelMetadataIdentity.ForType(bodyDescriptor.Descriptor.ClrType);
type = bodyDescriptor.Descriptor.ClrType;
parameterDescriptor = new ControllerParameterDescriptor { ParameterInfo = bodyDescriptor.ParameterInfo };
}
else
{
identity = ModelMetadataIdentity.ForType(bodyDescriptor.Descriptor.ClrType);
type = bodyDescriptor.Descriptor.ClrType;
}

apiDescription.ParameterDescriptions.Add(new ApiParameterDescription
{
Name = "Input",
ModelMetadata = new GrpcModelMetadata(identity),
Source = BindingSource.Body,
ParameterDescriptor = parameterDescriptor!
ParameterDescriptor = parameterDescriptor!,
Type = type
});
}

Expand All @@ -148,18 +164,20 @@ private static ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint,
{
var field = queryDescription.Value;
var propertyInfo = field.ContainingType.ClrType.GetProperty(field.PropertyName);
var fieldType = MessageDescriptorHelpers.ResolveFieldType(field);

// If from a property, create model as property to get its XML comments.
var identity = propertyInfo != null
? ModelMetadataIdentity.ForProperty(propertyInfo, MessageDescriptorHelpers.ResolveFieldType(field), field.ContainingType.ClrType)
: ModelMetadataIdentity.ForType(MessageDescriptorHelpers.ResolveFieldType(field));
? ModelMetadataIdentity.ForProperty(propertyInfo, fieldType, field.ContainingType.ClrType)
: ModelMetadataIdentity.ForType(fieldType);

apiDescription.ParameterDescriptions.Add(new ApiParameterDescription
{
Name = queryDescription.Key,
ModelMetadata = new GrpcModelMetadata(identity),
Source = BindingSource.Query,
DefaultValue = string.Empty
DefaultValue = string.Empty,
Type = fieldType
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<Reference Include="Grpc.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.Grpc.JsonTranscoding" />
<Reference Include="Microsoft.AspNetCore.Grpc.Swagger" />
<Reference Include="Microsoft.AspNetCore.OpenApi" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Grpc/JsonTranscoding/test/testassets/Sandbox/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddGrpc().AddJsonTranscoding();
services.AddMvc();

services.AddOpenApi();

#region Secret
services.AddSwaggerGen(c =>
{
Expand Down Expand Up @@ -40,6 +42,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
endpoints.MapOpenApi();
endpoints.MapGrpcService<JsonTranscodingGreeterService>();
endpoints.MapGrpcService<GreeterService>();
});
Expand Down

0 comments on commit 0a019a1

Please sign in to comment.