diff --git a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.Swagger/Internal/GrpcJsonTranscodingDescriptionProvider.cs b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.Swagger/Internal/GrpcJsonTranscodingDescriptionProvider.cs index 7b2c7537b8f0..270411f73924 100644 --- a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.Swagger/Internal/GrpcJsonTranscodingDescriptionProvider.cs +++ b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.Swagger/Internal/GrpcJsonTranscodingDescriptionProvider.cs @@ -120,7 +120,7 @@ private static ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, { // If from a property, create model as property to get its XML comments. var identity = bodyDescriptor.PropertyInfo != null - ? ModelMetadataIdentity.ForProperty(bodyDescriptor.PropertyInfo, bodyDescriptor.Descriptor.ClrType, bodyDescriptor.PropertyInfo.DeclaringType!) + ? ModelMetadataIdentity.ForProperty(bodyDescriptor.PropertyInfo, bodyDescriptor.PropertyInfo.PropertyType, bodyDescriptor.PropertyInfo.DeclaringType!) : ModelMetadataIdentity.ForType(bodyDescriptor.Descriptor.ClrType); // Or if from a parameter, create model as parameter to get its XML comments. diff --git a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/BodyTests.cs b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/BodyTests.cs new file mode 100644 index 000000000000..992c40007b04 --- /dev/null +++ b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/BodyTests.cs @@ -0,0 +1,82 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.AspNetCore.Grpc.Swagger.Tests.Infrastructure; +using Microsoft.AspNetCore.Grpc.Swagger.Tests.Services; +using Microsoft.OpenApi.Models; +using Xunit.Abstractions; + +namespace Microsoft.AspNetCore.Grpc.Swagger.Tests.Binding; + +public class BodyTests +{ + private readonly ITestOutputHelper _testOutputHelper; + + public BodyTests(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = testOutputHelper; + } + + [Fact] + public void PostRepeated() + { + // Arrange & Act + var swagger = OpenApiTestHelpers.GetOpenApiDocument(_testOutputHelper); + + // Assert + var path = swagger.Paths["/v1/body1"]; + Assert.True(path.Operations.TryGetValue(OperationType.Post, out var operation)); + + var bodySchema = operation.RequestBody.Content["application/json"].Schema; + Assert.Null(bodySchema.Reference); + Assert.Equal("array", bodySchema.Type); + Assert.Equal("RequestBody", bodySchema.Items.Reference.Id); + + var messageSchema = swagger.ResolveReference(bodySchema.Items.Reference); + Assert.NotNull(messageSchema); + } + + [Fact] + public void PostMap() + { + // Arrange & Act + var swagger = OpenApiTestHelpers.GetOpenApiDocument(_testOutputHelper); + + // Assert + var path = swagger.Paths["/v1/body2"]; + Assert.True(path.Operations.TryGetValue(OperationType.Post, out var operation)); + + var bodySchema = operation.RequestBody.Content["application/json"].Schema; + Assert.Null(bodySchema.Reference); + Assert.Equal("object", bodySchema.Type); + Assert.Equal("integer", bodySchema.AdditionalProperties.Type); + } + + [Fact] + public void PostMessage() + { + // Arrange & Act + var swagger = OpenApiTestHelpers.GetOpenApiDocument(_testOutputHelper); + + // Assert + var path = swagger.Paths["/v1/body3"]; + Assert.True(path.Operations.TryGetValue(OperationType.Post, out var operation)); + + var bodySchema = operation.RequestBody.Content["application/json"].Schema; + Assert.Equal("RequestBody", bodySchema.Reference.Id); + } + + [Fact] + public void PostRoot() + { + // Arrange & Act + var swagger = OpenApiTestHelpers.GetOpenApiDocument(_testOutputHelper); + + // Assert + var path = swagger.Paths["/v1/body4"]; + Assert.True(path.Operations.TryGetValue(OperationType.Post, out var operation)); + + var bodySchema = operation.RequestBody.Content["application/json"].Schema; + Assert.Equal("RequestOne", bodySchema.Reference.Id); + } +} diff --git a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Parameters/ParametersTests.cs b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/ParametersTests.cs similarity index 99% rename from src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Parameters/ParametersTests.cs rename to src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/ParametersTests.cs index d272a6866e0f..8a4db851175f 100644 --- a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Parameters/ParametersTests.cs +++ b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/ParametersTests.cs @@ -6,7 +6,7 @@ using Microsoft.OpenApi.Models; using Xunit.Abstractions; -namespace Microsoft.AspNetCore.Grpc.Swagger.Tests.Parameters; +namespace Microsoft.AspNetCore.Grpc.Swagger.Tests.Binding; public class ParametersTests { diff --git a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Microsoft.AspNetCore.Grpc.Swagger.Tests.csproj b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Microsoft.AspNetCore.Grpc.Swagger.Tests.csproj index 72ed48b04385..41247608140b 100644 --- a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Microsoft.AspNetCore.Grpc.Swagger.Tests.csproj +++ b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Microsoft.AspNetCore.Grpc.Swagger.Tests.csproj @@ -1,4 +1,4 @@ - + $(DefaultNetCoreTargetFramework) true @@ -7,6 +7,7 @@ + diff --git a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Proto/body.proto b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Proto/body.proto new file mode 100644 index 000000000000..054b538ed271 --- /dev/null +++ b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Proto/body.proto @@ -0,0 +1,54 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +syntax = "proto3"; + +package body; + +import "google/api/annotations.proto"; + +// Add go_package to keep protoc happy when testing generating OpenAPI from commandline. +option go_package = "github.com/dotnet/aspnetcore/swagger"; + +// HttpRule: https://cloud.google.com/endpoints/docs/grpc-service-config/reference/rpc/google.api#google.api.HttpRule + +service Body { + rpc DemoBodyOne (RequestOne) returns (BodyParamResponse) { + option (google.api.http) = { + post: "/v1/body1", + body: "parameter_one" + }; + } + rpc DemoBodyTwo (RequestOne) returns (BodyParamResponse) { + option (google.api.http) = { + post: "/v1/body2", + body: "parameter_two" + }; + } + rpc DemoBodyThree (RequestOne) returns (BodyParamResponse) { + option (google.api.http) = { + post: "/v1/body3", + body: "parameter_three" + }; + } + rpc DemoBodyFour (RequestOne) returns (BodyParamResponse) { + option (google.api.http) = { + post: "/v1/body4", + body: "*" + }; + } +} + +message RequestOne { + repeated RequestBody parameter_one = 1; + map parameter_two = 2; + RequestBody parameter_three = 3; +} + +message RequestBody { + string request_body = 1; +} + +message BodyParamResponse { + string message = 1; +} diff --git a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Services/BodyService.cs b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Services/BodyService.cs new file mode 100644 index 000000000000..7f8a88e6d975 --- /dev/null +++ b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Services/BodyService.cs @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Params; + +namespace Microsoft.AspNetCore.Grpc.Swagger.Tests.Services; + +public class BodyService : Body.Body.BodyBase +{ +}