Skip to content

OpenAPI: String names from enum in schema for enum attributes #1726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bjornharrtell opened this issue Apr 28, 2025 · 8 comments
Open

OpenAPI: String names from enum in schema for enum attributes #1726

bjornharrtell opened this issue Apr 28, 2025 · 8 comments

Comments

@bjornharrtell
Copy link
Contributor

bjornharrtell commented Apr 28, 2025

If one use an enum as attribute openapi generation will create a schema with the integer ids of the enum values. It would be great if it could also include the string names and possibly even description attributes as x-enum-varnames and/or x-enum-descriptions.

@bjornharrtell
Copy link
Contributor Author

bjornharrtell commented Apr 28, 2025

@bkoelman
Copy link
Member

Thanks for trying the preview and reporting back. It's already possible to serialize as strings. Did you add the last line from https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/test/OpenApiTests/LegacyOpenApi/LegacyStartup.cs?

I don't remember enum description handling, but https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/test/OpenApiTests/OpenApiStartup.cs#L28 might do the trick. If not, I'm open to support extensions like you mentioned. Have you tried if NSwag and Kiota understand them?

@bkoelman
Copy link
Member

@bkoelman
Copy link
Member

bkoelman commented Apr 28, 2025

According to microsoft/kiota#90, kiota looks for x-ms-enum, which is described at https://azure.github.io/autorest/extensions/#x-ms-enum.

@bkoelman
Copy link
Member

Seems that Swashbuckle doesn't support doc-comments on enum members, according to domaindrivendev/Swashbuckle.WebApi#1290. There it is mentioned the problem can be worked around with a custom schema filter.

@bjornharrtell
Copy link
Contributor Author

bjornharrtell commented Apr 28, 2025

I'm happy with serializing the id in json-api responses / documents, in this case, but want to describe those ids in openapi schema (for consumers of the api). I'm not sure if x-ms-enum is intended for that... I haven't tried getting any tooling to actually use x-enum-varnames and/or x-enum-descriptions, this was just something I stumbled upon when making an experimental api (as recreation of an older semi-json-api api).

Lengthy discussion about something similar at RicoSuter/NSwag#1993 though.

And I can see some custom swagger gen document filter in here - https://stackoverflow.com/questions/36452468/swagger-ui-web-api-documentation-present-enums-as-strings that does something along what I would like but just as a plain presentation string which.

Seems strange to me that openapi haven't standardised a way to describe enum values as something more than an ordered list of numbers of strings.

@bkoelman
Copy link
Member

Yeah, this is one of those gaps in the OpenAPI spec. At least a way is provided in the OpenAPI v3.1 spec: https://spec.openapis.org/oas/v3.1.1.html#x4-8-24-6-4-model-with-annotated-enumeration.

@bjornharrtell
Copy link
Contributor Author

I see. So mabye this is a wait and see thing for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants