Skip to content

Commit

Permalink
Merge pull request #1266 from hey-api/fix/array-items-one-of-length-1
Browse files Browse the repository at this point in the history
fix: correctly generate array when items are a oneOf array with length 1
  • Loading branch information
mrlubos authored Nov 9, 2024
2 parents f31d4e2 + d60d260 commit 04c88dd
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-trees-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: correctly generate array when items are a oneOf array with length 1
7 changes: 3 additions & 4 deletions packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ const parseArray = ({
if ('$ref' in schema.items) {
schemaItems.push(irItemsSchema);
} else {
const isComposedSchema = Boolean(
schema.items.allOf || schema.items.anyOf || schema.items.oneOf,
);
if (isComposedSchema) {
const ofArray =
schema.items.allOf || schema.items.anyOf || schema.items.oneOf;
if (ofArray && ofArray.length > 1 && !schema.items.nullable) {
// bring composition up to avoid incorrectly nested arrays
irSchema = {
...irSchema,
Expand Down
11 changes: 7 additions & 4 deletions packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ const parseArray = ({
) {
schemaItems = Array(schema.maxItems).fill(irItemsSchema);
} else {
const isComposedSchema = Boolean(
schema.items.allOf || schema.items.anyOf || schema.items.oneOf,
);
if (isComposedSchema) {
const ofArray =
schema.items.allOf || schema.items.anyOf || schema.items.oneOf;
if (
ofArray &&
ofArray.length > 1 &&
!getSchemaTypes({ schema: schema.items }).includes('null')
) {
// bring composition up to avoid incorrectly nested arrays
irSchema = {
...irSchema,
Expand Down
8 changes: 8 additions & 0 deletions packages/openapi-ts/test/3.0.x.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ describe(`OpenAPI ${VERSION}`, () => {
}),
description: 'allows arbitrary properties on objects',
},
{
config: createConfig({
input: 'array-items-one-of-length-1.json',
output: 'array-items-one-of-length-1',
}),
description:
'generates correct array when items are oneOf array with single item',
},
{
config: createConfig({
input: 'enum-escape.json',
Expand Down
8 changes: 8 additions & 0 deletions packages/openapi-ts/test/3.1.x.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ describe(`OpenAPI ${VERSION}`, () => {
}),
description: 'allows arbitrary properties on objects',
},
{
config: createConfig({
input: 'array-items-one-of-length-1.json',
output: 'array-items-one-of-length-1',
}),
description:
'generates correct array when items are oneOf array with single item',
},
{
config: createConfig({
input: 'duplicate-null.json',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Foo = {
foo?: Array<Bar>;
};

export type Bar = string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto-generated by @hey-api/openapi-ts
export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Foo = {
foo?: Array<Bar>;
};

export type Bar = string;
4 changes: 2 additions & 2 deletions packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const main = async () => {
input: {
// include:
// '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$',
path: './test/spec/3.0.x/parameter-explode-false.json',
path: './test/spec/3.1.x/array-items-one-of-length-1.json',
// path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
},
// name: 'foo',
Expand All @@ -30,7 +30,7 @@ const main = async () => {
{
// asClass: true,
// include...
name: '@hey-api/services',
// name: '@hey-api/services',
// serviceNameBuilder: '^Parameters',
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"openapi": "3.0.2",
"info": {
"title": "OpenAPI 3.0.2 array items oneOf length 1 example",
"version": "1"
},
"components": {
"schemas": {
"Foo": {
"type": "object",
"properties": {
"foo": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/Bar"
}
]
},
"maxItems": 2147483647,
"minItems": 1
}
}
},
"Bar": {
"type": "string"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI 3.1.0 array items oneOf length 1 example",
"version": "1"
},
"components": {
"schemas": {
"Foo": {
"type": "object",
"properties": {
"foo": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/Bar"
}
]
},
"maxItems": 2147483647,
"minItems": 1
}
}
},
"Bar": {
"type": "string"
}
}
}
}

0 comments on commit 04c88dd

Please sign in to comment.