-
-
Notifications
You must be signed in to change notification settings - Fork 248
Description
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:
- the response payload miss the
"$schema"field
response example
╰─ 16:10:01 ❯ http :8888/greeting/oneof ─╯
HTTP/1.1 200 OK
Content-Length: 28
Content-Type: application/json
Date: Mon, 17 Feb 2025 15:10:01 GMT
{
"message": "Hello, oneof!"
}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 and 2.b:
diff to the example
--- example.go 2025-02-17 16:14:55.593638808 +0100
+++ mod_1.go 2025-02-17 16:14:38.690559026 +0100
@@ -57,10 +57,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 +78,8 @@
"application/json": {
Schema: schema,
},
+ "old": {Schema: schOld},
+ "new": {Schema: schNew},
},
},
},1: for some reason, referencingschOldandschNewas schema "as is" to a referencedhuma.MediaTypeenable the magic that inject the$schemafield in response of these struct
new response example
╰─ 16:36:46 ✘ 1 ❯ http :8888/greeting/oneof ─╯
HTTP/1.1 200 OK
Content-Length: 88
Content-Type: application/json
Date: Mon, 17 Feb 2025 15:36:46 GMT
Link: </schemas/GreetingBody.json>; rel="describedBy"
{
"$schema": "http://localhost:8888/schemas/GreetingBody.json",
"message": "Hello, oneof!"
}2.b: the"old"and"new"key now exist in the UI, allowing me to inject more "context as doc"
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 my MediaType.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 access docs#/schemas/GreetingBodyOld to obtain a full doc of my response.
nb:\
- issue
1isn't that much of an issue, just my PTSD.\ - issue
2.ashall probably be opened on the swagger UI repo aka https://github.com/stoplightio/elements.\ - issue
2.bprobably has no solution as of today ?
