Skip to content

fix: csharp get file name from content disposition#17183

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
fghpdf:fix-csharp-download-file-name
Nov 27, 2023
Merged

fix: csharp get file name from content disposition#17183
wing328 merged 2 commits intoOpenAPITools:masterfrom
fghpdf:fix-csharp-download-file-name

Conversation

@fghpdf
Copy link
Contributor

@fghpdf fghpdf commented Nov 25, 2023

Summary

Download a file can't get the file name from the header, content-disposition.

The generator will generate the FileParameter class, and the code doesn't handle this type.

And also, according to RFC 2616, the HTTP headers are case insensitive.

OpenAPI Example

openapi: 3.0.0
info:
  title: Swagger Petstore - OpenAPI 3.1
  description: |-
  version: 1.0.11
servers:
  - url: http://localhost:8080
tags:
  - name: file
    description: A Restful API to manage files
paths:
  /files/${fileId}:
    get:
      tags:
        - file
      description: Download an existing file by Id
      operationId: downloadFile
      parameters:  
        - in: path
          name: fileId
          schema:
            type: string
            format: uuid
          required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary

OpenAPI CLI Command

openapi-generator-cli generate -g csharp --additional-properties=library=httpclient --additional-properties=targetFramework=net6.0 -i file.yaml 

Expect

Get file name: example.txt

Actual

Get file name: 'no_name_provided'
Screenshot from 2023-11-25 21-25-56

Server

If you want to have a quick test, you can use this Golang code:

package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"path/filepath"
)

func main() {
	r := gin.Default()
	r.GET("/files/:fileId", func(c *gin.Context) {
		fileId := c.Param("fileId")
		log.Println(fileId)
		filePath := filepath.Join("./", "example.txt")
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
		c.Header("Content-Disposition", "attachment; filename=example.txt")
		c.Header("Content-Type", "application/octet-stream")
		c.File(filePath)
	})
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@fghpdf
Copy link
Contributor Author

fghpdf commented Nov 26, 2023

@wing328 I appreciate the extra details you added.

@wing328
Copy link
Member

wing328 commented Nov 27, 2023

@fghpdf thanks for the PR.

Have you tested the fix locally to confirm it works for your use cases?

@fghpdf
Copy link
Contributor Author

fghpdf commented Nov 27, 2023

@fghpdf thanks for the PR.

Have you tested the fix locally to confirm it works for your use cases?

Sure, I tried this case locally:

[Fact]
public void UpdatePetTest()
{
    FileParameter file = instance.DownloadFile(Guid.NewGuid());
    _output.WriteLine(file.Name);
    Assert.Equal("example.txt", file.Name);
}

And this PR can solve this issue.

@wing328
Copy link
Member

wing328 commented Nov 27, 2023

ok. let's give it a try

@wing328 wing328 merged commit 8669646 into OpenAPITools:master Nov 27, 2023
@wing328 wing328 changed the title fix: chsarp get file name from content disposition fix: csharp get file name from content disposition Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants