Skip to content

Commit

Permalink
ensure HttpApi preserves referential equality of error schemas (#4145)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Dec 15, 2024
1 parent 9e40ece commit 883639c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-dogs-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

ensure HttpApi preserves referential equality of error schemas
1 change: 1 addition & 0 deletions packages/platform-node/test/HttpApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class UsersApi extends HttpApiGroup.make("users")
}))
.addSuccess(User)
.addError(UserError)
.addError(UserError) // ensure errors are deduplicated
)
.add(
HttpApiEndpoint.get("list")`/`
Expand Down
13 changes: 4 additions & 9 deletions packages/platform/src/HttpApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ const Proto = {
groups: this.groups,
errorSchema: HttpApiSchema.UnionUnify(
this.errorSchema,
schema.annotations(HttpApiSchema.annotations({
status: annotations?.status ?? HttpApiSchema.getStatusError(schema)
}))
annotations?.status
? schema.annotations(HttpApiSchema.annotations({ status: annotations.status }))
: schema
),
annotations: this.annotations,
middlewares: this.middlewares
Expand All @@ -207,12 +207,7 @@ const Proto = {
return makeProto({
identifier: this.identifier,
groups: this.groups,
errorSchema: HttpApiSchema.UnionUnify(
this.errorSchema,
tag.failure.annotations(HttpApiSchema.annotations({
status: HttpApiSchema.getStatusError(tag.failure)
}) as any)
),
errorSchema: HttpApiSchema.UnionUnify(this.errorSchema, tag.failure),
annotations: this.annotations,
middlewares: new Set([...this.middlewares, tag])
})
Expand Down
23 changes: 5 additions & 18 deletions packages/platform/src/HttpApiEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,9 @@ const Proto = {
schema: Schema.Schema.Any,
annotations?: { readonly status?: number }
) {
schema = schema.pipe(
Schema.annotations(HttpApiSchema.annotations({
status: annotations?.status ?? HttpApiSchema.getStatusSuccess(schema)
}))
)
schema = annotations?.status ?
schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) :
schema
return makeProto({
...this,
successSchema: this.successSchema === HttpApiSchema.NoContent ?
Expand All @@ -717,11 +715,7 @@ const Proto = {
...this,
errorSchema: HttpApiSchema.UnionUnify(
this.errorSchema,
schema.pipe(
Schema.annotations(HttpApiSchema.annotations({
status: annotations?.status ?? HttpApiSchema.getStatusError(schema)
}))
)
annotations?.status ? schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) : schema
)
})
},
Expand Down Expand Up @@ -758,14 +752,7 @@ const Proto = {
middleware(this: HttpApiEndpoint.AnyWithProps, middleware: HttpApiMiddleware.TagClassAny) {
return makeProto({
...this,
errorSchema: HttpApiSchema.UnionUnify(
this.errorSchema,
middleware.failure.pipe(
Schema.annotations(HttpApiSchema.annotations({
status: HttpApiSchema.getStatusError(middleware.failure)
}))
)
),
errorSchema: HttpApiSchema.UnionUnify(this.errorSchema, middleware.failure),
middlewares: new Set([...this.middlewares, middleware])
})
},
Expand Down
11 changes: 2 additions & 9 deletions packages/platform/src/HttpApiGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ const Proto = {
endpoints: this.endpoints,
errorSchema: HttpApiSchema.UnionUnify(
this.errorSchema,
schema.annotations(HttpApiSchema.annotations({
status: annotations?.status ?? HttpApiSchema.getStatusError(schema)
}))
annotations?.status ? schema.annotations(HttpApiSchema.annotations({ status: annotations.status })) : schema
),
annotations: this.annotations,
middlewares: this.middlewares
Expand All @@ -332,12 +330,7 @@ const Proto = {
identifier: this.identifier,
topLevel: this.topLevel,
endpoints: this.endpoints,
errorSchema: HttpApiSchema.UnionUnify(
this.errorSchema,
middleware.failure.annotations(HttpApiSchema.annotations({
status: HttpApiSchema.getStatusError(middleware.failure)
}) as any)
),
errorSchema: HttpApiSchema.UnionUnify(this.errorSchema, middleware.failure),
annotations: this.annotations,
middlewares: new Set([...this.middlewares, middleware])
})
Expand Down

0 comments on commit 883639c

Please sign in to comment.