Skip to content

OpenAPI document generation should emit documents with deterministic structure #59809

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
1 task done
mikekistler opened this issue Jan 10, 2025 · 5 comments
Open
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@mikekistler
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

The OpenAPI document currently generated by .NET has a non-deterministic ordering of certain features such as path items, tag objects, and possibly other elements. This is problematic in situations where the OpenAPI document is generated at build time and committed to source control, because a small change in the app could generate a diff in the OpenAPI document that is simply a reordering of elements.

An example of the problem is [this PR in the eShop project], where the diff of the src/Catalog.API/Catalog.API.json file shows:

>git diff --stat main src/Catalog.API/Catalog.API.json 
 src/Catalog.API/Catalog.API.json | 602 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------
 1 file changed, 306 insertions(+), 296 deletions(-)

but by sorting the files with jq, we can see that the substantive changes are much smaller:

>diff <(git show main:src/Catalog.API/Catalog.API.json | jq --sort-keys .) <(jq --sort-keys . src/Catalog.API/Catalog.API.json) 
379a380,389
>           "400": {
>             "content": {
>               "application/json": {
>                 "schema": {
>                   "$ref": "#/components/schemas/ProblemDetails"
>                 }
>               }
>             },
>             "description": "Bad Request"
>           },
968,970d977
<       "name": "Search"
<     },
<     {
974a982,984
>     },
>     {
>       "name": "Search"

Here only the 400 response is substantive -- the other change is a recording of the tag objects which isn't remedied by the --sort-keys feature of jq.

Describe the solution you'd like

The framework should generate the OpenAPI document with a deterministic structure, such that changes to the app will produce a new document with minimal and only "substantive" changes in the textual diff.

Additional context

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jan 10, 2025
@mikekistler mikekistler added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jan 10, 2025
@mikekistler
Copy link
Contributor Author

More information on this: I was able to write a transformer that minimizes the diff of the V1 API document in eShop. The transformer:

  • Reorders the Path items to be sorted by path
  • Reorders the operations in each Path Item in a fixed order (get, post, put, patch, delete, head, options)
  • Reorders the entries of the global tags field to be ordered by tag name.

I'll publish this transformer in my aspnet-transformers-gallery project shortly. Users needing this support can use the transformer until we add the support directly in the framework.

@eerhardt
Copy link
Member

I was able to write a transformer that minimizes the diff of the V1 API document in eShop

Hey @mikekistler, I'm seeing diffs in eshop after I build:

Image

Image

Was this issue worked around in dotnet/eshop?

@mikekistler
Copy link
Contributor Author

@eerhardt Is it possible that this is just line ending difference?

@eerhardt
Copy link
Member

It's possible. But eshop has

# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

In its .gitattributes file. So if the OpenAPI document generation was writing the file correctly, this diff shouldn't happen.

@mikekistler
Copy link
Contributor Author

I think the .gitattributes may be enforced on a commit but not necessarily on a diff. Here's what VSCode tells me about the line endings of src/Catalog.API/Catalog.API.json in the main branch of eShop.

Image

Do you see this same thing for the OpenAPI generated by your build?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

2 participants