-
-
Notifications
You must be signed in to change notification settings - Fork 421
Update addError to give precedence to newly added schemas #5479
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
Conversation
|
HttpApiSchema.UnionUnify creates a union whose members are listed in the order of `[...firstArgUnionMembers, ...secondArgUnionMembers]`. When encoding a union, Schema will try each member in the order that they appear. So the `addError` method on each of the HttpApi* classes, by passing the user-provided schema as a second argument, causes older schemas that encompass newer schemas to take precedence over the newer ones. By flipping the argument order, users are now able to use `addError` with a schema that (partly) encompasses an older one, to override the encoding behaviour of value conforming to that schema.
bb87db2 to
8ac3f22
Compare
|
I also have a WIP for adding a new However, since this already addresses the bulk of the pain with a very small change, and since I think it's a good change overall, I'd say it's still worth considering. |
|
There's more sites where a union of the For example, if the middleware failure is a schema that encompasses a type that was already part of the app, I guess it makes sense to do the encoding with the middleware-supplied schema? But I can see a counter-argument to that too. If we don't want to flip the arg order, an alternative solution in the same vain as this one, would be to have |
|
For some reason if I install the latest From reading the code, I don't really understand what changed it, but it even fixes the OpenAPI specs (which this PR doesn't). |
Actually no. The tagged error is still in the spec, but it's kind of hidden on the swagger page behind my custom 400-errors. |
|
I think the order that a user adds their error schemas should be preserved (if they call .addError multiple times etc). |
|
@tim-smart The order of The idea being that whoever attaches an error later, has knowledge of what came before, and so can make an informed decision about how to override previous encoders. To me, it makes more intuitive sense than the old behaviour where old codecs override newer ones. For example, now if I do That's essentially what I'm not sure about the mechanism by which this no longer seems to be the case on e8bd5b8 though. If it's intentional, then we can close this PR. I'll still work on a separate PR to make the default HttpApiDecodeError completely replaceable in the spec though, so it also doesn't show up in Swagger docs and whatnot. |
I don't think so, as a "catch-all" schema should be processed last, after all the previous schemas. Adding the system level schemas could probably be done last however. |
|
What about a |
|
@gcanti @tim-smart I opened an alternative PR a few weeks back that actually addresses the issue more directly and without drawbacks as far as I can see: #5654 |
Type
Description
HttpApiSchema.UnionUnifycreates a union whose members are listed in the order of[...firstArgUnionMembers, ...secondArgUnionMembers]. When encoding a union, Schema will try each member in the order that they appear.So the
addErrormethod on each of the HttpApi* classes, by passing the user-provided schema as a second argument, causes older schemas that encompass newer schemas to take precedence over the newer ones.By flipping the argument order, users are now able to use
addErrorwith a schema that (partly) encompasses an older one, to override the encoding behaviour for values conforming to those schemas.Related