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

fix: enforce path params are required #103

Merged
merged 1 commit into from
May 16, 2023
Merged

Conversation

iwong-isp
Copy link
Collaborator

Huma marks path params as required when generating the OpenAPI docs, which complies with OpenAPI 3.0. However, Huma does not check if path params values are non-empty during runtime.

For example, in the bookstore example, not providing the genre-id will result in a 404 after failing to find an empty genre instead of a 400.

restish :8888/v1/genres//books
HTTP/1.1 404 Not Found

{
  $schema: "https://localhost:8888/schemas/ErrorModel.json"
  detail: "Genre  not found"
  status: 404
  title: "Not Found"
}

Huma uses the Chi router which does not mark path parameters as required unless regex or additional custom logic is added. This PR modifies the resolver to check if a path param value is empty and throws an error instead of continuing.

The new output for the same bookstore request is as follows:

restish :8888/v1/genres//books
HTTP/1.1 400 Bad Request

{
  $schema: "https://localhost:8888/schemas/ErrorModel.json"
  detail: "Error while processing input parameters"
  errors: [
    {
      location: "path.genre-id"
      message: "genre-id is required"
      value: ""
    }
  ]
  status: 400
  title: "Bad Request"
}

@iwong-isp iwong-isp merged commit 62c3193 into main May 16, 2023
@iwong-isp iwong-isp deleted the iw/require-path-params branch May 16, 2023 17:06
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.

3 participants