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

Array and files swagger #928

Open
kinton opened this issue May 20, 2024 · 2 comments
Open

Array and files swagger #928

kinton opened this issue May 20, 2024 · 2 comments

Comments

@kinton
Copy link

kinton commented May 20, 2024

gem "apipie-rails", "~> 1.3"

Config

api! "Create a new post (multipart/form-data)"
  param :post, Hash, required: true, action_aware: true do
    param :title, String, desc: 'Title', required: true
    param :body, String, desc: 'Body', required: true
    param :region_id, :number, desc: 'Region ID', required: false, default_value: nil
    param :files, Array, of: ActionDispatch::Http::UploadedFile,
          desc: 'Array of files to be attached to the post (multipart/form-data array)',
          required: false, default_value: []
    param :images, Array, of: ActionDispatch::Http::UploadedFile,
          desc: 'Array of images to be attached to the post (multipart/form-data array)',
          required: false, default_value: []
  end

Have a look at files and images, they are wrong. Problem: it generates string fields and doesn't send them like value[] which is not recognized by rails server.

{
            "name": "post[files]",
            "type": "array",
            "items": {
              "type": "string"
            },
            "in": "formData",
            "default": [

            ],
            "description": "Array of files to be attached to the post (multipart/form-data array)"
          },
          {
            "name": "post[images]",
            "type": "array",
            "items": {
              "type": "string"
            },
            "in": "formData",
            "default": [

            ],
            "description": "Array of images to be attached to the post (multipart/form-data array)"
          }
Full output

"post": {
        "tags": [
          "posts"
        ],
        "consumes": [
          "application/x-www-form-urlencoded",
          "multipart/form-data"
        ],
        "operationId": "post_posts",
        "summary": "Create a new post (multipart/form-data)",
        "parameters": [
          {
            "name": "post[title]",
            "type": "string",
            "in": "formData",
            "required": true,
            "description": "Title"
          },
          {
            "name": "post[body]",
            "type": "string",
            "in": "formData",
            "required": true,
            "description": "Body"
          },
          {
            "name": "post[region_id]",
            "type": "number",
            "in": "formData",
            "default": null,
            "description": "Region ID"
          },
          {
            "name": "post[files]",
            "type": "array",
            "items": {
              "type": "string"
            },
            "in": "formData",
            "default": [

            ],
            "description": "Array of files to be attached to the post (multipart/form-data array)"
          },
          {
            "name": "post[images]",
            "type": "array",
            "items": {
              "type": "string"
            },
            "in": "formData",
            "default": [

            ],
            "description": "Array of images to be attached to the post (multipart/form-data array)"
          }
        ],
        "responses": {
          "200": {
            "description": "All posts for user with region",
            "schema": {
              "type": "object",
              "properties": {
                "status": {
                  "type": "object",
                  "properties": {
                    "code": {
                      "type": "number",
                      "required": true
                    },
                    "message": {
                      "type": "string",
                      "required": true
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "message"
                  ]
                },
                "data": {
                  "type": "object",
                  "properties": {
                    "post": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string",
                          "required": true
                        },
                        "title": {
                          "type": "string",
                          "required": true
                        },
                        "body": {
                          "type": "string",
                          "required": true
                        },
                        "files": {
                          "items": {
                            "type": "string"
                          },
                          "type": "array",
                          "required": true
                        },
                        "images": {
                          "items": {
                            "type": "string"
                          },
                          "type": "array",
                          "required": true
                        }
                      },
                      "additionalProperties": false,
                      "required": [
                        "id",
                        "title",
                        "body",
                        "files",
                        "images"
                      ]
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "post"
                  ]
                }
              },
              "additionalProperties": false,
              "required": [
                "status",
                "data"
              ]
            }
          },
          "422": {
            "description": "Unprocessable entity",
            "schema": {
              "type": "object",
              "properties": {
                "status": {
                  "type": "object",
                  "properties": {
                    "code": {
                      "type": "number",
                      "required": true
                    },
                    "message": {
                      "type": "string",
                      "required": true
                    }
                  },
                  "additionalProperties": false,
                  "required": [
                    "code",
                    "message"
                  ]
                }
              },
              "additionalProperties": false,
              "required": [
                "status"
              ]
            }
          }
        },
        "description": ""
      }
    },

In full output you can see, that consumes contains application/x-www-form-urlencoded which is used by swagger as primary method too while it should be multipart/form-data for files.

@jaynetics
Copy link

this may be the same issue as #895 and https://stackoverflow.com/questions/70746890/apipie-swagger-generator-with-array-of-hashes-produces-array-of-strings

@jaynetics
Copy link

also, the docs state this as known limitations of the swagger export:

The apipie formats value is ignored.
It is not possible to specify the "consumed" content type on a per-method basis

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

2 participants