Swagger: Reusable enums support

Created on 3 Nov 2019  路  9Comments  路  Source: nestjs/swagger

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Currently enums are not defined under components section

"components": {
    "schemas": {
        "Album": {
            "type": "object",
            "properties": {
                "direction": {
                    "type": "string",
                    "enum": [
                        "Up",
                        "Down",
                        "Right",
                        "Left"
                    ]
                }
            }
        }
    }
}

Expected behavior

"components": {
    "schemas": {
        "direction": {
            "type": "string",
            "enum": [
                "Up",
                "Down",
                "Right",
                "Left"
            ]
        },
        "Album": {
            "type": "object",
            "properties": {
                "direction": {
                    "schema": {
                        "$ref": "#/components/schemas/direction"
                    }
                }
            }
        }
    }
}

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

OpenApi 3 allows us to reference enums under components section to use them multiple times.
See the "Reusable Enums" section here https://swagger.io/docs/specification/data-models/enums/

It would allow us to get enums types on the front side too with classes, using a generator like ng-openapi-gen for example, which would be amazing.

Environment


Nest version: 4.0.0-next.2


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

enhancement

Most helpful comment

There is one caveat about this feature which is getting the Enum name at runtime. One possible solution without adding more property to ApiProperty is to have: enum: to accept a lazily-evaluated function as follow:

enum Status {
   Inactive = 'inactive',
   Active = 'active'
}

@ApiProperty({enum: () => Status})
status: Status;

This will allow us to grab the Status string as the enum name at runtime to be able to "reuse" it. Any suggestions?

All 9 comments

There is one caveat about this feature which is getting the Enum name at runtime. One possible solution without adding more property to ApiProperty is to have: enum: to accept a lazily-evaluated function as follow:

enum Status {
   Inactive = 'inactive',
   Active = 'active'
}

@ApiProperty({enum: () => Status})
status: Status;

This will allow us to grab the Status string as the enum name at runtime to be able to "reuse" it. Any suggestions?

It sounds good to me

PR #412 is in

@flogh are you open to pull my branch and give it some test runs?

Of course ! I'm doing it right now

Awesome. I'm kinda running on some tight timeline here but will test on my project as soon as I have some free time. That said, if you could post your feedback/bug reports/suggestions on the PR thread, that'd be awesome. Thanks again!

Added in 4.3.0. We're tracking PR to the docs here https://github.com/nestjs/docs.nestjs.com/pull/1024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kalaivanan-muthusamy picture kalaivanan-muthusamy  路  4Comments

KatSick picture KatSick  路  3Comments

cdiaz picture cdiaz  路  4Comments

mogusbi picture mogusbi  路  3Comments

Fiorello picture Fiorello  路  5Comments