-
-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OneOf enlightenment needed #730
Comments
I've also noticed another "issue" with schemas reached as The response example is only populated with example injected via the diff to examples/oneOf.go--- example.go 2025-02-17 16:47:14.226102040 +0100
+++ mod_1.go 2025-02-17 17:29:04.292782002 +0100
@@ -38,9 +38,20 @@
Body any
}
+type Some struct {
+ SomeInt int `json:"some_int" example:"42"`
+ SomeString string `json:"some_string" example:"forty two"`
+ Nested []Child `json:"child"`
+}
+
+type Child struct {
+ ID int `json:"id" example:"420"`
+}
+
// GreetingBody is the body of the response for the latest version of the API.
type GreetingBody struct {
Message string `json:"message"`
+ Objects Some `json:"objects"`
}
// GreetingBodyOld is the body of the response for the old version of the API.
@@ -57,10 +68,12 @@
// Create a schema for the output body.
registry := api.OpenAPI().Components.Schemas
+
+ schNew := registry.Schema(reflect.TypeOf(GreetingBody{}), true, "")
+ schOld := registry.Schema(reflect.TypeOf(GreetingBodyOld{}), true, "")
schema := &huma.Schema{
OneOf: []*huma.Schema{
- registry.Schema(reflect.TypeOf(GreetingBody{}), true, ""),
- registry.Schema(reflect.TypeOf(GreetingBodyOld{}), true, ""),
+ schNew, schOld,
},
}
@@ -76,6 +89,8 @@
"application/json": {
Schema: schema,
},
+ "old": {Schema: schOld},
+ "new": {Schema: schNew},
},
},
}, |
You should definitely open an issue on the Stoplight Elements repo for the UI-related concerns. I don't have much control over that unfortunately! As for the missing |
Seems issues related to oneOf are left as is on A good alternative would be huma wise, I now only needs to understand how to add the |
BTW I took a quick look at the schema generation code again and it currently has two constraints that prevent your example from working:
Even if I set a The transformer could probably be modified to support Edit: also yes Scalar rocks! I just can't change the default docs without potentially breaking a lot of people so for now you have to manually set it. I honestly thought Stoplight would be better supported long-term but since being bought I haven't seen a ton of progress on Stoplight Elements. |
Cool thanks for looking after it ! I think I can live with it If I know there isn't a proper solution yet / never. Feel free to close the issue |
Hi, I'm trying to "fully" support an endpoint that may return a "OneOf" equivalent as response body.
I've looked a bit around the github, found some issue and a nice example - but they're a few limitations I've faced afterward:
"$schema"
fieldresponse example
I've found some weird ways of "fixing" my issues, and I'd like help to get from "weird ways" to "expected ways" 🙈
I ended with the following piece of code to fix my issue
1
and2.b
:diff to the example
1
: for some reason, referencingschOld
andschNew
as schema "as is" to a referencedhuma.MediaType
enable the magic that inject the$schema
field in response of these structnew response example
2.b
: the"old"
and"new"
key now exist in the UI, allowing me to inject more "context as doc"Swagger UI screenshot
Little detours, If I now select the
"old"
or"new"
content type in the swagger UI, the example is now cleared. As a "fix" I'm currently setting myMediaType.Example
to some matching, json.RawMessage, but I would prefer for it to be auto-generated like usual response schema.Regarding
2.a
; I still haven't found a solution, so currently I have to accessdocs#/schemas/GreetingBodyOld
to obtain a full doc of my response.nb:\
1
isn't that much of an issue, just my PTSD.\2.a
shall probably be opened on the swagger UI repo aka https://github.com/stoplightio/elements.\2.b
probably has no solution as of today ?The text was updated successfully, but these errors were encountered: