danielgtaylor / huma

Huma REST/HTTP API Framework for Golang with OpenAPI 3.1
https://huma.rocks/
MIT License
1.71k stars 134 forks source link

feat: allow toggling additional properties from request body struct tag #441

Closed lsdch closed 2 months ago

lsdch commented 2 months ago

This is a small QoL improvement that allows toggling the additionalProperties attribute on a request body, without having to modify the body struct type.

Usage :

type HandlerInput struct {
  Body SomeInputType `additionalProperties:"true"`
}
codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 92.77%. Comparing base (e1b7179) to head (43dc637).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #441 +/- ## ======================================= Coverage 92.76% 92.77% ======================================= Files 21 21 Lines 3567 3571 +4 ======================================= + Hits 3309 3313 +4 Misses 220 220 Partials 38 38 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

danielgtaylor commented 2 months ago

@lsdch this is a good idea but I didn't add this because of the following scenario:

type MyBody struct {
  Field string `json:"field"`
}

type Response1 struct {
  Body MyBody `additionalProperties:"true"`
}

type Response2 struct {
  Body MyBody `additionalProperties:"false"`
}

How would you generate the OpenAPI spec for this? In JSON Schema the additionalProperties field is a property of the object schema rather than on the individual properties within some object. You could use allOf to combine the object definition with additionalProperties, but that has pretty undesirable effects for code generated SDKs.

lsdch commented 2 months ago

@danielgtaylor ah yes, totally agree thanks for pointing this out. Closing!