huma icon indicating copy to clipboard operation
huma copied to clipboard

Omit `$schema` property from request schemas in OpenAPI

Open not-my-profile opened this issue 6 months ago • 3 comments

The OpenAPI generated by Huma contains $schema properties:

{
  "components": {
    "schemas": {
      "Foo": {
        "additionalProperties": false,
        "properties": {
          "$schema": {
            "description": "A URL to the JSON Schema for this object.",
            "example": "https://example.com/schemas/Foo.json",
            "format": "uri",
            "readOnly": true,
            "type": "string"
          },

Since Huma generates these properties not just for response models but also for request models this leads to inconvenience when generating a client with oapi-codegen and using the exhaustruct linter (to make sure that I didn't forget any struct fields). Right now I have to resort to post-processing the generated OpenAPI with jq, which I'd like to avoid. It would be nice if there was an option to disable these $schema fields in the generated OpenAPI.

not-my-profile avatar Jul 02 '25 06:07 not-my-profile

Does #428 help, perhaps? Overriding the CreateHooks of the Huma config removes the $schema field from responses, but not components so it might not be fully what you need.

rhodeon avatar Jul 07 '25 22:07 rhodeon

Thanks that does work! I'm wondering now if adding the $schema to request models even makes any sense? This seems like a bug (or at least unfortunate side-effect of the $schema in response models) to me.

not-my-profile avatar Jul 08 '25 05:07 not-my-profile

Yes, the comments here and here imply that this is meant for the response only.

Though the justification for that field,

This is useful for clients to understand the structure of the response and enables things like as-you-type validation & completion of HTTP resources in editors like VSCode.

also applies to requests in my opinion.

But yeah, in any case, I agree it should be clearer if the $schema on requests is intended or if it's a side-effect.

rhodeon avatar Jul 08 '25 11:07 rhodeon