Comments (2)
I'm also curious why this functionality is desired and what some examples of usage might be
I've got quite a few APIs in Python and PHP where the input looks like this:
{
"type": "example",
"dto": "on the backend is based onthe type"
}
IMO, this is somewhat nicer for clients as it makes a particular field required vs...
In other systems like protobuf you don't really model explicit discriminated unions, it's more like a message with optional fields to all the other message types
Which would mean that all fields could be null or missing. That's an invalid request, but the OpenAPI spec doesn't really say that at least one is required.
I've also done stuff like this:
{
"type": "example",
"example_value": { ... }
}
Where the type
makes an {type}_value
or {type}
field required. And it's fine, but, IMO the generated api spec doesn't do a great job of conveying which fields are required/not required, etc.
I was going to go down this route for now with my current project and just do a little more work in the handler.
It seems like this:
you can't list implementations of an interface via reflection
is the crux of the issue though. And using discriminator map could mean that a lot of sugar could be provided by Huma, but it'd still be pretty gnarly to support in request Body
fields without full fledged support for union types in the go language itself.
Though maybe somethign like this could be done with generics now:
type RequestInput[T TypeOne | TypeTwo | TypeThree] struct {
Body T
}
from huma.
@chrisguitarguy thanks for opening this issue. We don't currently have any special support for discriminator fields. Like you said, oneOf
is supported in huma.Schema
but is currently never generated from Go structs because Go doesn't have union types and you can't list implementations of an interface via reflection, plus there's a desire to generally keep things simple (i.e. Huma doesn't have to support all features of OpenAPI since OpenAPI is meant to describe all possible APIs).
That said, I'm not opposed to adding support for discriminators to huma.Schema
for people who want to manually set them up, and like you said it may be possible to build some utilities on top of that (probably outside of the schema, maybe in a separate package?). I'm happy to review proposals and implementations with examples of how this would be used.
Given the potential complexity I'm also curious why this functionality is desired and what some examples of usage might be. In other systems like protobuf you don't really model explicit discriminated unions, it's more like a message with optional fields to all the other message types (possibly with a one-of, but that's just sugar it doesn't change the wire protocol or field names/types). Just trying to understand more about the use-cases and possible alternative approaches.
from huma.
Related Issues (20)
- Fiber adapter NewWithGroup should accept fiber.Router interface
- Getting "Example cannot be created for this schema" in stoplight Response Example section. HOT 4
- Parameters description added to incorrect level HOT 3
- Panic in OnAddOperation HOT 2
- How to add versioning to my API using huma? HOT 7
- Register routes functionality with middlewares HOT 3
- Api grouping bug - /openapi.yaml request from the docs page does not consider the groups HOT 2
- huma.NewError doesn't support error wrapping. HOT 3
- Setting headers in error response HOT 6
- Add more tests for cookie HOT 3
- Add custom header from SSE Handler HOT 3
- How should nullable query params be handled without pointers? HOT 4
- Add support for netip.Addr HOT 1
- Forwarding headers through the middleware not working HOT 5
- Override ErrorModel for single operations HOT 6
- Feature: Accept valueless path/query/header/cookie tags
- Docs UI doesn't handle multiple cookies properly HOT 1
- Allow custom Transform for nested objects HOT 2
- Error thrown when setting cli name HOT 2
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.