RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.79k stars 1.29k forks source link

Generated C# client doesn't work for file upload if $ref is used instead of inline schema definition #3533

Open aureole82 opened 3 years ago

aureole82 commented 3 years ago

Here we have 3 valid OpenApi v3 specifications for a file upload:

openapi: 3.0.1
info:
  title: Doc upload
  description: Doc upload
  version: 0.0.1
paths:
  /Documentsv1:
    post:
      tags:
        - Documents1
      summary: 'Adds the document. - original'
      operationId: Documentsv1
      requestBody:
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
        required: true
      responses:
        '202':
          description: Success
  /Documentsv2:
    post:
      tags:
        - Documents2
      summary: 'Adds the document. - generated by azure api management'
      operationId: Documentsv2
      requestBody:
        content:
          application/octet-stream:
            schema:
              $ref: "#/components/schemas/AddDocumentComponent"
        required: true
      responses:
        '202':
          description: Success
  /Documentsv3:
    post:
      tags:
        - Documents3
      summary: 'Adds the document - with request body'
      operationId: Documentsv3
      requestBody:
        $ref: "#/components/requestBodies/AddDocumentBody"
      responses:
        '202':
          description: Success
components:
  requestBodies:
    AddDocumentBody:
      content:
        application/octet-stream:
          schema:
            type: string
            format: binary
      required: true
  schemas:
    AddDocumentComponent:
      type: string
      format: binary

Unfortunately only the inline schema definition of Documentsv1 lets NSwag generate a valid client:

public async Task Documentsv1Async(System.IO.Stream body, ...) {
    //..
    var content_ = new System.Net.Http.StreamContent(body);

Documentsv2 and Documentsv3 will fail on runtime as they both try to serialize the body to JSON first before passing it into the StringContent constructor:

public async Task Documentsv2Async(FileParameter body, ...) {
    //..
    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));

and

public async Task Documentsv3Async(object body, ...) {
    //..
    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));

It's like that the last 2 requests "forgot" that the schema indirectly demands for binary and not byte. Could you fix this issues or if I get something wrong help me to address to real issuer of the problem?

(I'm really sorry to tell you we need to swallow the 2nd version of file upload generation because the Azure API Management returns it that way no matter if the API behind provides inline or $ref schema types. 😥)


NSwag Studio v13.11.3.0

aureole82 commented 3 years ago

As you see in https://github.com/OAI/OpenAPI-Specification/issues/2637 the schema is correct! Would you please be so kind and support $ref schema types for file transfers?