Skip to content

Commit

Permalink
generate refs for OpenApi
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Oct 18, 2024
1 parent 5d55f61 commit c13a56e
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions packages/platform/src/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,33 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
},
security: []
}
const jsonSchemaCache = new Map<AST.AST, JsonSchema.JsonSchema>()
const identifiers = new Map<AST.AST, number>()
function makeJsonSchema(schema: Schema.Schema.All): JsonSchema.JsonSchema {
const ast = schema.ast
if (jsonSchemaCache.has(ast)) {
return jsonSchemaCache.get(ast)!
}
const annotations = {
...(ast._tag === "Transformation" ? ast.to.annotations : {}),
...ast.annotations
}
let identifier = annotations[AST.IdentifierAnnotationId] as string | undefined
if (identifier) {
const count = identifiers.get(ast) ?? 0
if (count > 0) {
identifier = `${identifier}${count}`
}
identifiers.set(ast, count + 1)
spec.components!.schemas![identifier] = JsonSchema.make(schema as any)
const ref = { $ref: `#/components/schemas/${identifier}` }
jsonSchemaCache.set(ast, ref)
return ref
}
const jsonSchema = JsonSchema.make(schema as any)
jsonSchemaCache.set(ast, jsonSchema)
return jsonSchema
}
function registerSecurity(
tag: HttpApiMiddleware.TagClassSecurityAny,
name: string,
Expand Down Expand Up @@ -210,7 +237,7 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
op.requestBody = {
content: {
[HttpApiSchema.getMultipart(schema.ast) ? "multipart/form-data" : "application/json"]: {
schema: JsonSchema.make(schema)
schema: makeJsonSchema(schema)
}
},
required: true
Expand All @@ -227,14 +254,14 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
Option.map((ast) => {
op.responses![status].content = {
"application/json": {
schema: JsonSchema.make(Schema.make(ast))
schema: makeJsonSchema(Schema.make(ast))
}
}
})
)
}
if (Option.isSome(endpoint.pathSchema)) {
const schema = JsonSchema.make(endpoint.pathSchema.value) as JsonSchema.Object
const schema = makeJsonSchema(endpoint.pathSchema.value) as JsonSchema.Object
if ("properties" in schema) {
Object.entries(schema.properties).forEach(([name, jsonSchema]) => {
op.parameters!.push({
Expand All @@ -247,7 +274,7 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
}
}
if (!HttpMethod.hasBody(endpoint.method) && Option.isSome(endpoint.payloadSchema)) {
const schema = JsonSchema.make(endpoint.payloadSchema.value) as JsonSchema.Object
const schema = makeJsonSchema(endpoint.payloadSchema.value) as JsonSchema.Object
if ("properties" in schema) {
Object.entries(schema.properties).forEach(([name, jsonSchema]) => {
op.parameters!.push({
Expand All @@ -260,7 +287,7 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
}
}
if (Option.isSome(endpoint.headersSchema)) {
const schema = JsonSchema.make(endpoint.headersSchema.value) as JsonSchema.Object
const schema = makeJsonSchema(endpoint.headersSchema.value) as JsonSchema.Object
if ("properties" in schema) {
Object.entries(schema.properties).forEach(([name, jsonSchema]) => {
op.parameters!.push({
Expand All @@ -273,7 +300,7 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
}
}
if (Option.isSome(endpoint.urlParamsSchema)) {
const schema = JsonSchema.make(endpoint.urlParamsSchema.value) as JsonSchema.Object
const schema = makeJsonSchema(endpoint.urlParamsSchema.value) as JsonSchema.Object
if ("properties" in schema) {
Object.entries(schema.properties).forEach(([name, jsonSchema]) => {
op.parameters!.push({
Expand All @@ -295,7 +322,7 @@ export const fromApi = <A extends HttpApi.HttpApi.Any>(self: A): OpenAPISpec =>
Option.map((ast) => {
op.responses![status].content = {
"application/json": {
schema: JsonSchema.make(Schema.make(ast))
schema: makeJsonSchema(Schema.make(ast))
}
}
})
Expand Down

0 comments on commit c13a56e

Please sign in to comment.