Skip to content

feat: append new HTTP method handlers to existing route files#2104

Open
Copilot wants to merge 7 commits into
mainfrom
copilot/append-new-handlers-to-route-files
Open

feat: append new HTTP method handlers to existing route files#2104
Copilot wants to merge 7 commits into
mainfrom
copilot/append-new-handlers-to-route-files

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 22, 2026

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

  • Generate routes from a spec with GET /pet — verify routes/pet.ts is created with only a GET export.
  • Add POST /pet to the spec and regenerate — verify GET is unchanged and a new POST stub is appended at the bottom of the file.
  • Manually edit the GET handler before regenerating with POST — verify the custom edit survives and POST is still appended.
  • Remove import lines from an existing routes/pet.ts, then regenerate after adding a new method — verify missing import type lines are prepended at the top of the file.
  • Regenerate with no spec changes — verify no existing route file is modified.

Tasks

  • Added Repository.appendNewHandlers(fullPath, generatedContent) to:
    • Detect missing export const METHOD handlers in existing route files.
    • Extract matching import type lines and handler export blocks from generated content.
    • Insert missing imports after the last import when present, or prepend at position 0 when not.
    • Append only missing handler export blocks without modifying existing handler code.
  • Updated Repository.writeFiles() to use append behavior for existing route files instead of unconditionally skipping them.
  • Fixed TypeScript safety issues in append logic by narrowing regex capture values before use.
  • Added/updated repository tests for:
    • Appending newly added methods,
    • Preserving user edits,
    • No-op regeneration with unchanged specs,
    • Import-prepend fallback when existing route files have no import section.
  • Updated docs/faq.md to document append-on-update route behavior.

Copilot AI linked an issue May 22, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Append new handlers to route files feat: append new HTTP method handlers to existing route files May 22, 2026
Copilot AI requested a review from pmcelhaney May 22, 2026 23:12
Copy link
Copy Markdown
Collaborator

@pmcelhaney pmcelhaney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot CI is failing on lint rules

Comment thread src/typescript-generator/repository.ts Outdated
if (lastImport.index === undefined) {
debug(
`could not determine last import position in ${fullPath}; skipping import insertion`,
);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 22, 2026

@copilot CI is failing on lint rules

Fixed in 42663de. I investigated the failing CI run and the failure was in TypeScript checks (not eslint): nullable regex captures in repository.ts. The commit adds proper type narrowing and CI now passes locally with yarn lint, yarn build, and yarn test.

Copilot AI requested a review from pmcelhaney May 22, 2026 23:25
@pmcelhaney pmcelhaney marked this pull request as ready for review May 22, 2026 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Append new handlers to route files

2 participants