fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #572 from hey-api/fix/arguments-optional

fix: generate correct optional key in types when using positional arguments (useOptions: false)

authored by

Lubos and committed by
GitHub
67beaab2 0277e291

+43 -49
+5
.changeset/selfish-waves-give.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: generate correct optional key in types when using positional arguments (useOptions: false)
-19
packages/openapi-ts/src/utils/required.ts
··· 1 - import { Model, OperationParameter } from '../openApi'; 2 - import { getConfig } from './config'; 3 - 4 - export const getDefaultPrintable = ( 5 - p: OperationParameter | Model, 6 - ): string | undefined => { 7 - if (p.default === undefined) { 8 - return undefined; 9 - } 10 - return JSON.stringify(p.default, null, 4); 11 - }; 12 - 13 - export const modelIsRequired = (model: Model) => { 14 - const config = getConfig(); 15 - if (config?.useOptions) { 16 - return model.isRequired ? '' : '?'; 17 - } 18 - return !model.isRequired && !getDefaultPrintable(model) ? '?' : ''; 19 - };
+10 -2
packages/openapi-ts/src/utils/write/services.ts
··· 17 17 import type { Client } from '../../types/client'; 18 18 import { getConfig } from '../config'; 19 19 import { escapeComment, escapeName } from '../escape'; 20 - import { modelIsRequired } from '../required'; 21 20 import { transformServiceName } from '../transform'; 22 21 import { unique } from '../unique'; 23 22 import { uniqueTypeName } from './type'; ··· 82 81 ]; 83 82 } 84 83 84 + const getDefaultPrintable = ( 85 + p: OperationParameter | Model, 86 + ): string | undefined => { 87 + if (p.default === undefined) { 88 + return undefined; 89 + } 90 + return JSON.stringify(p.default, null, 4); 91 + }; 92 + 85 93 return operation.parameters.map((p) => { 86 94 const typePath = `${importedType}['${p.name}']`; 87 95 return { 88 96 default: p?.default, 89 - isRequired: modelIsRequired(p) === '', 97 + isRequired: (!p.isRequired && !getDefaultPrintable(p) ? '?' : '') === '', 90 98 name: p.name, 91 99 type: typePath, 92 100 };
+1 -2
packages/openapi-ts/src/utils/write/type.ts
··· 4 4 import { getConfig } from '../config'; 5 5 import { enumValue } from '../enum'; 6 6 import { escapeComment } from '../escape'; 7 - import { modelIsRequired } from '../required'; 8 7 import { unique } from '../unique'; 9 8 10 9 const base = (model: Model) => { ··· 86 85 } 87 86 88 87 const properties: Property[] = model.properties.map((property) => { 89 - let maybeRequired = modelIsRequired(property); 88 + let maybeRequired = property.isRequired ? '' : '?'; 90 89 let value = toType(property); 91 90 // special case for additional properties type 92 91 if (property.name === '[key: string]' && maybeRequired) {
+26 -26
packages/openapi-ts/test/__snapshots__/test/generated/v3_legacy_positional_args/types.gen.ts.snap
··· 24 24 /** 25 25 * This is a simple boolean with default value 26 26 */ 27 - parameterBoolean: boolean | null; 27 + parameterBoolean?: boolean | null; 28 28 /** 29 29 * This is a simple enum with default value 30 30 */ 31 - parameterEnum: 'Success' | 'Warning' | 'Error'; 31 + parameterEnum?: 'Success' | 'Warning' | 'Error'; 32 32 /** 33 33 * This is a simple model with default value 34 34 */ 35 - parameterModel: ModelWithString | null; 35 + parameterModel?: ModelWithString | null; 36 36 /** 37 37 * This is a simple number with default value 38 38 */ 39 - parameterNumber: number | null; 39 + parameterNumber?: number | null; 40 40 /** 41 41 * This is a simple string with default value 42 42 */ 43 - parameterString: string | null; 43 + parameterString?: string | null; 44 44 }; 45 45 46 46 export type CallWithDefaultOptionalParametersData = { 47 47 /** 48 48 * This is a simple boolean that is optional with default value 49 49 */ 50 - parameterBoolean: boolean; 50 + parameterBoolean?: boolean; 51 51 /** 52 52 * This is a simple enum that is optional with default value 53 53 */ 54 - parameterEnum: 'Success' | 'Warning' | 'Error'; 54 + parameterEnum?: 'Success' | 'Warning' | 'Error'; 55 55 /** 56 56 * This is a simple model that is optional with default value 57 57 */ 58 - parameterModel: ModelWithString; 58 + parameterModel?: ModelWithString; 59 59 /** 60 60 * This is a simple number that is optional with default value 61 61 */ 62 - parameterNumber: number; 62 + parameterNumber?: number; 63 63 /** 64 64 * This is a simple string that is optional with default value 65 65 */ 66 - parameterString: string; 66 + parameterString?: string; 67 67 }; 68 68 69 69 export type CallToTestOrderOfParamsData = { 70 70 /** 71 71 * This is a optional string with default 72 72 */ 73 - parameterOptionalStringWithDefault: string; 73 + parameterOptionalStringWithDefault?: string; 74 74 /** 75 75 * This is a optional string with empty default 76 76 */ 77 - parameterOptionalStringWithEmptyDefault: string; 77 + parameterOptionalStringWithEmptyDefault?: string; 78 78 /** 79 79 * This is a optional string with no default 80 80 */ ··· 82 82 /** 83 83 * This is a string that can be null with default 84 84 */ 85 - parameterStringNullableWithDefault: string | null; 85 + parameterStringNullableWithDefault?: string | null; 86 86 /** 87 87 * This is a string that can be null with no default 88 88 */ ··· 108 108 /** 109 109 * This is a simple boolean with default value 110 110 */ 111 - parameterBoolean: boolean | null; 111 + parameterBoolean?: boolean | null; 112 112 /** 113 113 * This is a simple enum with default value 114 114 */ 115 - parameterEnum: 'Success' | 'Warning' | 'Error'; 115 + parameterEnum?: 'Success' | 'Warning' | 'Error'; 116 116 /** 117 117 * This is a simple model with default value 118 118 */ 119 - parameterModel: ModelWithString | null; 119 + parameterModel?: ModelWithString | null; 120 120 /** 121 121 * This is a simple number with default value 122 122 */ 123 - parameterNumber: number | null; 123 + parameterNumber?: number | null; 124 124 /** 125 125 * This is a simple string with default value 126 126 */ 127 - parameterString: string | null; 127 + parameterString?: string | null; 128 128 }; 129 129 }; 130 130 post: { ··· 132 132 /** 133 133 * This is a simple boolean that is optional with default value 134 134 */ 135 - parameterBoolean: boolean; 135 + parameterBoolean?: boolean; 136 136 /** 137 137 * This is a simple enum that is optional with default value 138 138 */ 139 - parameterEnum: 'Success' | 'Warning' | 'Error'; 139 + parameterEnum?: 'Success' | 'Warning' | 'Error'; 140 140 /** 141 141 * This is a simple model that is optional with default value 142 142 */ 143 - parameterModel: ModelWithString; 143 + parameterModel?: ModelWithString; 144 144 /** 145 145 * This is a simple number that is optional with default value 146 146 */ 147 - parameterNumber: number; 147 + parameterNumber?: number; 148 148 /** 149 149 * This is a simple string that is optional with default value 150 150 */ 151 - parameterString: string; 151 + parameterString?: string; 152 152 }; 153 153 }; 154 154 put: { ··· 156 156 /** 157 157 * This is a optional string with default 158 158 */ 159 - parameterOptionalStringWithDefault: string; 159 + parameterOptionalStringWithDefault?: string; 160 160 /** 161 161 * This is a optional string with empty default 162 162 */ 163 - parameterOptionalStringWithEmptyDefault: string; 163 + parameterOptionalStringWithEmptyDefault?: string; 164 164 /** 165 165 * This is a optional string with no default 166 166 */ ··· 168 168 /** 169 169 * This is a string that can be null with default 170 170 */ 171 - parameterStringNullableWithDefault: string | null; 171 + parameterStringNullableWithDefault?: string | null; 172 172 /** 173 173 * This is a string that can be null with no default 174 174 */
+1
packages/openapi-ts/test/sample.cjs
··· 19 19 // include: '^NestedAnyOfArraysNullable', 20 20 // name: 'PascalCase', 21 21 }, 22 + // useOptions: false, 22 23 }; 23 24 24 25 const { createClient } = await import(