grpc-dotnet icon indicating copy to clipboard operation
grpc-dotnet copied to clipboard

JSON/REST transcoding

Open cwe1ss opened this issue 6 years ago • 168 comments

Hi, first, thanks for all the work you put into this library! I'm really looking forward to using it!!

Are there any plans to support the REST-like HTTP/JSON transcoding (https://cloud.google.com/endpoints/docs/grpc/transcoding ) via the http service annotations?

Having that together with OpenAPI/Swagger is a HUGE help during development when you can just call the API via JSON objects.


2020/01/14 UPDATE: https://github.com/grpc/grpc-dotnet/issues/167#issuecomment-574005197


2020/04/24 UPDATE:


2020/12/04 UPDATE:


2022/02/16 UPDATE:

Thanks for all the interest in this feature. I'm happy to say that the .NET team is investing in JSON/REST transcoding. The aim is to publish this feature along with .NET 7.

Details:

  • .NET 7 or later will be required
  • Will continue to be a NuGet package
  • I estimate a preview will ship in .NET 7 preview 3 or preview 4.
  • A final version will ship with .NET 7 in November.

Development work is tracked at [EPIC] gRPC JSON transcoding.

cwe1ss avatar Mar 17 '19 06:03 cwe1ss

No plans to do it for 3.0. Beyond 3.0, it depends on how many people ask for it.

JamesNK avatar Mar 17 '19 16:03 JamesNK

We're keeping this issue open to solicit feedback and see how much demand there exists for this feature.

JunTaoLuo avatar Mar 19 '19 17:03 JunTaoLuo

FTR, regardless of whether this gets implemented or not, there are multiple proxies that currently offer this functionality: https://github.com/grpc-ecosystem/grpc-gateway https://www.envoyproxy.io/docs/envoy/latest/configuration/http_filters/grpc_json_transcoder_filter

jtattermusch avatar Mar 22 '19 16:03 jtattermusch

We need HTTP/JSON to gRPC transcoding so that same plumbing/service can be used to support REST and gRPC clients

govis avatar Sep 18 '19 15:09 govis

This would be super helpful to be provided out of the box. As opposed to setting up grpc gateway

raz-canva avatar Sep 24 '19 02:09 raz-canva

Also interested. We use grpc gateway in golang but need the same functionality in dotnet as we are a big .net consumer.

Jonathan34 avatar Oct 07 '19 13:10 Jonathan34

Also interested in this. While there are 3rd party tools that can do this, a simpler implementation is always better.

bobrot avatar Oct 23 '19 19:10 bobrot

if i had to start again, I'd probably use an envoy proxy instead similar to that: https://blog.envoyproxy.io/envoy-and-grpc-web-a-fresh-new-alternative-to-rest-6504ce7eb880 so at least I can have the same solution and transcoding for all the languages

Jonathan34 avatar Oct 23 '19 20:10 Jonathan34

That would be very cool since would allow smooth transition from JSON API to GRPC

VladimirLuzhin avatar Jan 13 '20 08:01 VladimirLuzhin

We're ru

Bob9098-source avatar Jan 13 '20 08:01 Bob9098-source

We have an experimental project that allows JSON/REST with gRPC here: https://github.com/aspnet/AspLabs/tree/master/src/GrpcHttpApi

The README.md at the link has details about how to use it and its status as a project.

JamesNK avatar Jan 14 '20 05:01 JamesNK

Looking forward to this feature.

ElderJames avatar Jan 21 '20 07:01 ElderJames

Great job! I think this is a must have for dotnet core framework and tooling. Why proxy gRPC calls when you don't have to. Much more elegant IMO.

dashkan avatar Feb 20 '20 16:02 dashkan

This is very helpful. Should it be possible to use Swagger (for example Swashbuckle) with this setup? For now it seems it cannot find any operations.

TomDeVree avatar Mar 01 '20 15:03 TomDeVree

There is currently no way to generate Swagger. I think a Swashbuckle extension would need to be created that lets Swashbuckle understand gRPC endpoints and protobuf schemas.

JamesNK avatar Mar 30 '20 20:03 JamesNK

Thoughts on support for Azure Functions?

cc @jeffhollan

MedAnd avatar Apr 01 '20 12:04 MedAnd

impressive. I hope to find these functionalities in production soon

Otto404 avatar Apr 15 '20 16:04 Otto404

Great project. I think GrpcHttpApi should be part of dotnet 5. Waiting for AuthContext implementation... ;-)

kollerdroid avatar Apr 21 '20 07:04 kollerdroid

The gRPC HTTP API is a great solution - almost every .net application that is adding on gRPC will benefit in maintaining existing external REST interfaces via this solution. We tried it within our team and it works seamlessly on .net core 3.0. However our application is being implemented using .net core 3.1 - any plans on moving the gRPC HTTP API forward?

Dchoksi-brierley avatar Jun 04 '20 18:06 Dchoksi-brierley

Does it not work on 3.1?

JamesNK avatar Jun 04 '20 19:06 JamesNK

Hi there, I followed the instructions from https://www.nuget.org/packages/Microsoft.AspNetCore.Grpc.HttpApi... Installed the pre-release version of Microsoft.AspNetCore.Grpc.HttpApi and Microsoft.AspNetCore.Grpc.Swagger (0.1.0-alpha.20254.1) from NuGet official library. However, when opening swagger UI it says "No operations defined in spec!" and the endpoints are not reachable through HTTP (getting 404)... I'm using .NET Core 3.1, I think a similar issue was already reported here: https://github.com/grpc/grpc-dotnet/issues/745. Do you know if Grpc.Swagger works for 3.1?

This is the Startup.cs

using GrpcGateway.Server; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models;

namespace GrpcGateway { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpcHttpApi();

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "gRPC HTTP API Example", Version = "v1" });
        });
        services.AddGrpcSwagger();

        //services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();
        app.UseDefaultFiles();
        app.UseStaticFiles();

        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "gRPC HTTP API Example V1");
        });

        app.UseRouting();

        //app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGrpcService<GreeterService>();
        });
    }
}

}

Thanks for the details Alejandro.. James - please let us know if you have any info for us or need any details about the issue. Thanks so much for responding so quickly - much appreciated!

Dchoksi-brierley avatar Jun 04 '20 20:06 Dchoksi-brierley

It should work in 3.0. I haven't got time to debug this, but feel free to copy the source code for those packages to your machine and step through the code to find out why you get that message.

JamesNK avatar Jun 05 '20 10:06 JamesNK

Thanks again for responding back. We'll debug into the source code - fingers crossed. We were not sure if there were known issues with 3.1 ("Beyond 3.0, it depends on how many people ask for it")... In any case, we'll share what we find - in case it helps any one else.

Dchoksi-brierley avatar Jun 05 '20 15:06 Dchoksi-brierley

Works fine in 3.1 - https://github.com/aspnet/AspLabs/tree/master/src/GrpcHttpApi/sample

JamesNK avatar Jun 06 '20 00:06 JamesNK

Many thanks!! We were able to make progress - adding a reference to Grpc.AspNetCore.Server and Grpc.Tools made the difference. Those two packages were not required to generate the server stubs in the latest version of Grpc.AspNetCore but looks like they were needed for Microsoft.AspNetCore.Grpc.HttpApi.

Dchoksi-brierley avatar Jun 06 '20 04:06 Dchoksi-brierley

It works in 3.1. In case this helps someone else. In my case the issue was in the proto files, the Proto folder needs to be placed in the solution's root folder and create a service reference in the project using the main .proto file (the one who imports annotations.proto) marking it to generate the server class. I was placing those files in the project folder and generating the server stub directly from it (without the service reference), in that way it doesn't work.

Has Enum type handling already implemented? I have a field which is completely missing from JSON result.

kollerdroid avatar Jun 16 '20 15:06 kollerdroid

It should work.

Can you post your proto file?

JamesNK avatar Jun 16 '20 20:06 JamesNK

Sure. Thank You.

Using enum, bookDefinition/status is missing:

{
  "id": "1",
  "isbn": "98765",
  "author": "Author",
  "description": "Description",
  "isActive": true,
  "bookGroups": [
    "1",
    "2"
  ],
  "bookDefinition": {
    "id": "1",
    "content": "A longer text...",
    "version": 1
  },
  "readerPermissions": [
    "15",
    "23"
  ],
  "availableVersions": [
    1
  ]
}

Using int32 or string, bookDefinition/status exists:

{
  "id": "1",
  "isbn": "98765",
  "author": "Author",
  "description": "Description",
  "isActive": true,
  "bookGroups": [
    "1",
    "2"
  ],
  "bookDefinition": {
    "id": "1",
    "content": "A longer text...",
    "version": 1,
    "status": 2
  },
  "readerPermissions": [
    "15",
    "23"
  ],
  "availableVersions": [
    1
  ]
}

kollerdroid avatar Jun 17 '20 06:06 kollerdroid