Skip to content

Commit

Permalink
fixed doc gen bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fomalhautb committed Jun 14, 2024
1 parent 0694f09 commit 348dc18
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/fern/openapi/client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ paths:
type: string
description: Selected team ID
example: team-id
selectedTeam:
type: object
required:
- projectId
- id
Expand Down Expand Up @@ -186,6 +188,8 @@ paths:
type: string
description: Selected team ID
example: team-id
selectedTeam:
type: object
required:
- projectId
- id
Expand Down
17 changes: 11 additions & 6 deletions docs/fern/openapi/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +42,8 @@ paths:
type: string
description: User display name
example: John Doe
selectedTeam:
type: object
selectedTeamId:
type: string
description: Selected team ID
Expand Down Expand Up @@ -131,6 +128,8 @@ paths:
type: string
description: User display name
example: John Doe
selectedTeam:
type: object
selectedTeamId:
type: string
description: Selected team ID
Expand Down Expand Up @@ -269,6 +268,8 @@ paths:
type: string
description: User display name
example: John Doe
selectedTeam:
type: object
selectedTeamId:
type: string
description: Selected team ID
Expand Down Expand Up @@ -373,6 +374,8 @@ paths:
type: string
description: User display name
example: John Doe
selectedTeam:
type: object
selectedTeamId:
type: string
description: Selected team ID
Expand Down Expand Up @@ -506,6 +509,8 @@ paths:
type: string
description: User display name
example: John Doe
selectedTeam:
type: object
selectedTeamId:
type: string
description: Selected team ID
Expand Down
18 changes: 12 additions & 6 deletions packages/stack-server/src/lib/openapi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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),
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -191,12 +196,13 @@ function toExamples(description: yup.SchemaDescription) {

export function parseSchema(options: {
metadata: yup.InferType<typeof endpointMetadataSchema>,
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) : [];
Expand Down

0 comments on commit 348dc18

Please sign in to comment.