Nswag: OpenAPI Files Not Handled Correctly

Created on 15 Apr 2019  路  3Comments  路  Source: RicoSuter/NSwag

Hi,

I'm seeing a problem with NSwag (12.1.0) where file uploads are not handled correctly. Take the following OpenAPI schema:

openapi: 3.0.0
servers:
  - url: https://www.example.com/
info:
  version: '2.0.0'
  title: 'Test API'   
################################################################
#
# PATHS
#
################################################################  
paths:
  /files:
    post:
      tags:
        - Files
      summary: 'Add File'
      operationId: addFile
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FileToken'
      requestBody:
        content:
          image/png:
            schema:
              type: string
              format: binary
components:
  schemas:
    FileToken:
      type: object
      required:
        - fileId    
      properties:  
        fileId:
          type: string
          format: uuid

Take the following generation code:

var document = await SwaggerYamlDocument.FromFileAsync("openapi.yaml");

var settings = new SwaggerToCSharpClientGeneratorSettings();

var generator = new SwaggerToCSharpClientGenerator(document, settings);

var output = generator.GenerateFile(ClientGeneratorOutputType.Full);

await File.WriteAllTextAsync("Output.cs", output);

Output.cs contains the following code:

public async System.Threading.Tasks.Task<FileToken> AddFileAsync(FileParameter body, System.Threading.CancellationToken cancellationToken)
{
    var urlBuilder_ = new System.Text.StringBuilder();
    urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/files");

    var client_ = _httpClient;
    try
    {
        using (var request_ = new System.Net.Http.HttpRequestMessage())
        {
            var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));
            content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");

So for whatever reason it is converting the image/png request body and treating it as JSON.

ConsoleApp8.zip

Most helpful comment

Created a PR, does the output look fine with it?

All 3 comments

Created a PR, does the output look fine with it?

@RicoSuter Hi, I believe this fixes it, thank you for the super quick response! I pulled in the updated NSwag from git as project references, this is now the output from my test code above:

public async System.Threading.Tasks.Task<FileToken> AddFileAsync(System.IO.Stream body, System.Threading.CancellationToken cancellationToken)
{
    var urlBuilder_ = new System.Text.StringBuilder();
    urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/files");

    var client_ = _httpClient;
    try
    {
        using (var request_ = new System.Net.Http.HttpRequestMessage())
        {
            var content_ = new System.Net.Http.StreamContent(body);
            content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("image/png");

Need to do some more checks but I think this PR is fine and can be merged...

Was this page helpful?
0 / 5 - 0 ratings