-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add schemaName support for OpenAPI spec generation #171
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
6 commits
Select commit
Hold shift + click to select a range
308c9ea
feat: add schemaName support for OpenAPI spec generation
andrewzolotukhin 8c4009e
Potential fix for pull request finding
andrewzolotukhin 5c9df84
Potential fix for pull request finding
andrewzolotukhin d4e33dc
Potential fix for pull request finding
andrewzolotukhin 8ebe83c
Potential fix for pull request finding
andrewzolotukhin 80cd028
feat: enhance schema conversion with name resolver support
andrewzolotukhin 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| --- | ||
| '@cleverbrush/schema': minor | ||
| '@cleverbrush/schema-json': minor | ||
| '@cleverbrush/server-openapi': minor | ||
| --- | ||
|
|
||
| Add `.schemaName()` and `components/schemas` `$ref` deduplication | ||
|
|
||
| ### `@cleverbrush/schema` | ||
|
|
||
| New method `.schemaName(name: string)` on every schema builder. Attaches an OpenAPI component name to the schema as runtime metadata (accessible via `.introspect().schemaName`). Has no effect on validation. Follows the same immutable-builder pattern as `.describe()`. | ||
|
|
||
| ```ts | ||
| import { object, string, number } from '@cleverbrush/schema'; | ||
|
|
||
| export const UserSchema = object({ | ||
| id: number(), | ||
| name: string(), | ||
| }).schemaName('User'); | ||
|
|
||
| UserSchema.introspect().schemaName; // 'User' | ||
| ``` | ||
|
|
||
| ### `@cleverbrush/schema-json` | ||
|
|
||
| New optional `nameResolver` option on `toJsonSchema()`. When provided, it is called for every schema node during recursive conversion. Returning a non-null string short-circuits conversion and emits a `$ref` pointer instead: | ||
|
|
||
| ```ts | ||
| toJsonSchema(schema, { | ||
| $schema: false, | ||
| nameResolver: s => s.introspect().schemaName ?? null, | ||
| }); | ||
| ``` | ||
|
|
||
| ### `@cleverbrush/server-openapi` | ||
|
|
||
| Named schemas are now automatically collected into `components.schemas` and referenced via `$ref` throughout the generated OpenAPI document: | ||
|
|
||
| ```ts | ||
| import { object, string, number, array } from '@cleverbrush/schema'; | ||
| import { generateOpenApiSpec } from '@cleverbrush/server-openapi'; | ||
| import { endpoint } from '@cleverbrush/server'; | ||
|
|
||
| export const UserSchema = object({ id: number(), name: string() }) | ||
| .schemaName('User'); | ||
|
|
||
| const GetUser = endpoint.get('/users/:id').returns(UserSchema); | ||
| const ListUsers = endpoint.get('/users').returns(array(UserSchema)); | ||
|
|
||
| // Both operations emit $ref: '#/components/schemas/User' | ||
| // A single components.schemas.User entry holds the full definition. | ||
| generateOpenApiSpec({ registrations: [...], info: { title: 'API', version: '1' } }); | ||
| ``` | ||
|
|
||
| Two different schema instances with the same name throw at generation time — uniqueness is the caller's responsibility. | ||
|
|
||
| New exports from `@cleverbrush/server-openapi`: | ||
| - `SchemaRegistry` — low-level registry class (for custom tooling) | ||
| - `walkSchemas` — recursive schema walker used by the pre-pass |
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
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
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.