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

Fix required body if one parameter is required #476

Merged
merged 4 commits into from
Nov 13, 2021

Conversation

ced42
Copy link
Contributor

@ced42 ced42 commented May 7, 2021

Hello,

When Flasgger is used with marshmallow and custom schema, the issue concerns the requirement of the body. Currently, the body is not required if at least one parameter (of the body) is required. This is the patch of this issue.

For example, my application has one endpoint to create an user: "/v1/pcc/{pcc_uid}/user" with a body parameter which is a UserSchema. One parameter in this schema is required, but body is not required.
Flasgger generates this JSON:

{
  "basePath": "/adduser_python_admin",
  "definitions": {
    "UserSchema": {
      "properties": {
        "email": {
          "format": "email",
          "type": "string"
        },
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "shortName": {
          "type": "string"
        }
      },
      "required": [
        "shortName"
      ],
      "type": "object"
    },
    "user.Credentials": {
      "properties": {
        "otpSecret": {
          "description": "OTP",
          "type": "string"
        },
        "password": {
          "description": "password",
          "type": "string"
        }
      }
    },
    "user.Id": {
      "properties": {
        "userId": {
          "description": "Id of created user",
          "type": "integer"
        }
      }
    },
    "user.State": {
      "properties": {
        "state": {
          "description": "wanted state",
          "type": "string"
        }
      }
    }
  },
  "info": {
  },
  "paths": {
   "/v1/pcc/{pcc_uid}/user": {
      "post": {
        "consumes": [
          "application/json"
        ],
        "deprecated": false,
        "description": "Create an user for a PCC",
        "operationId": "admin.createUser",
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": false,
            "schema": {
              "$ref": "#/definitions/UserSchema"
            }
          },
          {
            "in": "path",
            "name": "pcc_uid",
            "required": true,
            "type": "string"
          }
        ],
        "produces": [
          "application/json"
        ],
        "responses": {
          "201": {
            "description": "Success request",
            "schema": {
              "$ref": "#/definitions/user.Id"
            }
          },
          "400": {
            "description": "Validation error"
          }
        },
        "schemes": [],
        "security": [],
        "summary": "Create the user ",
        "tags": [
          "admin"
        ]
      }
    }
  },
  "swagger": "2.0"
}

The important part is "required": false in:

        "operationId": "admin.createUser",
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": false,
            "schema": {
              "$ref": "#/definitions/UserSchema"
            }

It normally should be "required": true because one parameter of UserSchema is required.

Regards,
Cédric.

@ced42
Copy link
Contributor Author

ced42 commented May 27, 2021

Hello,
Please let me know what is missing in my PR to be merged.

Regards,
Cédric.

@billyrrr billyrrr merged commit ff52fcf into flasgger:master Nov 13, 2021
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.

None yet

2 participants