feat(codegen): migrate to oazapfts v7 #5228
feat(codegen): migrate to oazapfts v7 #5228Suto-Michimasa wants to merge 16 commits intoreduxjs:masterfrom
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 74c035a:
|
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| export function isReference( | ||
| obj: unknown | ||
| ): obj is OpenAPIV3.ReferenceObject { | ||
| return typeof obj === 'object' && obj !== null && '$ref' in (obj as any); | ||
| } | ||
|
|
||
| export function getReferenceName( | ||
| obj: unknown | ||
| ): string | undefined { | ||
| if (isReference(obj)) { | ||
| return obj.$ref.split('/').pop(); | ||
| } | ||
| } |
There was a problem hiding this comment.
oazapfts/oazapfts#826 creates a @oazapfts/resolve package exposing these and other schema related helpers.
| function getTypeFromResponse( | ||
| ctx: OazapftsContext, | ||
| response: OpenAPIV3.ResponseObject | OpenAPIV3.ReferenceObject, | ||
| onlyMode?: OnlyMode | ||
| ): ts.TypeNode { | ||
| const resolved = resolve(response, ctx); | ||
| if (!resolved.content) return keywordType.void; | ||
| const schema = getSchemaFromContent(resolved.content); | ||
| return getTypeFromSchema(onlyMode ? withMode(ctx, onlyMode) : ctx, schema); | ||
| } |
There was a problem hiding this comment.
7.5.0-alpha.5 exposes this under oazapfts/generate
| return res; | ||
| } | ||
|
|
||
| function preprocessComponents(ctx: OazapftsContext): void { |
There was a problem hiding this comment.
7.5.0-alpha.5 does this within createContext
Xiphe
left a comment
There was a problem hiding this comment.
LGTM! Will make sure to release 7.5 stable in the next 2 days
|
@Xiphe |
|
Hey @Suto-Michimasa Sorry I had planned to get it out before a vacation but eventually did not come around to it. |
In order to unblock Redux Toolkit from upgrading to the latest oazapfts, this focuses on making the `oazapfts/generate` submodule a reliable public integration surface for schema/type-generation helpers outside the default codegen pipeline, with follow-up fixes around exported helpers and preprocess flow needed by the RTK migration path. ref: #830 ref: reduxjs/redux-toolkit#5228 --- * feat(codegen): add filterEndpoint plugin hook in order to allow plugins to customize which endpoints are being generated and which are not. ref https://github.com/reduxjs/redux-toolkit/issues/5225\#issuecomment-3938309171 * test(codegen): add test for include/exclude options * refactor(codegen): move include/exclude option handling into plugin dogfood, mjummy * refactor(codegen): change generateMethod hook from waterfall to bail In order to allow plugins to fully own method generation and skip the default implementation entirely, `generateMethod` is now a bail hook (first-return-wins) instead of a waterfall. The default oazapfts method generation has been moved into a LAZY internal plugin (`defaultGenerateMethodPlugin`) and serves as the fallback when no other plugin claims the endpoint. Plugins return `undefined` to delegate, or an array to claim generation. ref: reduxjs/redux-toolkit#5225 (comment) Co-authored-by: Cursor <cursoragent@cursor.com> * feat(codegen): add refineMethod waterfall hook for per-endpoint transforms In order to allow plugins to modify, rename, annotate or filter generated methods without claiming full generation ownership, a new `refineMethod` waterfall hook is introduced. It runs after `generateMethod` (bail) for each endpoint and receives the generated `FunctionDeclaration[]` to transform and return. This also extracts the shared endpoint argument shape into `UNSTABLE_EndpointHookArgs` to reduce type duplication across hooks. ref: reduxjs/redux-toolkit#5225 (comment) Co-authored-by: Cursor <cursoragent@cursor.com> * feat(codegen): add composeSource/refineSource plugin hooks In order to allow plugins to fully customize (or replace) top-level source statement composition, which was previously hardcoded in generateApi. The default composition is now provided by an internal LAZY plugin (defaultComposeSourcePlugin), so user plugins can bail composeSource and take full control. Banner insertion and createSourceFile construction remain fixed in core flow. ref: #830 (comment) Co-authored-by: Cursor <cursoragent@cursor.com> * refactor(codegen): move method name de-collision into generation time In order to allow the generate/refine method hooks to accept ts.Statement[] instead of ts.FunctionDeclaration[], name collision handling must happen at name-reservation time rather than as a post-processing step in defaultComposeSource. operationNames (Map<string, number>) is added to OazapftsContext and passed to getOperationNames, which now reserves each name as it is computed. The reserveOperationName helper ensures generated suffixes never collide with other operation names already in the map (including explicitly-suffixed operationIds like "hello1" or "hello2"). defaultComposeSource no longer calls dedupeMethodNames. ref: #830 (comment) Co-authored-by: Cursor <cursoragent@cursor.com> * refactor(codegen): loosen generateMethod/refineMethod to ts.Statement[] In order to allow plugins to return arbitrary top-level statements from generateMethod, not just function declarations, so that custom output formats are no longer constrained to ts.FunctionDeclaration. ref: #830 (comment) Co-authored-by: Cursor <cursoragent@cursor.com> * refactor(codegen): rename getTypeForSchema to getTypeFromSchema In order to align the file name with the exported function name `getTypeFromSchema`, making imports and navigation more consistent. * docs(codegen): mark supportDeepObjects as deprecated In order to communicate to future contributors that supportDeepObjects is no longer in use since v7.0 dropped support for the Swagger-to-OpenAPI conversion path that relied on it. * refactor(codegen): move getTypeFromSchema into its own top-level module In order to make the schema type resolution logic available outside of the generate pipeline and to establish clearer module boundaries within the codegen package, the getTypeFromSchema cluster of files is extracted from src/generate/ into a dedicated src/getTypeFromSchema/ directory. A barrel index.ts re-exports getTypeFromSchema as the public entry point. Import paths in the generate/ consumers are updated accordingly. ref: #830 (comment) * refactor(codegen): propagate onlyMode via context instead of function params In order to avoid threading the `onlyMode` argument through every level of the type generation call stack, it is now stored as `mode` on a derived context object created via the new `withMode()` helper. `withMode()` does a shallow clone of the context, so all mutable shared state (aliases, refs, etc.) is still shared across calls while each frame carries its own mode snapshot — without needing save/restore logic. Entry points (`getRefAlias`, `generateClientMethod`, `getTypeFromResponses`) now derive a mode-scoped context. Intermediate functions (`getTypeFromSchema`, `getBaseTypeFromSchema`, `getUnionType`, `getTypeFromProperties`) drop the `onlyMode` parameter and read `ctx.mode` directly. Also fixes a long-standing bug in `generateApi` where the resolved schema object was incorrectly passed as the `ignoreDiscriminator?: boolean` argument to `getRefAlias`, causing `allSchemas` to generate base type names suffixed with "Base" instead of the correct names. Co-authored-by: Cursor <cursoragent@cursor.com> * feat(codegen): expose generate subpath api with full tests In order to make the generate module a reliable public entrypoint, this change stabilizes its exports and adds direct tests for exported schema and generation helpers. ref: #830 (comment) * feat(codegen): stabilize plugin api naming In order to make plugin integration a stable part of the codegen surface, this removes the UNSTABLE_ prefixes from plugin types, helpers, and options. Co-authored-by: Cursor <cursoragent@cursor.com> * style: apply prettier * fix(codegen): build generate export ref https://github.com/oazapfts/oazapfts/pull/830\#pullrequestreview-3858005073 * fix(codegen): guard against undefined deprecatedLegacyName In order to prevent returning `undefined` from `getOperationName` when `DEPRECATED_legacyName` flag is set but no legacy name exists in the computed names object. * feat(codegen): export getTypeFromResponse as part of public API In order to allow consumers to resolve a single response object to its TypeScript type node directly, without needing the full responses map. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(codegen): move preprocessComponents call into createContext In order to ensure component preprocessing is part of context initialization rather than a separate concern in the API generation pipeline, the call is moved from generateApi into createContext. ref: #830 (comment) * Revert "feat(codegen): stabilize plugin api naming" This reverts commit 8addaa8. * Revert "fix(codegen): move preprocessComponents call into createContext" This reverts commit 597aa78. * feat(codegen): move preprocessComponents to generate and export from oazapfts/generate In order to allow consumers (e.g. RTK codegen) that use createContext + getTypeFromSchema directly without generateApi to call preprocessComponents themselves when needed. Co-locates the logic with the generate pipeline and adds comprehensive tests. ref: #830 (comment) * style: apply prettier --------- Co-authored-by: Cursor <cursoragent@cursor.com>
- Updated oazapfts dependency from v6.4.0 to v7.3.0 in package.json and yarn.lock. - Introduced oazapfts-compat.ts to adapt to breaking changes in oazapfts v7, replacing the ApiGenerator with OazapftsAdapter. - Refactored generate.ts to utilize the new compatibility layer. - Updated TypeScript version handling in index.ts for compatibility with oazapfts v7. - Adjusted type generation logic to ensure backward compatibility with existing code. - Fixed optional properties in generated types to be required where necessary in test snapshots.
- Updated oazapfts dependency from v7.3.0 to v7.0.0 in both package.json and yarn.lock to ensure compatibility with existing code. - Adjusted related versioning and checksums accordingly.
- Updated oazapfts dependency to v7.5.0-alpha.3 in package.json and yarn.lock. - Introduced new utility functions for operation name generation and deep object support. - Removed the deprecated oazapfts-compat.ts file and refactored generate.ts to utilize the new public API. - Enhanced type generation logic to support TypeScript enums based on the useEnumType option. - Updated tests to reflect changes in type generation and added new tests for enum generation.
- Removed unnecessary mapping logic for discriminator properties in makeDiscriminatorPropertiesRequired function. - Streamlined the process of making discriminator properties required for child schemas. - Improved code readability by eliminating redundant checks and variables.
…an up tests - Eliminated the makeDiscriminatorPropertiesRequired function to simplify schema processing. - Updated tests to remove unnecessary snapshot expectations and adjusted generated types to make allowance_type properties optional. - Improved overall code clarity and maintainability.
- Updated oazapfts dependency to v7.5.0-alpha.5 in package.json and yarn.lock. - Refactored generate.ts to improve type resolution logic and removed unused preprocessing functions. - Enhanced import statements for better clarity and maintainability.
…ersion enforcement - Removed the getOperationName utility function and updated imports to use the version from oazapfts/generate. - Simplified TypeScript version enforcement logic in index.ts for better clarity and reliability. - Enhanced overall code organization by removing unused files and improving import statements.
- Updated the context creation in generate.ts to ensure proper handling of the useEnumType option. - Enhanced code clarity by restructuring the parameters passed to createContext.
- Updated oazapfts dependency to v7.5.0-alpha.6 in package.json and yarn.lock. - Added preprocessComponents function call in generate.ts to improve component processing during API generation. - Refined import statements for better clarity and maintainability.
- Updated oazapfts dependency to v7.5.0 in both package.json and yarn.lock to reflect the latest stable version. - Adjusted checksums in yarn.lock for consistency with the new version.
f10756e to
30e3ac4
Compare
|
I believe the PR description needs to be updated. |
|
@aryaemami59 |
…o pr/5228/feat/codegen-oazapfts-v7-enum-style
…o pr/5228/feat/codegen-oazapfts-v7-enum-style
|
@aryaemami59 |
aryaemami59
left a comment
There was a problem hiding this comment.
To me, everything looks good, but I would feel much more confident merging this if another team member reviewed it.
Summary
Migrate
@rtk-query/codegen-openapifrom oazapfts v6 to v7.5.0 stable.Approach
oazapfts v7 removed the
ApiGeneratorclass that codegen previously depended on. Through collaboration with the oazapfts maintainer (@Xiphe) in oazapfts#830, the necessary APIs were exported as stable subpath entries:oazapfts/context—createContext,withModeoazapfts/generate—getTypeFromSchema,getTypeFromResponse,getResponseType,getSchemaFromContent,getOperationName,preprocessComponents@oazapfts/resolve—resolve,resolveArray,isReference,getReferenceNameThis replaces the previous compatibility adapter with direct API usage. The oazapfts plugin system was also evaluated but the direct API is a better fit since RTK codegen replaces the entire output structure rather than extending it (details).
Changes
generate.ts— ReplaceApiGeneratorusage with direct calls tocreateContext+preprocessComponents+getTypeFromSchemaetc.package.json— Update oazapfts from v6 to v7.5.0generateEndpoints.test.ts— Add discriminator test cases (issue Inheritance in openApi code generation incorrect. #2002, Openapi-codegen doesn’t handle discriminated union properly #3369)oazapfts-compat.tscompatibility adapter (no longer needed)All 74 tests pass with no snapshot diffs.
Related issues
enumStyleoption from oazapfts v7.3.0 #5223 —enumStylefeature request (follow-up PR)@rtk-query/codegen-openapi#5225 — Migration strategy discussion