Skip to content

Commit

Permalink
Revert default values being non-nullable; add --default-non-nullable …
Browse files Browse the repository at this point in the history
…flag

#613
  • Loading branch information
drwpow committed Jun 3, 2021
1 parent e2fb371 commit 6942fb4
Show file tree
Hide file tree
Showing 24 changed files with 4,504 additions and 6,017 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ For anything more complicated, or for generating specs dynamically, you can also

#### CLI Options

| Option | Alias | Default | Description |
| :----------------------------- | :---- | :------: | :--------------------------------------------------------------- |
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
| `--auth [token]` | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema). |
| `--immutable-types` | | `false` | (optional) Generates immutable types (readonly properties and readonly array). |
| `--additional-properties` | `-ap` | `false` | (optional) Allow arbitrary properties for all schema objects without `additionalProperties: false` |
| `--prettier-config [location]` | | | (optional) Path to your custom Prettier configuration for output |
| `--raw-schema` | | `false` | Generate TS types from partial schema (e.g. having `components.schema` at the top level) |
| Option | Alias | Default | Description |
| :----------------------------- | :---- | :------: | :------------------------------------------------------------------------------------------------------ |
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
| `--auth [token]` | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema) |
| `--immutable-types` | `-it` | `false` | (optional) Generates immutable types (readonly properties and readonly array) |
| `--additional-properties` | `-ap` | `false` | (optional) Allow arbitrary properties for all schema objects without `additionalProperties: false` |
| `--default-non-nullable` | | `false` | (optional) Treat schema objects with default values as non-nullable |
| `--prettier-config [location]` | `-c` | | (optional) Path to your custom Prettier configuration for output |
| `--raw-schema` | | `false` | Generate TS types from partial schema (e.g. having `components.schema` at the top level) |

### 🐢 Node

Expand Down
5 changes: 5 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Options
--auth (optional) Provide an authentication token for private URL
--immutable-types, -it (optional) Generates immutable types (readonly properties and readonly array)
--additional-properties, -ap (optional) Allow arbitrary properties for all schema objects without "additionalProperties: false"
--default-non-nullable (optional) If a schema object has a default value set, don’t mark it as nullable
--prettier-config, -c (optional) specify path to Prettier config file
--raw-schema (optional) Parse as partial schema (raw components)
--version (optional) Force schema parsing version
Expand All @@ -35,6 +36,9 @@ Options
type: "boolean",
alias: "it",
},
defaultNonNullable: {
type: "boolean",
},
additionalProperties: {
type: "boolean",
alias: "ap",
Expand Down Expand Up @@ -82,6 +86,7 @@ async function generateSchema(pathToSpec) {
auth: cli.flags.auth,
additionalProperties: cli.flags.additionalProperties,
immutableTypes: cli.flags.immutableTypes,
defaultNonNullable: cli.flags.defaultNonNullable,
prettierConfig: cli.flags.prettierConfig,
rawSchema: cli.flags.rawSchema,
version: cli.flags.version,
Expand Down
10,416 changes: 4,426 additions & 5,990 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"eslint": "^7.26.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^26.6.3",
"ts-jest": "^26.5.6",
"jest": "^27.0.3",
"ts-jest": "^27.0.2",
"typescript": "^4.2.4"
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export default function openapiTS(
): string {
// 1. set up context
const ctx: GlobalContext = {
auth: options.auth,
additionalProperties: options.additionalProperties || false,
auth: options.auth,
defaultNonNullable: options.defaultNonNullable || false,
formatter: typeof options.formatter === "function" ? options.formatter : undefined,
immutableTypes: options.immutableTypes || false,
rawSchema: options.rawSchema || false,
Expand Down
3 changes: 2 additions & 1 deletion src/transform/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo

// 2. name (with “?” if optional property)
const readonly = tsReadonly(options.immutableTypes);
const required = options.required.has(k) || hasDefaultValue(v.schema || v) ? "" : "?";
const required =
options.required.has(k) || (options.defaultNonNullable && hasDefaultValue(v.schema || v)) ? "" : "?";
output += `${readonly}"${k}"${required}: `;

// 3. transform
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export interface SwaggerToTSOptions {
formatter?: SchemaFormatter;
/** Generates immutable types (readonly properties and readonly array) */
immutableTypes?: boolean;
/** (optional) Treat schema objects with default values as non-nullable */
defaultNonNullable?: boolean;
/** (optional) Path to Prettier config */
prettierConfig?: string;
/** (optional) Parsing input document as raw schema rather than OpenAPI document */
Expand All @@ -138,6 +140,7 @@ export interface GlobalContext {
auth?: string;
formatter?: SchemaFormatter;
immutableTypes: boolean;
defaultNonNullable: boolean;
/** (optional) Should logging be suppressed? (necessary for STDOUT) */
silent?: boolean;
namespace?: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/expected/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
};
Category: {
id?: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/expected/prettier-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
shipDate?: string
/** Order Status */
status?: 'placed' | 'approved' | 'delivered'
complete: boolean
complete?: boolean
}
Category: {
id?: number
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/expected/prettier-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
shipDate?: string
/** Order Status */
status?: 'placed' | 'approved' | 'delivered'
complete: boolean
complete?: boolean
}
Category: {
id?: number
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/expected/stdout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
};
Category: {
id?: number;
Expand Down
7 changes: 6 additions & 1 deletion tests/operation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { transformOperationObj } from "../src/transform/operation";

const defaults = { additionalProperties: false, immutableTypes: false, rawSchema: false };
const defaults = {
additionalProperties: false,
immutableTypes: false,
defaultNonNullable: false,
rawSchema: false,
};

describe("requestBody", () => {
const basicSchema = {
Expand Down
7 changes: 6 additions & 1 deletion tests/parameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { transformParametersArray } from "../src/transform/parameters";

const defaults = { additionalProperties: false, immutableTypes: false, rawSchema: false };
const defaults = {
additionalProperties: false,
immutableTypes: false,
defaultNonNullable: false,
rawSchema: false,
};

describe("transformParametersArray()", () => {
describe("v2", () => {
Expand Down
1 change: 1 addition & 0 deletions tests/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const defaults = {
additionalProperties: false,
globalParameters: {},
immutableTypes: false,
defaultNonNullable: false,
operations: {},
rawSchema: false,
version: 3, // both 2 and 3 should generate the same
Expand Down
7 changes: 6 additions & 1 deletion tests/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import prettier from "prettier";
import { transformRequestBodies } from "../src/transform/request";

const defaults = { additionalProperties: false, immutableTypes: false, rawSchema: false };
const defaults = {
additionalProperties: false,
immutableTypes: false,
defaultNonNullable: false,
rawSchema: false,
};

function format(source: string) {
return prettier.format(`type requestBodies = {${source.trim()}}`, { parser: "typescript" });
Expand Down
24 changes: 24 additions & 0 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { transformSchemaObj as transform } from "../src/transform/schema";
const defaults = {
additionalProperties: false,
immutableTypes: false,
defaultNonNullable: false,
required: new Set<string>(),
rawSchema: false,
version: 3,
Expand Down Expand Up @@ -368,6 +369,29 @@ describe("SchemaObject", () => {
/** user photo */
"avatar"?: string;
}`);
});
});

describe("--default-non-nullable", () => {
it("default: objects with default values are nullable", () => {
expect(
transform({ type: "object", properties: { default: { type: "boolean", default: true } } }, { ...defaults })
).toBe(`{
"default"?: boolean;
}`);
});

it("enabled: objects with default values are non-nullable", () => {
expect(
transform(
{ type: "object", properties: { default: { type: "boolean", default: true } } },
{ ...defaults, defaultNonNullable: true }
)
).toBe(`{
"default": boolean;
}`);
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/v2/expected/petstore.immutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface definitions {
readonly shipDate?: string;
/** Order Status */
readonly status?: "placed" | "approved" | "delivered";
readonly complete: boolean;
readonly complete?: boolean;
};
readonly Category: {
readonly id?: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/v2/expected/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface definitions {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
};
Category: {
id?: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/expected/petstore-openapitools.additional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface components {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
} & { [key: string]: any };
/** A category for a pet */
Category: {
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/expected/petstore-openapitools.immutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface components {
readonly shipDate?: string;
/** Order Status */
readonly status?: "placed" | "approved" | "delivered";
readonly complete: boolean;
readonly complete?: boolean;
};
/** A category for a pet */
readonly Category: {
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/expected/petstore-openapitools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface components {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
};
/** A category for a pet */
Category: {
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/expected/petstore.additional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
} & { [key: string]: any };
Category: {
id?: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/expected/petstore.immutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
readonly shipDate?: string;
/** Order Status */
readonly status?: "placed" | "approved" | "delivered";
readonly complete: boolean;
readonly complete?: boolean;
};
readonly Category: {
readonly id?: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/expected/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface components {
shipDate?: string;
/** Order Status */
status?: "placed" | "approved" | "delivered";
complete: boolean;
complete?: boolean;
};
Category: {
id?: number;
Expand Down

0 comments on commit 6942fb4

Please sign in to comment.