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

Sort keys in OpenAPI specifications #4862

Open
varganatt opened this issue Apr 18, 2024 · 0 comments
Open

Sort keys in OpenAPI specifications #4862

varganatt opened this issue Apr 18, 2024 · 0 comments

Comments

@varganatt
Copy link

varganatt commented Apr 18, 2024

Thanks for a great library!

When generating an OpenAPI specifications, the paths and components are not sorted in the same order when compiling on Windows vs. Docker. Perhaps the sort order is not specified by NSwag and therefore somewhat arbitrary? That would explain why different instances of .NET cause different outputs.

Feature suggestion: Automatically sort paths and components.

Background for why this is important: I want generation of the OpenAPI spec to be deterministic, so that I more easily can check for unexpected changes.


As a workaround, I have added a document processor:

config.DocumentProcessors.Add(new SortKeysDocumentProcessor());

public class SortKeysDocumentProcessor : IDocumentProcessor
{
    private static void SortKeys<T>(IDictionary<string, T> dictionary)
    {
        var keyValues = dictionary.OrderBy(x => x.Key).ToList();
        dictionary.Clear();
        foreach (var kv in keyValues)
            dictionary.Add(kv.Key, kv.Value);
    }

    public void Process(DocumentProcessorContext context)
    {
        SortKeys(context.Document.Paths);
        SortKeys(context.Document.Components.Schemas);
    }
}
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

1 participant