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

Order of struct fields does not seem to be preserved even when using the preserve_order feature #561

Closed
johnperry-math opened this issue Apr 4, 2023 · 5 comments · Fixed by #562
Labels
bug Something isn't working

Comments

@johnperry-math
Copy link

Issue #428 addressed the question of preserving the order of fields as declared in a struct, but when I add this feature to Cargo.toml, the fields are still sorted in the Schema.

For example, suppose I modify the todo-axum example's Cargo.toml so that we have:

utoipa = { path = "../../utoipa", features = ["axum_extras", "preserve_order"] }

...and main.rs so that we have this:

    /// Item to do.
    #[derive(Serialize, Deserialize, ToSchema, Clone)]
    pub(super) struct Todo {
        id: i32,
        #[schema(example = "Buy groceries")]
        value: String,
        checked: bool,
        done: bool,
    }

The result is that the fields are still in alphabetical order:
image

Am I doing something wrong, have I misunderstood the purpose of that change, or is it something else entirely?

Similarly, is it possible to display the paths in the order they're declared in the openapi macro's paths(...) declaration?

@juhaku
Copy link
Owner

juhaku commented Apr 4, 2023

Am I doing something wrong, have I misunderstood the purpose of that change, or is it something else entirely?

Hey, this seems to be regression bug in the utoipa-swagger-ui. If you print the schema as json to to console or file before you pass it to the SwaggerUi you should see correct behavior. I believe this occurred due to changes here #502.

I will fix as a next thing on my list.

Similarly, is it possible to display the paths in the order they're declared in the openapi macro's paths(...) declaration?

This is not directly possible with utoipa since paths are sorted in UI itself. https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display In order to sort paths you need to provide operationSorter attribute that is not supported by utoipa-swagger-ui by default at the moment.

@juhaku juhaku added the bug Something isn't working label Apr 4, 2023
@juhaku juhaku moved this to In Progress in utoipa kanban Apr 4, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in utoipa kanban Apr 4, 2023
@juhaku juhaku moved this from Done to Released in utoipa kanban Apr 4, 2023
@johnperry-math
Copy link
Author

Thank you for the quickr response! Do you know when it will appear via cargo update or cargo upgrade?

@juhaku
Copy link
Owner

juhaku commented Apr 6, 2023

To my knowledge it should be already available since I have already released it to the crates. https://crates.io/crates/utoipa-swagger-ui

@johnperry-math
Copy link
Author

Thanks, I can confirm it works :-)

@juhaku
Copy link
Owner

juhaku commented Apr 6, 2023

Great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

2 participants