feat: append new HTTP method handlers to existing route files#2104
feat: append new HTTP method handlers to existing route files#2104Copilot wants to merge 7 commits into
Conversation
pmcelhaney
left a comment
There was a problem hiding this comment.
@copilot CI is failing on lint rules
| if (lastImport.index === undefined) { | ||
| debug( | ||
| `could not determine last import position in ${fullPath}; skipping import insertion`, | ||
| ); |
There was a problem hiding this comment.
@copilot if you can't find an existing import then default to position 0. In other words, preprend the import to the beginning of the file. That should always be safe.
There was a problem hiding this comment.
Implemented in 42663de. In appendNewHandlers, when import insertion cannot determine a valid existing import index, it now falls back to insert position 0 (prepend). I also added a regression test that strips imports from an existing route file and verifies the new import is prepended when appending a new method handler.
Fixed in |
Summary
When a new HTTP method is added to an existing path in the OpenAPI spec, regeneration previously skipped updating existing route files, so new handler stubs were missing. This change keeps existing route code intact while appending only missing handler exports (and required type imports).
It also incorporates PR feedback by ensuring import insertion safely falls back to prepending at file start when no existing import position can be determined, and fixes TypeScript typing issues in the append logic.
Original Prompt
When the OpenAPI spec is updated to add a new request method to an existing route, the type for that request method is generated. However, the handler under routes is not generated, due to the rule that existing files under routes should not be overwritten.
In this case, it's okay to APPEND a new export to the file under routes as long as none of the existing code has been changed.
Manual acceptance tests
GET /pet— verifyroutes/pet.tsis created with only aGETexport.POST /petto the spec and regenerate — verifyGETis unchanged and a newPOSTstub is appended at the bottom of the file.GEThandler before regenerating withPOST— verify the custom edit survives andPOSTis still appended.routes/pet.ts, then regenerate after adding a new method — verify missingimport typelines are prepended at the top of the file.Tasks
Repository.appendNewHandlers(fullPath, generatedContent)to:export const METHODhandlers in existing route files.import typelines and handler export blocks from generated content.0when not.Repository.writeFiles()to use append behavior for existing route files instead of unconditionally skipping them.docs/faq.mdto document append-on-update route behavior.