Skip to content
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

Generate OpenApi 3.1 compatible Enums? #4839

Open
JohnGalt1717 opened this issue Mar 26, 2024 · 3 comments
Open

Generate OpenApi 3.1 compatible Enums? #4839

JohnGalt1717 opened this issue Mar 26, 2024 · 3 comments

Comments

@JohnGalt1717
Copy link

Hi, is it possible to generate enaums like this: https://stackoverflow.com/questions/66465888/how-to-define-enum-mapping-in-openapi ?

I can't find any documentation on how to enable it, but I need 3.1 compatibility with the oneOf syntax.

@RicoSuter
Copy link
Owner

You can implement your own ISchemaProcessor and transform all enum schemas to OAI 3.1 enum schemas

@JohnGalt1717
Copy link
Author

OK, thanks.

Is there any plans to add this natively?

@JohnGalt1717
Copy link
Author

PS for anyone that needs it, here's quick and dirty standards compliant enum Schema Processor.

public sealed class FixNSwagEnumsProcessor : ISchemaProcessor {
	public void Process(SchemaProcessorContext context) {
		if (!context.ContextualType.Type.IsEnum)
			return;
		
		foreach(var item in context.Schema.EnumerationNames.Index()) {
			var enumValue = context.Schema.Enumeration.ElementAt(item.index);
			var enumSchema = new JsonSchema
			{
				ExtensionData = new Dictionary<string, object?>
				{
					{ "const", enumValue },
					{ "title", item.item }
				}
			};

			context.Schema.OneOf.Add(enumSchema);			
		}
	}
}

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

No branches or pull requests

2 participants