Skip to content

Conversation

@18IsabellaM
Copy link

  • This PR depends on functionality added to a forked version of the openapiv3 crate, which I also have up for review in a separate PR. kurtbuilds/openapiv3#7
  • This PR helps resolve issue #23.

PR Description:

This PR improves the OpenAPI spec generation by correctly capturing and displaying error status codes and their descriptions.

Changes made:

  • Updated the OpenAPI generator to automatically include error responses derived from handlers that return errors as (StatusCode, String) tuples.

  • Added support for grouping multiple error messages under the same status code:

    • If a handler returns the same status code with different messages, those messages will now be collected into a list in the OpenAPI spec.
    • Example:
    Err((StatusCode::BAD_REQUEST, "Invalid email".into()))
    Err((StatusCode::BAD_REQUEST, "Missing field 'username'".into()))

    Will produce the following OpenAPI response entry:

    "400":
      description: |
        - Invalid email
        - Missing field 'username'
  • Added a new test case: 04-status_code.rs and a corresponding snapshot: 04-status_code.yaml.

  • Improved the test framework to compare parsed YAML structures instead of raw strings, preventing failures due to key ordering differences.

core/Cargo.toml Outdated
[dependencies]
inventory = "0.3.13"
openapiv3-extended = { version = "6" }
openapiv3-extended = { git = "https://github.com/18IsabellaM/openapiv3", branch = "isabella/error_json"}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Note: Using a Git branch here due to unmerged functionality in the openapiv3 repo that introduces a helper method: kurtbuilds/openapiv3#7
  • Once merged upstream, this can be changed back to the released version.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also use an (uncommitted) .cargo/config.toml file with the following to keep the PR mergeable:

[patch.crates-io]
openapiv3-extended = { path = "path/to/modified/local/copy" }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kurtbuilds fixed it

http = "1.0.0"
indexmap = "2"
openapiv3-extended = { version = "6" }
openapiv3-extended = { git = "https://github.com/18IsabellaM/openapiv3", branch = "isabella/error_json"}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Note: Using a Git branch here due to unmerged functionality in the openapiv3 repo that introduces a helper method: kurtbuilds/openapiv3#7
  • Once merged upstream, this can be changed back to the released version.

@18IsabellaM
Copy link
Author

Hey @kurtbuilds , I’ve opened a PR adding new functionality for error responses. When you have a chance, could you please review and consider merging it? Thanks so much!

@kurtbuilds
Copy link
Owner

kurtbuilds commented Aug 8, 2025

Thanks. I was fixing these merge conflicts but didn't finish last night. I'll get this over the line and published this weekend. Thank you for the contribution! IIRC, there's 1 failing test to be fixed. Welcome your help on that.

@18IsabellaM
Copy link
Author

Just ran just test and cargo test --all -all-features everything passed on my end, but let me know if I’m missing something. Happy to dig in more!

@18IsabellaM 18IsabellaM force-pushed the isabella/status_codes branch from a49877a to 0ff7b7a Compare August 13, 2025 17:28
macro/src/lib.rs Outdated
let mut collector = ErrorCollector::default();
collector.visit_block(&ast.block);

let mut errors_by_code: HashMap<String, Vec<String>> = HashMap::new();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be BTreeMap to enforce deterministic order

@jkleinknox
Copy link

jkleinknox commented Sep 17, 2025

Hi @kurtbuilds I pushed an update to change HashMap to BTreeMap to ensure deterministic order. So the PR is ready to be reviewed again. Thanks.

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

Successfully merging this pull request may close these issues.

Support status codes with axum

3 participants