Skip to content

Commit

Permalink
feat: extend nullable handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnig committed Mar 29, 2022
1 parent 1101917 commit b9e7291
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 47 deletions.
51 changes: 51 additions & 0 deletions packages/cli/src/schema/__tests__/nullableSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,54 @@ test('convert anyOf to nullable', () => {
}
`);
});

const input2 = {
AnyResponse: {
type: 'object',
properties: {
multiNull: {
type: ['null', 'number', 'string'],
},
multiNull2: {
type: ['number', 'string'],
},
},
additionalProperties: false,
$id: 'AnyResponse',
},
};

test('convert anyOf to nullable', () => {
expect(convertNullToNullable(input2)).toMatchInlineSnapshot(`
Object {
"AnyResponse": Object {
"$id": "AnyResponse",
"additionalProperties": false,
"properties": Object {
"multiNull": Object {
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
"multiNull2": Object {
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
},
},
"type": "object",
},
}
`);
});
13 changes: 13 additions & 0 deletions packages/cli/src/schema/helper/convertNullToNullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,18 @@ export function convertNullToNullable(object: any): any {
return convertNullToNullable(result);
}
}

if (value.type && _.isArray(value.type)) {
if (value.type.includes('null')) {
value.nullable = true;
}
value.anyOf = value.type
.filter((x: string) => x !== 'null')
.map((x: string) => {
return { type: x };
});
delete value.type;
return convertNullToNullable(value);
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,37 @@ Object {
"type": "number",
},
"multiNull": Object {
"type": Array [
"null",
"number",
"string",
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
"multiNumber": Object {
"type": Array [
"null",
"number",
"string",
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
"multiString": Object {
"type": Array [
"null",
"number",
"string",
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
},
"required": Array [
Expand Down Expand Up @@ -369,25 +381,37 @@ Object {
"type": "number",
},
"multiNull": Object {
"type": Array [
"null",
"number",
"string",
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
"multiNumber": Object {
"type": Array [
"null",
"number",
"string",
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
"multiString": Object {
"type": Array [
"null",
"number",
"string",
"anyOf": Array [
Object {
"type": "number",
},
Object {
"type": "string",
},
],
"nullable": true,
},
},
"required": Array [
Expand Down
12 changes: 6 additions & 6 deletions packages/integration-tests/tests-e2e/api/GeneratedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export interface AnyResponse {
anyString: any;
anyArray: any[];
anyObject: any;
multiNull: null | number | string;
multiNumber: null | number | string;
multiString: null | number | string;
multiNull: number | string | null;
multiNumber: number | string | null;
multiString: number | string | null;
}

export interface UndefinedResponse {
Expand Down Expand Up @@ -342,9 +342,9 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
anyString: any;
anyArray: any[];
anyObject: any;
multiNull: null | number | string;
multiNumber: null | number | string;
multiString: null | number | string;
multiNull: number | string | null;
multiNumber: number | string | null;
multiString: number | string | null;
},
any
>({
Expand Down
29 changes: 12 additions & 17 deletions playground/template-prisma/tests-e2e/api/GeneratedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@
* ---------------------------------------------------------------
*/

export interface TodoResponse {
id: number;

/** @format date-time */
createdAt: string;
text: string;
priority: number;
done: boolean;
export interface ListTodoQuery {
includeDone?: boolean;
}

/**
* Model todos
*/
export interface Todos {
export interface TodoResponse {
id: number;

/** @format date-time */
Expand All @@ -45,10 +36,6 @@ export interface UpdateTodo {
done?: boolean;
}

export interface ListTodoQuery {
includeDone?: boolean;
}

import axios, { AxiosInstance, AxiosRequestConfig, ResponseType } from 'axios';

export type QueryParamsType = Record<string | number, any>;
Expand Down Expand Up @@ -150,8 +137,16 @@ export class HttpClient<SecurityDataType = unknown> {
body = this.createFormData(body as Record<string, unknown>);
}

if (!type) {
type = ContentType.Json;
}

if (!body) {
body = {};
}

try {
const result = await this.instance.request({
const result: any = await this.instance.request({
...requestParams,
headers: {
...(type && type !== ContentType.FormData ? { 'Content-Type': type } : {}),
Expand Down

0 comments on commit b9e7291

Please sign in to comment.