Skip to content

Commit 88f24aa

Browse files
authored
use schema class identifier annotation for OpenApi ref identifier (#643)
* use schema class identifier annotation for openapi ref identifier * update dependencies
1 parent bc66b1e commit 88f24aa

File tree

7 files changed

+188
-99
lines changed

7 files changed

+188
-99
lines changed

.changeset/odd-kiwis-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect-http": patch
3+
---
4+
5+
Use an identifier annotation of a class schema as an OpenApi reference identifier.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@types/swagger-ui-dist": "^3.30.5",
3737
"@typescript-eslint/eslint-plugin": "^7.18.0",
3838
"@typescript-eslint/parser": "^7.18.0",
39-
"@vitest/coverage-v8": "^2.0.4",
39+
"@vitest/coverage-v8": "^2.0.5",
4040
"babel-plugin-annotate-pure-calls": "^0.4.0",
4141
"eslint": "^8.57.0",
4242
"eslint-import-resolver-typescript": "^3.6.1",
@@ -49,9 +49,9 @@
4949
"madge": "^7.0.0",
5050
"prettier": "^3.3.3",
5151
"rimraf": "^6.0.1",
52-
"tsx": "^4.16.2",
52+
"tsx": "^4.16.3",
5353
"typescript": "^5.5.4",
54-
"vitest": "^2.0.4"
54+
"vitest": "^2.0.5"
5555
},
5656
"pnpm": {
5757
"patchedDependencies": {

packages/effect-http-node/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
"effect-http": "workspace:^"
4040
},
4141
"devDependencies": {
42-
"@effect/platform": "^0.61.0",
43-
"@effect/platform-bun": "^0.41.0",
44-
"@effect/platform-node": "^0.56.0",
42+
"@effect/platform": "^0.61.1",
43+
"@effect/platform-bun": "^0.41.1",
44+
"@effect/platform-node": "^0.56.1",
4545
"@effect/schema": "^0.70.0",
4646
"@types/node": "^22.0.0",
4747
"effect": "^3.6.0",

packages/effect-http/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"devDependencies": {
3939
"@apidevtools/swagger-parser": "^10.1.0",
40-
"@effect/platform": "^0.61.0",
40+
"@effect/platform": "^0.61.1",
4141
"@effect/schema": "^0.70.0",
4242
"effect": "^3.6.0"
4343
}

packages/effect-http/src/internal/open-api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,17 @@ export const openAPISchemaForAst = (
426426
componentSchemaCallback(identifier, ast)
427427
return reference(identifier)
428428
}
429+
430+
if (ast._tag === "Transformation") {
431+
const identifierTo = Option.getOrUndefined(AST.getIdentifierAnnotation(ast.to))
432+
const identifierFrom = Option.getOrUndefined(AST.getIdentifierAnnotation(ast.from))
433+
434+
if (identifierTo !== undefined && identifierFrom === undefined && componentSchemaCallback) {
435+
componentSchemaCallback(identifierTo, ast.from)
436+
return reference(identifierTo)
437+
}
438+
}
439+
429440
return null
430441
}
431442

packages/effect-http/test/openapi.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,4 +1432,61 @@ describe("component schema and reference", () => {
14321432
}
14331433
})
14341434
})
1435+
1436+
it("reference with class schema", async () => {
1437+
class ReferencedType extends Schema.Class<ReferencedType>("ReferencedType")(
1438+
Schema.Struct({ something: Schema.String })
1439+
) {}
1440+
1441+
const api = Api.make({ title: "test", version: "0.1" }).pipe(
1442+
Api.addEndpoint(
1443+
Api.post("getPet", "/pet").pipe(Api.setResponseBody(ReferencedType))
1444+
)
1445+
)
1446+
const spec = OpenApi.make(api)
1447+
1448+
const openapi = {
1449+
openapi: "3.0.3",
1450+
info: { title: "test", version: "0.1" },
1451+
tags: [{ "name": "default" }],
1452+
paths: {
1453+
"/pet": {
1454+
post: {
1455+
tags: ["default"],
1456+
operationId: "getPet",
1457+
responses: {
1458+
200: {
1459+
content: {
1460+
"application/json": {
1461+
schema: {
1462+
$ref: "#/components/schemas/ReferencedType"
1463+
}
1464+
}
1465+
},
1466+
description: "Response 200"
1467+
}
1468+
}
1469+
}
1470+
}
1471+
},
1472+
components: {
1473+
schemas: {
1474+
ReferencedType: {
1475+
properties: {
1476+
something: {
1477+
description: "a string",
1478+
type: "string"
1479+
}
1480+
},
1481+
required: ["something"],
1482+
type: "object"
1483+
}
1484+
}
1485+
}
1486+
}
1487+
expect(spec).toStrictEqual(openapi)
1488+
1489+
// @ts-expect-error
1490+
SwaggerParser.validate(spec)
1491+
})
14351492
})

0 commit comments

Comments
 (0)