这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 25, 2025

  • Explored repository structure and understood the issue
  • Reproduced the issue with test OpenAPI spec
  • Identified the problem in readWrite.ts transform logic
  • Analyzed root cause of context propagation issue
  • Designed minimal fix for the issue
  • Implemented the fix
  • Updated test snapshots to reflect new behavior
  • Verified fix with original issue spec
  • Ran existing tests - all pass (954 unit tests + 613 integration tests)
  • Run linting and type checking - all pass
  • Committed changes locally
  • Requested code review - no issues found
  • Updated all snapshots including zod packages
  • Ready for maintainer review

Summary

Fixed a regression introduced in v0.86.4 where response types incorrectly used writable variants instead of read-only ones when schemas reference other schemas with writeOnly fields.

The Problem

When a schema like Item references Payload (which has writeOnly fields and is split into Payload and PayloadWritable), both request and response types were using the writable variant. This is incorrect - responses should use the read variant.

Before (buggy):

export type Item = {
    payload: PayloadWritable;  // ❌ Wrong! Should use Payload
};

export type ItemCreateResponses = {
    201: Item;  // ❌ Using writable variant in response
};

After (fixed):

export type Item = {
    payload: Payload;  // ✅ Correct! Uses read variant
};

export type ItemWritable = {
    payload: PayloadWritable;  // ✅ Write variant for requests
};

export type ItemCreateResponses = {
    201: Item;  // ✅ Uses read variant
};

Root Cause

The splitSchemas function compared pruned read/write variants and skipped splitting if identical. However, schemas that only reference split schemas (via $refs) don't change during pruning, so they were incorrectly not split.

Solution

Check transitive dependencies: if a schema references any schema that needs read/write variants, split it even if pruning doesn't change it directly.

Testing

✅ All 954 unit tests pass
✅ All 613 integration tests pass
✅ All 150 zod tests pass
✅ Linting and type checking pass
✅ Automated code review found no issues
✅ Manually verified with issue reproduction case

Original prompt

This section details on the original issue you should resolve

<issue_title>writeOnly regression: response types now use writable variant</issue_title>
<issue_description>### Description

The fix for #2792 shipped in @hey-api/openapi-ts@0.86.4 restored writeOnly fields for request bodies, but it now propagates the writable shape everywhere the schema is referenced. If a schema is shared between a request body and a response, the generated response type is also replaced with PayloadWritable.

This is different from the original bug: request payloads are now correct, but responses are incorrectly using the writable variant instead of the read-only one.

Reproducible example or configuration

npx --yes @hey-api/openapi-ts@latest \
  --input writeonly-regression.yml \
  --output out \
  --plugins @hey-api/typescript

Generated snippet (out/types.gen.ts):

export type Item = {
    payload: PayloadWritable;
};

export type CreateItemRequest = {
    payload: PayloadWritable;
};

export type Payload = {
    kind: 'jpeg';
};

export type PayloadWritable = {
    kind: 'jpeg';
    /**
     * Data sent on write only
     */
    encoded: string;
};

export type ItemCreateData = {
    body: CreateItemRequest;
    path?: never;
    query?: never;
    url: '/items';
};

export type ItemCreateResponses = {
    /**
     * Created item
     */
    201: Item;
};

export type ItemCreateResponse = ItemCreateResponses[keyof ItemCreateResponses];

export type ItemRetrieveData = {
    body?: never;
    path: {
        id: string;
    };
    query?: never;
    url: '/items/{id}';
};

export type ItemRetrieveResponses = {
    /**
     * Item
     */
    200: Item;
};

export type ItemRetrieveResponse = ItemRetrieveResponses[keyof ItemRetrieveResponses];

OpenAPI specification

openapi: 3.0.3
info:
  title: writeOnly regression repro
  version: 1.0.0
paths:
  /items:
    post:
      operationId: item_create
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateItemRequest'
      responses:
        '201':
          description: Created item
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
  /items/{id}:
    get:
      operationId: item_retrieve
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Item
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
components:
  schemas:
    Item:
      type: object
      required: [payload]
      properties:
        payload:
          $ref: '#/components/schemas/Payload'
    CreateItemRequest:
      type: object
      required: [payload]
      properties:
        payload:
          $ref: '#/components/schemas/Payload'
    Payload:
      type: object
      required: [kind, encoded]
      properties:
        kind:
          type: string
          enum: [jpeg]
        encoded:
          type: string
          writeOnly: true
          description: Data sent on write only

System information

@hey-api/openapi-ts 0.86.4
Node.js v23.7.0
macOS 15.7.1 (24G231)

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@changeset-bot
Copy link

changeset-bot bot commented Oct 25, 2025

🦋 Changeset detected

Latest commit: 16cc0e7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hey-api/openapi-ts Patch

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

@vercel
Copy link

vercel bot commented Oct 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
hey-api-docs Ready Ready Preview Comment Oct 26, 2025 1:29am

Copilot AI changed the title [WIP] Fix writeOnly regression in response types Fix writeOnly regression: response types incorrectly use writable variants Oct 25, 2025
Copilot AI requested a review from mrlubos October 25, 2025 17:36
Copilot finished work on behalf of mrlubos October 25, 2025 17:36
@mrlubos mrlubos marked this pull request as ready for review October 25, 2025 20:24
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. bug 🔥 Something isn't working labels Oct 25, 2025
@mrlubos
Copy link
Member

mrlubos commented Oct 25, 2025

@copilot update all snapshots with pnpm test:update

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 25, 2025

@copilot update all snapshots with pnpm test:update

Updated all snapshots with pnpm vitest run --update in the zod test packages (v3 and v4). All tests now pass. Commit: 16c3c0f

@codecov
Copy link

codecov bot commented Oct 26, 2025

Codecov Report

❌ Patch coverage is 58.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 30.38%. Comparing base (3292581) to head (16cc0e7).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...napi-ts/src/openApi/shared/transforms/readWrite.ts 58.33% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2850      +/-   ##
==========================================
+ Coverage   30.36%   30.38%   +0.01%     
==========================================
  Files         485      485              
  Lines       39803    39802       -1     
  Branches     2413     2415       +2     
==========================================
+ Hits        12088    12092       +4     
+ Misses      27684    27678       -6     
- Partials       31       32       +1     
Flag Coverage Δ
unittests 30.38% <58.33%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 26, 2025

Open in StackBlitz

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/codegen-core@2850
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@2850
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@2850
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@2850

commit: 16cc0e7

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Oct 26, 2025
@mrlubos mrlubos merged commit f14f95a into main Oct 26, 2025
17 checks passed
@mrlubos mrlubos deleted the copilot/fix-writeonly-regression branch October 26, 2025 01:39
@hey-api hey-api bot mentioned this pull request Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🔥 Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

writeOnly regression: response types now use writable variant

2 participants