question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reusable enums support

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
nartccommented, Nov 3, 2019

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?

1reaction
kamilmysliwieccommented, Feb 23, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enums - Swagger
Reusable enums. In OpenAPI 3.0, both operation parameters and data models use a schema , making it easy to reuse the data types....
Read more >
How do I create reusable enums in Swagger specification ...
I have a problem with Enums. Say, I have two methods returning the very same Enum. In OpenAPI specification, I would like to...
Read more >
Reusable enums in Absinth : r/elixir - Reddit
Actually, dynamic Enum values support was added to absinthe v1.6.0 (https://github.com/absinthe-graphql/absinthe/pull/1023, ...
Read more >
Extensible Enums - Business Central - Microsoft Learn
Enums can be extended in order to add more values to the enumeration list in which case the Extensible property must be set...
Read more >
Build Enumerations of Constants With Python's Enum
This module provides the Enum class for supporting ... need to build a hierarchy of enum classes to reuse functionality through inheritance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found