Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aureole82 opened this issue Jul 1, 2021 · 1 comment

Comments

@aureole82
Copy link

aureole82 commented Jul 1, 2021

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 aureole82 changed the title C# client generation doesn't work for file if $ref is used instead of inline schema definition C# generated client doesn't work for file upload if $ref is used instead of inline schema definition Jul 1, 2021
@aureole82 aureole82 changed the title C# generated client doesn't work for file upload if $ref is used instead of inline schema definition Generated C# client doesn't work for file upload if $ref is used instead of inline schema definition Jul 1, 2021
@aureole82
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant