Skip to content

Commit

Permalink
fix: avoid printing duplicate null nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Sep 26, 2024
1 parent 6b4b117 commit 11ee53f
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-moose-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: avoid printing duplicate null nodes
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
**/.vitepress/cache
**/.vitepress/dist
**/test/e2e
**/__snapshots__

**/CHANGELOG.md
pnpm-lock.yaml
5 changes: 4 additions & 1 deletion packages/openapi-ts/src/utils/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ const typeUnionOrIntersection = ({
const node =
style === 'union'
? compiler.typeUnionNode({
isNullable: model.isNullable,
// avoid printing duplicate null statements
isNullable:
model.isNullable &&
!model.properties.find((property) => property.isNullable),
types,
})
: compiler.typeIntersectionNode({
Expand Down
65 changes: 65 additions & 0 deletions packages/openapi-ts/test/3.1.0.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { describe, expect, it } from 'vitest';

import { createClient } from '../';
import type { UserConfig } from '../src/types/config';
import { getFilePaths } from './utils';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

describe('OpenAPI 3.1.0', () => {
const createConfig = (userConfig: UserConfig): UserConfig => ({
client: '@hey-api/client-fetch',
schemas: false,
...userConfig,
input: path.join(
__dirname,
'spec',
'3.1.0',
typeof userConfig.input === 'string' ? userConfig.input : '',
),
output: path.join(
__dirname,
'generated',
'3.1.0',
typeof userConfig.output === 'string' ? userConfig.output : '',
),
});

const scenarios = [
{
config: createConfig({
input: 'duplicate-null.json',
output: 'duplicate-null',
services: {
export: false,
},
}),
description: 'does not generate duplicate null',
},
];

it.each(scenarios)('$description', async ({ config }) => {
// @ts-ignore
await createClient(config);

const outputPath = typeof config.output === 'string' ? config.output : '';
const filePaths = getFilePaths(outputPath);

filePaths.forEach((filePath) => {
const fileContent = readFileSync(filePath, 'utf-8');
expect(fileContent).toMatchFileSnapshot(
path.join(
__dirname,
'__snapshots__',
'3.1.0',
filePath.slice(outputPath.length + 1),
),
);
});
});
});
2 changes: 2 additions & 0 deletions packages/openapi-ts/test/__snapshots__/3.1.0/index.ts
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';
18 changes: 18 additions & 0 deletions packages/openapi-ts/test/__snapshots__/3.1.0/types.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This file is auto-generated by @hey-api/openapi-ts

export type PostTestData = {
/**
* should not produce duplicate null
*/
body?: {
weirdEnum?: ('' | (string) | null);
};
};

export type $OpenApiTs = {
'/test': {
post: {
req: PostTestData;
};
};
};
23 changes: 12 additions & 11 deletions packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,28 @@ const main = async () => {
// debug: true,
// experimental_parser: true,
// input: './test/spec/v3-transforms.json',
input: './test/spec/v3.json',
// input: './test/spec/v3.json',
input: './test/spec/3.1.0/duplicate-null.json',
// input: './test/spec/v2.json',
// input: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
// name: 'foo',
output: {
path: './test/generated/sample/',
},
plugins: [
{
// infiniteQueryOptions: false,
// mutationOptions: false,
name: '@tanstack/react-query',
// queryOptions: false,
},
// '@hey-api/services',
],
// plugins: [
// {
// // infiniteQueryOptions: false,
// // mutationOptions: false,
// name: '@tanstack/react-query',
// // queryOptions: false,
// },
// // '@hey-api/services',
// ],
schemas: {
export: false,
},
services: {
// export: false,
export: false,
// asClass: true,
// filter: '^GET /api/v{api-version}/simple:operation$',
// export: false,
Expand Down
37 changes: 37 additions & 0 deletions packages/openapi-ts/test/spec/3.1.0/duplicate-null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0"
},
"paths": {
"/test": {
"post": {
"requestBody": {
"description": "should not produce duplicate null",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"weirdEnum": {
"oneOf": [
{
"type": "string",
"enum": [""]
},
{
"type": "string",
"nullable": true
}
],
"nullable": true
}
}
}
}
}
}
}
}
}
}
20 changes: 20 additions & 0 deletions packages/openapi-ts/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { readdirSync, statSync } from 'node:fs';
import path from 'node:path';

export const getFilePaths = (dirPath: string): Array<string> => {
let filePaths: Array<string> = [];
const files = readdirSync(dirPath);

for (const file of files) {
const filePath = path.join(dirPath, file);
const stat = statSync(filePath);

if (stat.isDirectory()) {
filePaths = filePaths.concat(getFilePaths(filePath));
} else {
filePaths.push(filePath);
}
}

return filePaths;
};

0 comments on commit 11ee53f

Please sign in to comment.