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

File API #131

Merged
merged 12 commits into from
Nov 10, 2019
Merged

File API #131

merged 12 commits into from
Nov 10, 2019

Conversation

sergey-tihon
Copy link
Member

@sergey-tihon sergey-tihon commented Nov 8, 2019

reworked version of #108 with desire to fix #59 and #61

Related links

@sergey-tihon
Copy link
Member Author

Related OpenApi schema part

"/api/ReturnFile": {
      "get": {
        "tags": [
          "ReturnFile"
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          }
        }
      }
    },
    "/api/ReturnFile/single": {
      "post": {
        "tags": [
          "ReturnFile"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "file": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          }
        }
      }
    },
    "/api/ReturnFile/multiple": {
      "post": {
        "tags": [
          "ReturnFile"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "files": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "binary"
                    }
                  }
                }
              },
              "encoding": {
                "files": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "integer",
                  "format": "int32"
                }
              }
            }
          }
        }
      }
    },
    "/api/ReturnFile/form-with-file": {
      "post": {
        "tags": [
          "ReturnFile"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "Name": {
                    "type": "string"
                  },
                  "File": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "Name": {
                  "style": "form"
                },
                "File": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          }
        }
      }
    },

@sergey-tihon sergey-tihon marked this pull request as ready for review November 10, 2019 10:01
@sergey-tihon
Copy link
Member Author

sergey-tihon commented Nov 10, 2019

@baronfel May I ask you to quickly take a look at this PR?

Does it cover everything you did in #108?
It looks good enough to be merged as File support.

Comment on lines +28 to +29
let data = WebAPI.OperationTypes.PostApiReturnFileSingle_formData(toStream text)
let! stream = api.PostApiReturnFileSingle(data)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not supper happy that we generate wrapper object around payload ...
but in OpenAPI payload described as one parameter requestBody that we map to provided type

    "/api/ReturnFile/single": {
      "post": {
        "tags": [
          "ReturnFile"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "file": {
                  "style": "form"
                }
              }
            }
          }
        },

Comment on lines +93 to +94
let filename = Guid.NewGuid().ToString() // asp.net core cannot deserialize IFormFile otherwise
cnt.Add(new StreamContent(stream), name, filename)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another hack is that we have to provide file have for content disposition header, otherwise asp.net core backend cannot map it to IFormFile, but this is not reflected in OpenAPI schema :'(

"requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "file": {
                  "style": "form"
                }
              }
            }
          }
        },

@sergey-tihon sergey-tihon merged commit 613f94b into master Nov 10, 2019
@sergey-tihon
Copy link
Member Author

I merge it, because it definitely better than nothing - we can improve in the future.

@baronfel
Copy link
Contributor

@sergey-tihon this looks perfectly fine to me, thanks for carrying the torch here and adding this functionality :) As for your comment about needing the wrapper-type, that seems like something that can be added later as an additional overload if/when there's time. So then you'd have one overload with the wrapper-type (which you could mark as obsolete), and another overload with just the input stream (which I guess seems like the 'ideal' interface to me?)

@sergey-tihon sergey-tihon added this to the v0.10.0 milestone Nov 10, 2019
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

Successfully merging this pull request may close these issues.

FormData parameters of type 'file' get sent as 'System.Byte[]' instead of the buffer contents.
2 participants