danielgtaylor / huma

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

Need to explicitly call PrecomputeMessages() when using custom RequestBody #454

Closed guneyizol closed 1 month ago

guneyizol commented 4 months ago

https://github.com/danielgtaylor/huma/blob/6019e80758fc8b209c5e1a4e319d429572104f35/validate.go#L543

When using a custom RequestBody in a huma.Operation given to the huma.Register function, s.propertyNames field is only populated when the PrecomputeMessages() method is explicitly called on the Schema given to a MediaType.

Example:

// Schema definition
// ...

schema.PrecomputeMessages()

huma.Register(
        api,
        huma.Operation{
            OperationID:   "post-user-attributes",
            Method:        http.MethodPost,
            Path:          "/",
            Description:   "upsert the given attributes for the given user, validating using the given appKey's attributes",
            Summary:       "upsert attributes",
            DefaultStatus: http.StatusOK,
            RequestBody: &huma.RequestBody{
                Description: "My custom request schema",
                Required:    true,
                Content: map[string]*huma.MediaType{
                    "application/json": {
                        Schema: schema,
                    },
                },
            },
        },
// handler omitted

If this is a feature, I believe that it should be explicitly documented.

Thanks 🙏

danielgtaylor commented 4 months ago

@guneyizol nice catch, this is probably a bug. I'll try and get a fix up to ensure we always precompute at registration.