forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gRPC JSON transcoding: Fix request body OpenAPI (dotnet#47513)
- Loading branch information
Showing
6 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Binding/BodyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BodyService>(_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<BodyService>(_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<BodyService>(_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<BodyService>(_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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Proto/body.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int32, int32> parameter_two = 2; | ||
RequestBody parameter_three = 3; | ||
} | ||
|
||
message RequestBody { | ||
string request_body = 1; | ||
} | ||
|
||
message BodyParamResponse { | ||
string message = 1; | ||
} |
10 changes: 10 additions & 0 deletions
10
...Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.Swagger.Tests/Services/BodyService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
} |