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.

Reuse enum definition multiple times

See original GitHub issue

Enum in generated OpenAPI document is defined inline.

  "schemas": {
    "myRequest": {
(...)
      "properties": {
        "myEnumProperty": {
          "enum": [
            "value1",
            "value2"
          ],
          "type": "string",
(...)

When using an enum multiple times within the same API, Visual Studio ‘Connected Services’ (utilizing nswag) will generate multiple enum definitions on client-side for the same enum. This requires uneccessary type conversions in client code.

OpenAPI spec allows to define enum as reference, which results in only one enum definition on client-side:

  "schemas": {
    "myEnumType": {
      "enum": [
        "value1",
        "value2"
      ],
      "type": "string",
(...)
    },
    "myRequest": {
(...)
      "properties": {
        "myEnumProperty": {
          "$ref": "#/components/schemas/myEnumType"
        }
(...)

Is it possible to configure enum gerenation as reference?

Would be a great help.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
AllTakencommented, Nov 24, 2021

@level120 Having this work as long as enum names are unique would already add a lot of value…

2reactions
HermannGrubercommented, Aug 24, 2021

I guess I’m facing the same issue. I have an azure function, for which the generated swagger.json looks similar as above, but I want to have the enums defined once and then referenced. Currently it reads

{
            "name": "pruefungszeitpunkt",
            "in": "query",
            "description": "Prüfungszeitpunkt",
            "schema": {
              "enum": [
                "beiKodierung",
                "zurAbrechnung",
                "waehlbar"
              ],
              "type": "string",
              "default": "beiKodierung"
            },
            "x-ms-visibility": "important"
          }

But I would like to see

          {
            "name": "pruefungszeitpunkt",
            "in": "query",
            "description": "Prüfungszeitpunkt",
            "schema": {
              "$ref": "#/components/schemas/Pruefungszeitpunkt",
              "type": "string"
            },
            "x-ms-visibility": "important"
          },

Can you please tell me whether this will be resolved using the current issue? If not, I will happily open a separate issue. (My generated swagger.json says at the top of the file that it is targeting OpenApi 3.0.1.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reusing enum values in separate enum types - c++
Is there a way to reuse the same enum value in separate types? I'd like to be able to something like the following:...
Read more >
Why are two literals allowed to have the same values in ...
Enumerations are just named integer constants with a type, so you can have two constants with the same name. Notably this allows you...
Read more >
Language Guide (proto 2) | Protocol Buffers Documentation
Combining Messages leads to bloat While multiple message types (such as message, enum, and service) can be defined in a single .proto file,...
Read more >
Build Enumerations of Constants With Python's Enum
In this tutorial, you'll learn how to create and use enumerations of semantically related constants in Python. To do this, you'll use the ......
Read more >
How to extend enums in TypeScript
The short answer is no, you can't extend enums because TypeScript offers no language feature to extend them. However, there are workarounds you ......
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