From 348dc187ccaacc894c2ddf8ff4e71d43cfb362e6 Mon Sep 17 00:00:00 2001 From: Zai Shi Date: Fri, 14 Jun 2024 13:57:19 +0200 Subject: [PATCH] fixed doc gen bug --- docs/fern/openapi/client.yaml | 4 ++++ docs/fern/openapi/server.yaml | 17 +++++++++++------ packages/stack-server/src/lib/openapi.tsx | 18 ++++++++++++------ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/docs/fern/openapi/client.yaml b/docs/fern/openapi/client.yaml index bbd86b80b..beca48cdf 100644 --- a/docs/fern/openapi/client.yaml +++ b/docs/fern/openapi/client.yaml @@ -78,6 +78,8 @@ paths: type: string description: Selected team ID example: team-id + selectedTeam: + type: object required: - projectId - id @@ -186,6 +188,8 @@ paths: type: string description: Selected team ID example: team-id + selectedTeam: + type: object required: - projectId - id diff --git a/docs/fern/openapi/server.yaml b/docs/fern/openapi/server.yaml index 1378eef75..f4ce5532a 100644 --- a/docs/fern/openapi/server.yaml +++ b/docs/fern/openapi/server.yaml @@ -11,12 +11,7 @@ paths: summary: List users description: List all the users in the project operationId: listUsers - parameters: - - name: userId - in: path - schema: - type: string - required: false + parameters: [] responses: "200": description: Successful response @@ -47,6 +42,8 @@ paths: type: string description: User display name example: John Doe + selectedTeam: + type: object selectedTeamId: type: string description: Selected team ID @@ -131,6 +128,8 @@ paths: type: string description: User display name example: John Doe + selectedTeam: + type: object selectedTeamId: type: string description: Selected team ID @@ -269,6 +268,8 @@ paths: type: string description: User display name example: John Doe + selectedTeam: + type: object selectedTeamId: type: string description: Selected team ID @@ -373,6 +374,8 @@ paths: type: string description: User display name example: John Doe + selectedTeam: + type: object selectedTeamId: type: string description: Selected team ID @@ -506,6 +509,8 @@ paths: type: string description: User display name example: John Doe + selectedTeam: + type: object selectedTeamId: type: string description: Selected team ID diff --git a/packages/stack-server/src/lib/openapi.tsx b/packages/stack-server/src/lib/openapi.tsx index 51a981070..c2dadfa44 100644 --- a/packages/stack-server/src/lib/openapi.tsx +++ b/packages/stack-server/src/lib/openapi.tsx @@ -43,7 +43,7 @@ export function parseOpenAPI(options: { const handlers = isRouteHandler(endpoint.handler) ? [endpoint.handler] : crudHandlerToArray(endpoint.handler); for (const handler of handlers) { - const parsed = parseRouteHandler({ handler, audience: options.audience, tags: endpoint.tags }); + const parsed = parseRouteHandler({ handler, audience: options.audience, tags: endpoint.tags, path: endpoint.path }); result.paths[endpoint.path] = { ...result.paths[endpoint.path], ...parsed }; } } @@ -71,13 +71,12 @@ function undefinedIfMixed(value: yup.SchemaDescription | undefined): yup.SchemaD function parseRouteHandler(options: { handler: RouteHandler, + path: string, audience: 'client' | 'server' | 'admin', tags?: string[], }) { let schema = options.handler.schemas.get(options.audience); if (!schema) return {}; - - // const metadata = endpointMetadataSchema.validateSync(serverSchema.request.describe().meta); if (!schema.metadata) throw new Error('Missing metadata'); let result: any = {}; @@ -88,6 +87,7 @@ function parseRouteHandler(options: { result[method.toLowerCase()] = { ...parseSchema({ metadata: schema.metadata, + path: options.path, pathDesc: undefinedIfMixed(requestFields.params), parameterDesc: undefinedIfMixed(requestFields.query), requestBodyDesc: undefinedIfMixed(requestFields.body), @@ -132,11 +132,16 @@ function getFieldSchema(field: yup.SchemaFieldDescription): { type: string, item return schema; } -function toParameters(description: yup.SchemaDescription, inType: 'query' | 'path' = 'query') { +function toParameters(description: yup.SchemaDescription, path?: string) { + const pathParams: string[] = path ? path.match(/{[^}]+}/g) || [] : []; + return Object.entries((description as any).fields).map(([key, field]) => { + if (path && !pathParams.includes(`{${key}}`)) { + return { schema: null }; + } return { name: key, - in: inType, + in: path ? 'path' : 'query', schema: getFieldSchema(field as any), required: !(field as any).optional && !(field as any).nullable, }; @@ -191,12 +196,13 @@ function toExamples(description: yup.SchemaDescription) { export function parseSchema(options: { metadata: yup.InferType, + path: string, pathDesc?: yup.SchemaDescription, parameterDesc?: yup.SchemaDescription, requestBodyDesc?: yup.SchemaDescription, responseDesc?: yup.SchemaDescription, }) { - const pathParameters = options.pathDesc ? toParameters(options.pathDesc, 'path') : []; + const pathParameters = options.pathDesc ? toParameters(options.pathDesc, options.path) : []; const queryParameters = options.parameterDesc ? toParameters(options.parameterDesc) : []; const responseSchema = options.responseDesc ? toSchema(options.responseDesc) : {}; const responseRequired = options.responseDesc ? toRequired(options.responseDesc) : [];