-
-
Notifications
You must be signed in to change notification settings - Fork 133
feat(server): add OpenAPI spec generation for RESTful API #2498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
59e3c3a
feat(server): add OpenAPI spec generation with filter kind slicing su…
ymc9 ff5735f
undo file renaming
ymc9 ab81f9d
fix build
ymc9 8eea293
fix build
ymc9 1853a04
feat(server): use @@meta("description") as model schema description i…
ymc9 ba315ea
remove unused test baseline files
ymc9 76b634c
feat(server): add operation slicing, meta descriptions, and queryOpti…
ymc9 4e7cad4
fix(server): hide relations to excluded models in REST OpenAPI spec a…
ymc9 ac2e936
fix(server): use proper OpenAPI Response Objects for error responses …
ymc9 913798e
fix: precise select/include/omit schemas for rpc spec
ymc9 3983beb
fix: detailed spec for count/aggregate/groupby
ymc9 6ab9b8a
refactor: consolidate OpenAPI baseline tests and add spec validation
ymc9 2a2fcd7
refactor: extract shared filter schemas in RPC OpenAPI spec generation
ymc9 63a5e4e
refactor: temporarily remove rpc schema generator
ymc9 873b4e9
fix build
ymc9 6a05ef0
fix: structure REST OpenAPI model schema with attributes/relationships
ymc9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| import z from 'zod'; | ||
|
|
||
| export const loggerSchema = z.union([z.enum(['debug', 'info', 'warn', 'error']).array(), z.function()]); | ||
|
|
||
| const fieldSlicingSchema = z.looseObject({ | ||
| includedFilterKinds: z.string().array().optional(), | ||
| excludedFilterKinds: z.string().array().optional(), | ||
| }); | ||
|
|
||
| const modelSlicingSchema = z.looseObject({ | ||
| includedOperations: z.array(z.string()).optional(), | ||
| excludedOperations: z.array(z.string()).optional(), | ||
| fields: z.record(z.string(), fieldSlicingSchema).optional(), | ||
| }); | ||
|
|
||
| const slicingSchema = z.looseObject({ | ||
| includedModels: z.array(z.string()).optional(), | ||
| excludedModels: z.array(z.string()).optional(), | ||
| models: z.record(z.string(), modelSlicingSchema).optional(), | ||
| includedProcedures: z.array(z.string()).optional(), | ||
| excludedProcedures: z.array(z.string()).optional(), | ||
| }); | ||
|
|
||
| export const queryOptionsSchema = z.looseObject({ | ||
| omit: z.record(z.string(), z.record(z.string(), z.boolean())).optional(), | ||
| slicing: slicingSchema.optional(), | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { lowerCaseFirst } from '@zenstackhq/common-helpers'; | ||
| import type { QueryOptions } from '@zenstackhq/orm'; | ||
| import { ExpressionUtils, type AttributeApplication, type SchemaDef } from '@zenstackhq/orm/schema'; | ||
|
|
||
| /** | ||
| * Checks if a model is included based on slicing options. | ||
| */ | ||
| export function isModelIncluded(modelName: string, queryOptions?: QueryOptions<any>): boolean { | ||
| const slicing = queryOptions?.slicing; | ||
| if (!slicing) return true; | ||
|
|
||
| const excluded = slicing.excludedModels as readonly string[] | undefined; | ||
| if (excluded?.includes(modelName)) return false; | ||
|
|
||
| const included = slicing.includedModels as readonly string[] | undefined; | ||
| if (included && !included.includes(modelName)) return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a CRUD operation is included for a model based on slicing options. | ||
| */ | ||
| export function isOperationIncluded(modelName: string, op: string, queryOptions?: QueryOptions<any>): boolean { | ||
| const slicing = queryOptions?.slicing; | ||
| if (!slicing?.models) return true; | ||
|
|
||
| const modelKey = lowerCaseFirst(modelName); | ||
| const modelSlicing = (slicing.models as Record<string, any>)[modelKey] ?? (slicing.models as any).$all; | ||
| if (!modelSlicing) return true; | ||
|
|
||
| const excluded = modelSlicing.excludedOperations as readonly string[] | undefined; | ||
| if (excluded?.includes(op)) return false; | ||
|
|
||
| const included = modelSlicing.includedOperations as readonly string[] | undefined; | ||
| if (included && !included.includes(op)) return false; | ||
|
|
||
| return true; | ||
| } | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Checks if a procedure is included based on slicing options. | ||
| */ | ||
| export function isProcedureIncluded(procName: string, queryOptions?: QueryOptions<any>): boolean { | ||
| const slicing = queryOptions?.slicing; | ||
| if (!slicing) return true; | ||
|
|
||
| const excluded = slicing.excludedProcedures as readonly string[] | undefined; | ||
| if (excluded?.includes(procName)) return false; | ||
|
|
||
| const included = slicing.includedProcedures as readonly string[] | undefined; | ||
| if (included && !included.includes(procName)) return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a field should be omitted from the output schema based on queryOptions.omit. | ||
| */ | ||
| export function isFieldOmitted(modelName: string, fieldName: string, queryOptions?: QueryOptions<any>): boolean { | ||
| const omit = queryOptions?.omit as Record<string, Record<string, boolean>> | undefined; | ||
| return omit?.[modelName]?.[fieldName] === true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the list of model names from the schema that pass the slicing filter. | ||
| */ | ||
| export function getIncludedModels(schema: SchemaDef, queryOptions?: QueryOptions<any>): string[] { | ||
| return Object.keys(schema.models).filter((name) => isModelIncluded(name, queryOptions)); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a filter kind is allowed for a specific field based on slicing options. | ||
| */ | ||
| export function isFilterKindIncluded( | ||
| modelName: string, | ||
| fieldName: string, | ||
| filterKind: string, | ||
| queryOptions?: QueryOptions<any>, | ||
| ): boolean { | ||
| const slicing = queryOptions?.slicing; | ||
| if (!slicing?.models) return true; | ||
|
|
||
| const modelKey = lowerCaseFirst(modelName); | ||
| const modelSlicing = (slicing.models as Record<string, any>)[modelKey] ?? (slicing.models as any).$all; | ||
| if (!modelSlicing?.fields) return true; | ||
|
|
||
| const fieldSlicing = modelSlicing.fields[fieldName] ?? modelSlicing.fields.$all; | ||
| if (!fieldSlicing) return true; | ||
|
|
||
| const excluded = fieldSlicing.excludedFilterKinds as readonly string[] | undefined; | ||
| if (excluded?.includes(filterKind)) return false; | ||
|
|
||
| const included = fieldSlicing.includedFilterKinds as readonly string[] | undefined; | ||
| if (included && !included.includes(filterKind)) return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts a "description" from `@@meta("description", "...")` or `@meta("description", "...")` attributes. | ||
| */ | ||
| export function getMetaDescription(attributes: readonly AttributeApplication[] | undefined): string | undefined { | ||
| if (!attributes) return undefined; | ||
| for (const attr of attributes) { | ||
| if (attr.name !== '@meta' && attr.name !== '@@meta') continue; | ||
| const nameArg = attr.args?.find((a) => a.name === 'name'); | ||
| if (!nameArg || ExpressionUtils.getLiteralValue(nameArg.value) !== 'description') continue; | ||
| const valueArg = attr.args?.find((a) => a.name === 'value'); | ||
| if (valueArg) { | ||
| return ExpressionUtils.getLiteralValue(valueArg.value) as string | undefined; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { QueryOptions } from '@zenstackhq/orm'; | ||
| import type { SchemaDef } from '@zenstackhq/orm/schema'; | ||
| import type { OpenAPIV3_1 } from 'openapi-types'; | ||
|
|
||
| export type CommonHandlerOptions<Schema extends SchemaDef> = { | ||
| /** Query options that affect the behavior of the OpenAPI provider. */ | ||
| queryOptions?: QueryOptions<Schema>; | ||
| }; | ||
|
|
||
| export type OpenApiSpecOptions = { | ||
| /** Spec title. Defaults to 'ZenStack Generated API' */ | ||
| title?: string; | ||
|
|
||
| /** Spec version. Defaults to '1.0.0' */ | ||
| version?: string; | ||
|
|
||
| /** Spec description. */ | ||
| description?: string; | ||
|
|
||
| /** Spec summary. */ | ||
| summary?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Interface for generating OpenAPI specifications. | ||
| */ | ||
| export interface OpenApiSpecGenerator { | ||
| /** | ||
| * Generates an OpenAPI v3.1 specification document. | ||
| */ | ||
| generateSpec(options?: OpenApiSpecOptions): Promise<OpenAPIV3_1.Document>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export type { OpenApiSpecGenerator, OpenApiSpecOptions } from './common/types'; | ||
| export { RestApiHandler, type RestApiHandlerOptions } from './rest'; | ||
| export { RPCApiHandler, type RPCApiHandlerOptions } from './rpc'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.