0

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'
                }
            }
        }
    }
}
João Casarin
  • 674
  • 2
  • 9
  • 27
  • 1
    Can you show a definition of this route for Swagger in your post? – Anatoly Oct 06 '21 at 05:06
  • Hey @Anatoly sure I can, I'm updating it, but the swagger route definition is not complete, I'm still learning it. – João Casarin Oct 06 '21 at 17:24
  • Related (or duplicate): [How to specify if a field is optional or required in OpenAPI/Swagger?](https://stackoverflow.com/q/40113049/113116) – Helen Oct 06 '21 at 21:17

1 Answers1

1

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/

Akshay
  • 158
  • 1
  • 7