Pass OpenAPI document to Context class constructor#1638
Pass OpenAPI document to Context class constructor#1638
Conversation
🦋 Changeset detectedLatest commit: 7545c07 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
pmcelhaney
left a comment
There was a problem hiding this comment.
If the document is changed on disk make sure the object passed to the context object receives the updates.
One way to do that is to remove all top level keys and replace them.
Done in commit 2cfdd55. In |
pmcelhaney
left a comment
There was a problem hiding this comment.
Maybe it would be smarter to assign openApiDocument reference to a Proxy that is read only.
Done in commit 4c25b46. The |
pmcelhaney
left a comment
There was a problem hiding this comment.
The .current property seems unnecessary. The object could just be assigned to openApiDocumentRef.
I don't understand why you wouldn't update the openApiDocument if it wasn't assigned originally. Make openApiDocumentRef default to an empty object if necessary.
If there are good reasons for either of these decisions, don't make changes. Just explain the reasoning.
Done in commit a20b89c.
|
Coverage Report for PR #1638Coverage decreased (-0.1%) to 87.212%Diff Coverage: 36 of 54 changed lines covered (66.67%)Uncovered Changes
Coverage Regressions1 previously-covered line in 1 file lost coverage.
💛 - Coveralls |
|
@copilot clean up the merge conflict mess |
Head branch was pushed to by a user without write access
Done in commit f9d7dc7. Resolved the leftover conflict markers in both files:
|
|
@copilot TypeScript compilation is failing src/server/module-loader.ts:53:5 - error TS2741: Property 'paths' is missing in type '{}' but required in type 'OpenApiDocument'. 53 openApiDocument: OpenApiDocument = {}, src/server/dispatcher.ts:60:3 Found 1 error in src/server/module-loader.ts:53 |
Summary
The
Contextclass in_.context.tshad no way to access the loaded OpenAPI document at construction time. This change passes it asopenApiDocumentin the constructor options object, alongside the existingloadContextandreadJsonhelpers. When the OpenAPI spec changes on disk, the document is reloaded and the Context automatically receives the latest spec via a stable read-onlyProxy.Original Prompt
Pass the OpenAPI document to the constructor of the Context class (exported from
_.context.ts).Manual acceptance tests
_.context.tsconstructor receivesopenApiDocumentand it matches the loaded spec (e.g.openApiDocument.pathscontains the routes defined in the YAML)_.context.tsfiles with no constructor argument continue to work unchangedloadContextandreadJsonstill work correctly alongsideopenApiDocumentopenApiPath === "_"),openApiDocumentis an empty object — notundefinedor an erroropenApiDocumentreflects the updated spec through the same proxy referenceopenApiDocumentinside a Context does not throw and has no effect (read-only)setOpenApiDocumenteven when no initial document was provided correctly populates the proxyTasks
openApiDocument: OpenApiDocument = {}parameter (defaulting to empty object) toModuleLoaderconstructorOpenApiDocumenttype fromdispatcher.tsintomodule-loader.tsopenApiDocumentRef(no.currentwrapper); the Proxy capturesself = thisin its handler closures so reads always reflect the current value ofthis.openApiDocumentRefsetOpenApiDocument(newDoc)simply reassignsthis.openApiDocumentRef = newDoc— no mutation of the document object, no early-return guard; always works even when no initial document was providedProxy(this.openApiDocumentProxy) is always created (even when no document is provided) and returnsfalsefor anysetordeletePropertytrapsthis.openApiDocumentProxyinto the Context constructor options objectapp.ts,counterfact()loadsopenApiDocumentconditionally (openApiPath === "_" ? undefined : loadOpenApiDocument(...)) and shares it with bothDispatcherandModuleLoadercreateMswHandlers()inapp.tsto forwardopenApiDocumenttoModuleLoaderapp.ts, added acodeGenerator"generate" event listener that reloads the spec on disk change and callsmoduleLoader.setOpenApiDocument(newDoc)+ updatesdispatcher.openApiDocumentsrc/server/module-loader.tsandsrc/app.ts, combining the feature branch's Proxy/OpenApiDocumentadditions with main'sFileDiscoveryandmiddleware-detectoradditionspathsoptional in theOpenApiDocumentinterface indispatcher.tsso that{}is a valid default value (thefor...inloop overpathsindispatcher.tsalready handlesundefinedgracefully)