Skip to content

Commit 8ac3f22

Browse files
committed
Update addError to give precedence to newly added schemas
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.
1 parent f60dce6 commit 8ac3f22

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

packages/platform/src/HttpApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ const Proto = {
184184
identifier: this.identifier,
185185
groups: this.groups,
186186
errorSchema: HttpApiSchema.UnionUnify(
187-
this.errorSchema,
188187
annotations?.status
189188
? schema.annotations(HttpApiSchema.annotations({ status: annotations.status }))
190-
: schema
189+
: schema,
190+
this.errorSchema
191191
),
192192
annotations: this.annotations,
193193
middlewares: this.middlewares

packages/platform/src/HttpApiEndpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,8 @@ const Proto = {
771771
return makeProto({
772772
...this,
773773
errorSchema: HttpApiSchema.UnionUnify(
774+
annotations?.status ? schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) : schema,
774775
this.errorSchema,
775-
annotations?.status ? schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) : schema
776776
)
777777
})
778778
},

packages/platform/src/HttpApiGroup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ const Proto = {
307307
topLevel: this.topLevel,
308308
endpoints: this.endpoints,
309309
errorSchema: HttpApiSchema.UnionUnify(
310+
annotations?.status ? schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) : schema,
310311
this.errorSchema,
311-
annotations?.status ? schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) : schema
312312
),
313313
annotations: this.annotations,
314314
middlewares: this.middlewares

0 commit comments

Comments
 (0)