Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix nested length-constrained array generation#2330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Jamie5 wants to merge1 commit intoopenapi-ts:main
base:main
Choose a base branch
Loading
fromJamie5:array

Conversation

Jamie5
Copy link

@Jamie5Jamie5 commentedMay 21, 2025
edited
Loading

Changes

This fixes a bug when doing nested length-constrained array generation, specifically the situation added inoptions > arrayLength: true > minItems: 1, maxItems: 1; minItems: 1, maxItems: 1. Without the change, the code would generate the following (note the extra layer of nesting):

[    [        string    ][]]

What went wrong in the old code? I think the main issue is thatitemType could either be the actual item type, or the type of the entire array (in the case of a tuple). In the problematic nesting situation, in the outer nesting, we run the codeitemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));. The call totransformSchemaObject returns[string], and thencreateArrayTypeNode turns it into[string][]. Finally, we go through the logic ofarrayLength, which wraps that double nested array again as a singleton array, to become a triply-nested array.

This also likely fixes a situation where length-constrained arrays were not being declared asreadonly when the immutable flag was set - notice the old code early-returns when computing length-constrained arrays.

The obvious first change is to stop doingitemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options)); completely and always just doitemType = transformSchemaObject(schemaObject.items, options);, but this runs into a problem with basic nested arrays. In the basic nested array case, in the inner runitemType isstring[], which makes sense, and then we would like to in the outer run turn that it intostring[][]. However, this does not happen - when computingfinalType, we will skip the array-wrapping in this case, as itemType is indeed an array (it is an array because the type of the item is a nested array, but we do not know that).

Fundamentally, I think the problem is that sometimesitemType is the type of a single item in the array, and sometimes it is the entire array, and we cannot simply check whetheritemType is an array/tuple or not to detect that, so in the length-unconstrained array case we added a hack that then caused problems with the nested length-constrained array case.

Hence, this diff changes things so thatitemType is truly the type of the item, and hence is applicable only with arrays. Instead, we havearrayType, which is the type of the array/tuple before potentially applying immutability. In this flow of logic, for tuple types, we simply create the tuple and stop there in thearrayType computation. It is only for arrays that we compute theitemType, which is truly the type of a single item in the array. Then, for length-constrained arrays we use thatitemType to generate the array, whiule for length-unconstrained arrays we simply make an array ofitemType.

How to Review

The main thing is - are there additional missing tests, which would catch issues overlooked in this change? Also, the change in logic should hopefully be clearer in addition to fixing this specific problem.

(The main code is also easier to review when hiding whitespace)

Checklist

  • Unit tests updated
  • [N/A]docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

throwandgo reacted with heart emoji
@Jamie5Jamie5 requested a review froma team as acode ownerMay 21, 2025 22:28
@netlifyNetlify
Copy link

netlifybot commentedMay 21, 2025
edited
Loading

👷 Deploy request foropenapi-ts pending review.

Visit the deploys page to approve it

NameLink
🔨 Latest commit9749add

@changeset-botchangeset-bot
Copy link

changeset-botbot commentedMay 21, 2025
edited
Loading

⚠️ No Changeset found

Latest commit:9749add

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go.If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Jamie5Jamie5force-pushed thearray branch 2 times, most recently fromfafb525 to19be4e4CompareMay 21, 2025 22:33
@throwandgo
Copy link

@duncanbeevers my team is also running into this issue. Is there any additional context we can provide to help push this through?

@duncanbeevers
Copy link
Contributor

Ithink#2148 should address this, but I haven't rebased it for a while.
I'll see if I can squeeze in some time this weekend to compare this PR to the older one and shepherd a solution.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@duncanbeeversduncanbeeversAwaiting requested review from duncanbeeversduncanbeevers is a code owner automatically assigned from openapi-ts/maintainers

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@Jamie5@throwandgo@duncanbeevers

[8]ページ先頭

©2009-2025 Movatter.jp