azure-functions-openapi-extension icon indicating copy to clipboard operation
azure-functions-openapi-extension copied to clipboard

Reuse enum definition multiple times

Open OliverNET opened this issue 4 years ago • 13 comments

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.

OliverNET avatar Jun 29 '21 09:06 OliverNET

Hello, I'm not sure if it's the answer, but there's a way to reuse it, so I'm attaching a link. https://swagger.io/docs/specification/data-models/enums/

changbokLee avatar Aug 22 '21 18:08 changbokLee

Hello, I'm not sure if it's the answer, but there's a way to reuse it, so I'm attaching a link. https://swagger.io/docs/specification/data-models/enums/

Thanks for the info! Let's find a way to reuse the enum through this extension.

justinyoo avatar Aug 23 '21 04:08 justinyoo

@changbokLee Would you mind taking a look at this issue?

justinyoo avatar Aug 23 '21 04:08 justinyoo

okay sir i will try , our project are supporting that swagger version 2.0 3.0? , documention that it is explainning only 2.0 3.0 version .

changbokLee avatar Aug 23 '21 04:08 changbokLee

@changbokLee Our extension currently relies on https://github.com/microsoft/OpenAPI.NET that supports both OpenAPI v2 and v3.0.1. But your codebase should focus on v3.0.1. The rest will be automatically done for you.

justinyoo avatar Aug 23 '21 07:08 justinyoo

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.)

HermannGruber avatar Aug 24 '21 10:08 HermannGruber

@justinyoo Could you assign this to me?

level120 avatar Aug 26 '21 10:08 level120

@level120 Let’s wait for @changbokLee ‘s answer.

justinyoo avatar Aug 26 '21 12:08 justinyoo

@justinyoo i m trying if i cant , i will can entrust wait for 2weeks only @level120

changbokLee avatar Aug 27 '21 05:08 changbokLee

Unfortunately I was not able to follow up with the latest discussion on this. Can you confirm that my comment above refers to the same issue? Otherwise, I'd happily open a separate issue.

HermannGruber avatar Sep 09 '21 12:09 HermannGruber

I think this issue can't start before solving https://github.com/Azure/azure-functions-openapi-extension/issues/248 Because enum type may have many definitions having different namespaces.

namespace Some.Alpha
{
    public enum Status { ... }
}

namespace Any.Alpha
{
    public enum Status { ... }
}

level120 avatar Oct 01 '21 10:10 level120

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

AllTaken avatar Nov 24 '21 10:11 AllTaken

Has there been any progress here?

josh-burton avatar Apr 28 '22 00:04 josh-burton

its already so many months and there isnt anything new about it ?

kamilpaszkowski avatar Jan 05 '23 11:01 kamilpaszkowski

What is the status on this, it currently gives us lots of hassle when the enums aren't placed in the schema.

Dennis-Rosenbaum avatar Feb 08 '23 11:02 Dennis-Rosenbaum

Bump, fix this pls :)

RoelVerhees avatar Jul 06 '23 12:07 RoelVerhees

#610 has addressed this issue. The next v2-preview release will contain this fix.

justinyoo avatar Sep 19 '23 11:09 justinyoo