Home > front end >  How to make Swagger UI accepts request body with optional field
How to make Swagger UI accepts request body with optional field

Time:10-09

Well, the title is very easy to understande, but to make things detailed:

I have an expressjs application, written with TypeScript. The app receives a request, where the JSON body is like this:

{
    name: "name",
    description: "description",
    github: "github",
    logo: "logo",
    app: "app"
}

But the thing is: The app property is optional, since I will insert the object in the database WITH that property only if the request was made with it declared.

I'd like to know if it's possible to make the Swagger accept that optional thing, and if so, how to do that?

EDIT: As requested by @Anatoly, this is the swagger route definition:

post: {
    summary: 'Create a new project',
    tags: ['projects'],
    requestBody: {
        content: {
            'application/json': {
                schema: {
                    $ref: '#/components/schemas/Project'
                }
            }
        }
    }
},
components: {
    schemas: {
        Project: {
            type: 'object',
            properties: {
                name: {
                    type: 'string'
                },
                description: {
                    type: 'string'
                },
                github: {
                    type: 'string'
                },
                logo: {
                    type: 'string'
                },
                app: {
                    type: 'string'
                }
            }
        }
    }
}

CodePudding user response:

All params except path ones are optional unless they have the required attribute as true

app:
  type:string
  required:true

Refer the heading Required and Optional Parameters at https://swagger.io/docs/specification/describing-parameters/

  • Related