nestia icon indicating copy to clipboard operation
nestia copied to clipboard

Enum as its own schema

Open sztyup opened this issue 4 months ago • 0 comments

Feature Request

I would like to have enums not generated in the swagger as an inline type string schema with enum values but as its own schema element, that can be reused and have its own name. This same functionality is supported by the builtin NestJs swagger generator: https://docs.nestjs.com/openapi/types-and-parameters#enums-schema

If you can give me a few pointers I can try to come up with a PR.

Example method

enum AnimalEnum {
    Cat,
    Dog
}

interface RequestBody {
    animal: AnimalEnum
}
public function exampleRequest(@TypedBody() body: RequestBody)

Expected output

"requestBody": {
  "content": {
    "application/json": {
      "schema": {
        "type": "object",
        "properties": {
          "animal": {
            "$ref": "#/components/schemas/AnimalEnum"
          }
        }
      }
    }
  }
},
"components": {
  "schemas": {
    "AnimalEnum": {
      "type": "string",
      "enum": [
        "Cat",
        "Dog"
       ]
    },
  }
},

Actual output

"requestBody": {
  "content": {
    "application/json": {
      "schema": {
        "type": "object",
        "properties": {
          "animal": {
            "type": "string",
            "enum": [
                "Cat",
                "Dog"
            ]
          }
        }
      }
    }
  }
},

sztyup avatar Oct 06 '24 20:10 sztyup