Skip to content

Commit

Permalink
feat: improve anyOf handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnig committed Mar 29, 2022
1 parent 5658962 commit 0a3b4d2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 69 deletions.
7 changes: 7 additions & 0 deletions packages/cli/src/schema/__tests__/nullableSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ const input2 = {
multiNull2: {
type: ['number', 'string'],
},
multiNull3: {
type: ['null', 'string'],
},
},
additionalProperties: false,
$id: 'AnyResponse',
Expand Down Expand Up @@ -137,6 +140,10 @@ test('convert anyOf to nullable', () => {
},
],
},
"multiNull3": Object {
"nullable": true,
"type": "string",
},
},
"type": "object",
},
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/schema/helper/convertNullToNullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ export function convertNullToNullable(object: any): any {
if (value.type.includes('null')) {
value.nullable = true;
}
value.anyOf = value.type
.filter((x: string) => x !== 'null')
.map((x: string) => {
const notNullTypes = value.type.filter((x: string) => x !== 'null');
if (notNullTypes.length === 1) {
value.type = notNullTypes[0];
} else {
value.anyOf = notNullTypes.map((x: string) => {
return { type: x };
});
delete value.type;
delete value.type;
}

return convertNullToNullable(value);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`schema 1`] = `
exports[`schema2 1`] = `
Object {
"components": Object {
"schemas": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,61 +1184,3 @@ Object {
},
}
`;

exports[`schema 2`] = `
Object {
"components": Object {
"schemas": Object {
"V2Response": Object {
"additionalProperties": false,
"properties": Object {
"foo": Object {
"type": "string",
},
},
"required": Array [
"foo",
],
"type": "object",
},
},
},
"info": Object {
"description": "",
"title": "Api",
"version": "",
},
"openapi": "3.0.3",
"paths": Object {
"/v2": Object {
"get": Object {
"operationId": "string",
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"additionalProperties": false,
"properties": Object {
"foo": Object {
"type": "string",
},
},
"required": Array [
"foo",
],
"type": "object",
},
},
},
"description": "Default Response",
},
},
"tags": Array [
"v2",
],
},
},
},
}
`;
6 changes: 6 additions & 0 deletions packages/integration-tests/tests-e2e/swagger-2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { client2 } from './api';

test('schema2', async () => {
const { data } = await client2.instance.get('/json');
expect(data).toMatchSnapshot();
});
7 changes: 1 addition & 6 deletions packages/integration-tests/tests-e2e/swagger.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { client2, client } from './api';
import { client } from './api';

test('schema', async () => {
const { data } = await client.instance.get('/json');
expect(data).toMatchSnapshot();
});

test('schema', async () => {
const { data } = await client2.instance.get('/json');
expect(data).toMatchSnapshot();
});

0 comments on commit 0a3b4d2

Please sign in to comment.