Comments (1)
@jpincas great question! This is somewhat complicated for a few reasons (isn't it always 😂...) There are really three scenarios for handling read-only fields:
- Ignore them. Huma doesn't care, and it's up to the service implementor to determine what to do with them. This is easy to implement and has no performance penalty but makes life harder when writing services. This makes round-trips of data easy.
- Return a validation error. Huma detects which fields are read-only, and if set in any
PUT
/POST
/PATCH
request it returns a 422. We use the same logic as defaults/resolvers to walk the incoming structure to determine if any value is non-zero. This has a small performance penalty but can be skipped for objects without read-only fields. This prevents round-trips from working. - Zero the fields. Like defaults, we walk the incoming structure and explicitly set zero values for any read-only fields. This has the highest performance penalty and is tricky to implement, but doable.
One of the major scenarios I needed to efficiently support with Huma is round-tripping of data with read-only fields, for example think of a created date or updated date or some computed hash value in a field of the resource. So you need to be able to GET
the resource, modify it in-place, then submit via PUT
and have it just work.
For Huma v2 I wound up going with option 1: ignore read-only fields. This is simplest and gives the most flexibility in how to handle the incoming fields, but it does mean you need to be careful when writing your service. For example, when doing PUT
we tend to either fetch any existing resource and copy over the read-only fields, or use partial update DB queries which ignore the read-only fields.
I'm happy to revisit this decision. It let me ship Huma v2 faster and keeps it easier to maintain, but if desired we can always add support for additional scenarios, and can probably do a better job of documenting this stuff instead of having it swimming around in my head 😄
Interesting side note: I actually had a Huma v1 PR for implementing each of these, see #63.
from huma.
Related Issues (20)
- Routing groups in Go 1.22 HOT 2
- Discussion: How best to catch and log 500s? HOT 2
- Panic on Validation Phase of custom `time.Time` Type HOT 2
- Setting headers in error response always appends HOT 1
- fmt.Println message: warning: unable to set write deadline HOT 1
- Panic on write with very large response body bricks server for some time as response body is logged to standard output HOT 1
- Applying common headers to multiple responses (doc's issue)
- Response struct without a "Body" field is silently ignored if using Register HOT 1
- Make huma play well with custom error types returned from handlers
- Humagin multiple API's per route group? HOT 1
- Returning nil as response body causes 500
- Can I use net/http.Handler directly?
- custom validation tags with constraints specified in the spec.
- Handling of custom types for List inputs of query params
- Suggestion: groups and sub-routing
- Discussion: option to make all APIs registered with security scheme by default HOT 1
- SSE Error with gin adapter: "warning: unable to set write deadline"
- Validation for nested to array primitive types.
- Required HTTP headers set via `huma.Param` are neither enforced nor validated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from huma.