这是indexloc提供的服务,不要输入任何密码
Skip to content

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: "http://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: "http://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.

4 participants