这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-zod-union-deduplication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(parser): expand schema deduplication by including validation constraints in type ID
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"openapi": "3.1.0",
"info": {
"title": "String Constraints Union Test",
"version": "1.0.0"
},
"components": {
"schemas": {
"LocaleOrLanguage": {
"anyOf": [
{
"type": "string",
"minLength": 5,
"maxLength": 5,
"description": "Combination of ISO 639-1 and ISO 3166-1 alpha-2 separated by a hyphen."
},
{
"type": "string",
"minLength": 2,
"maxLength": 2,
"description": "ISO 639-1 language code."
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by @hey-api/openapi-ts

import * as z from 'zod/v4-mini';

export const zLocaleOrLanguage = z.union([
z.string().check(z.length(5)),
z.string().check(z.length(2))
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by @hey-api/openapi-ts

import { z } from 'zod';

export const zLocaleOrLanguage = z.union([
z.string().length(5),
z.string().length(2)
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by @hey-api/openapi-ts

import { z } from 'zod/v4';

export const zLocaleOrLanguage = z.union([
z.string().length(5),
z.string().length(2)
]);
7 changes: 7 additions & 0 deletions packages/openapi-ts-tests/zod/v3/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ for (const zodVersion of zodVersions) {
description:
"validator schemas with merged unions (can't use .merge())",
},
{
config: createConfig({
input: 'validators-string-constraints-union.json',
output: 'validators-string-constraints-union',
}),
description: 'validator schemas with string constraints union',
},
];

it.each(scenarios)('$description', async ({ config }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by @hey-api/openapi-ts

import * as z from 'zod/mini';

export const zLocaleOrLanguage = z.union([
z.string().check(z.length(5)),
z.string().check(z.length(2))
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by @hey-api/openapi-ts

import { z } from 'zod/v3';

export const zLocaleOrLanguage = z.union([
z.string().length(5),
z.string().length(2)
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by @hey-api/openapi-ts

import { z } from 'zod';

export const zLocaleOrLanguage = z.union([
z.string().length(5),
z.string().length(2)
]);
7 changes: 7 additions & 0 deletions packages/openapi-ts-tests/zod/v4/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ for (const zodVersion of zodVersions) {
description:
"validator schemas with merged unions (can't use .merge())",
},
{
config: createConfig({
input: 'validators-string-constraints-union.json',
output: 'validators-string-constraints-union',
}),
description: 'validator schemas with string constraints union',
},
];

it.each(scenarios)('$description', async ({ config }) => {
Expand Down
20 changes: 19 additions & 1 deletion packages/openapi-ts/src/ir/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ export const deduplicateSchema = <T extends IR.SchemaObject>({
item.format !== undefined && detectFormat
? `format-${item.format}`
: '';
const typeId = `${item.$ref ?? ''}${item.type ?? ''}${constant}${format}`;

// Include validation constraints in the type ID to avoid incorrect deduplication
const constraints = [
item.minLength !== undefined ? `minLength-${item.minLength}` : '',
item.maxLength !== undefined ? `maxLength-${item.maxLength}` : '',
item.minimum !== undefined ? `minimum-${item.minimum}` : '',
item.maximum !== undefined ? `maximum-${item.maximum}` : '',
item.exclusiveMinimum !== undefined
? `exclusiveMinimum-${item.exclusiveMinimum}`
: '',
item.exclusiveMaximum !== undefined
? `exclusiveMaximum-${item.exclusiveMaximum}`
: '',
item.minItems !== undefined ? `minItems-${item.minItems}` : '',
item.maxItems !== undefined ? `maxItems-${item.maxItems}` : '',
item.pattern !== undefined ? `pattern-${item.pattern}` : '',
].join('');

const typeId = `${item.$ref ?? ''}${item.type ?? ''}${constant}${format}${constraints}`;
if (!typeIds.includes(typeId)) {
typeIds.push(typeId);
uniqueItems.push(item);
Expand Down
Loading