dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.22k stars 9.95k forks source link

How to properly type Microsoft.AspNetCore.Mvc.ProblemDetails in a typed client? #17579

Closed mattiasw2 closed 4 years ago

mattiasw2 commented 4 years ago

Is your feature request related to a problem? Please describe.

I use aspnet core 3.0, and I am generating the client using openapi-generator for Elm. The problem is that Elm is a typed programming language, so the extensions field below in the swagger.json cannot be handled by Elm. If TypeScript takes types seriously, it should have the same problem.

  "ProblemDetails": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "type": {
        "type": "string",
        "nullable": true
      },
      "title": {
        "type": "string",
        "nullable": true
      },
      "status": {
        "type": "integer",
        "format": "int32",
        "nullable": true
      },
      "detail": {
        "type": "string",
        "nullable": true
      },
      "instance": {
        "type": "string",
        "nullable": true
      },
      "extensions": {
        "type": "object",
        "nullable": true,
        "additionalProperties": {}
      }
    }
  },

I am also wondering about the type, it just says object, but according to https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.problemdetails.extensions?view=aspnetcore-3.0 it should be

 public System.Collections.Generic.IDictionary<string,object> Extensions { get; }

Why the mismatch? However this would not help me, since there is still a generic object in the type.

Describe the solution you'd like

Currently, I just delete the extensions field in swagger.json, and then generate the api. So even if the server sends it, it will just be thrown away by the client code.

However, a more type-friendly solution would be appreciated.

mattiasw2 commented 4 years ago

The swagger.json is generated by

   "x-generator": "NSwag v13.1.6.0 (NJsonSchema v10.0.28.0 (Newtonsoft.Json v12.0.0.0))",
mattiasw2 commented 4 years ago

After reading the https://tools.ietf.org/html/rfc7807 , I get the impression that the extensions field shouldn't even be part of the the type in the swagger.json. When a ProblemDetails object is converted to json, all pairs in extensions should be lifted to the top level.

mkArtakMSFT commented 4 years ago

Hi. Thanks for contacting us. We're closing this issue as there was not much community interest in this ask for quite a while now.