diff --git a/.changeset/fresh-timers-train.md b/.changeset/fresh-timers-train.md new file mode 100644 index 000000000..b579adbae --- /dev/null +++ b/.changeset/fresh-timers-train.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": minor +--- + +feat(schema): add support for default values diff --git a/.changeset/swift-candles-melt.md b/.changeset/swift-candles-melt.md new file mode 100644 index 000000000..11016d5d0 --- /dev/null +++ b/.changeset/swift-candles-melt.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": minor +--- + +feat(schema): add array of enum values for enums diff --git a/src/openApi/common/interfaces/client.ts b/src/openApi/common/interfaces/client.ts index 62512af10..35eeb0aac 100644 --- a/src/openApi/common/interfaces/client.ts +++ b/src/openApi/common/interfaces/client.ts @@ -56,6 +56,7 @@ export interface Operation extends OperationParameters { } export interface Schema { + default?: unknown; exclusiveMaximum?: boolean; exclusiveMinimum?: boolean; format?: @@ -95,7 +96,6 @@ export interface Model extends Schema { */ $refs: string[]; base: string; - default?: string; deprecated?: boolean; description: string | null; enum: Enum[]; diff --git a/src/openApi/v3/parser/getModelDefault.ts b/src/openApi/common/parser/getDefault.ts similarity index 64% rename from src/openApi/v3/parser/getModelDefault.ts rename to src/openApi/common/parser/getDefault.ts index 2828f3091..36a2b1194 100644 --- a/src/openApi/v3/parser/getModelDefault.ts +++ b/src/openApi/common/parser/getDefault.ts @@ -1,7 +1,12 @@ import type { Model } from '../../common/interfaces/client'; -import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; - -export const getModelDefault = (definition: OpenApiSchema, model?: Model): string | undefined => { +import type { OpenApiParameter } from '../../v2/interfaces/OpenApiParameter'; +import type { OpenApiSchema } from '../../v3/interfaces/OpenApiSchema'; +import type { OperationParameter } from '../interfaces/client'; + +export const getDefault = ( + definition: OpenApiSchema | OpenApiParameter, + model?: Model | OperationParameter +): string | undefined => { if (definition.default === undefined) { return undefined; } @@ -16,18 +21,15 @@ export const getModelDefault = (definition: OpenApiSchema, model?: Model): strin case 'int': case 'integer': case 'number': - if (model?.export === 'enum' && model.enum?.[definition.default]) { - const { value } = model.enum[definition.default]; + if (model?.export === 'enum' && model.enum?.[definition.default as number]) { + const { value } = model.enum[definition.default as number]; return typeof value === 'string' ? `'${value}'` : String(value); } return String(definition.default); - - case 'boolean': - return JSON.stringify(definition.default); - case 'string': return `'${definition.default}'`; - + case 'array': + case 'boolean': case 'object': try { return JSON.stringify(definition.default, null, 4); @@ -35,6 +37,5 @@ export const getModelDefault = (definition: OpenApiSchema, model?: Model): strin // Ignore } } - return undefined; }; diff --git a/src/openApi/common/parser/sort.ts b/src/openApi/common/parser/sort.ts index 66c591980..6691cc27a 100644 --- a/src/openApi/common/parser/sort.ts +++ b/src/openApi/common/parser/sort.ts @@ -2,7 +2,7 @@ * Sort list of values and ensure that required parameters are first so that we do not generate * invalid types. Optional parameters cannot be positioned after required ones. */ -export function toSortedByRequired(values: T[]): T[] { +export function toSortedByRequired(values: T[]): T[] { return values.sort((a, b) => { const aNeedsValue = a.isRequired && a.default === undefined; const bNeedsValue = b.isRequired && b.default === undefined; diff --git a/src/openApi/v2/interfaces/OpenApiParameter.ts b/src/openApi/v2/interfaces/OpenApiParameter.ts index 10913d520..62c707f9d 100644 --- a/src/openApi/v2/interfaces/OpenApiParameter.ts +++ b/src/openApi/v2/interfaces/OpenApiParameter.ts @@ -29,7 +29,7 @@ export interface OpenApiParameter extends OpenApiReference, WithEnumExtension, W allowEmptyValue?: boolean; items?: OpenApiItems; collectionFormat?: 'csv' | 'ssv' | 'tsv' | 'pipes' | 'multi'; - default?: number; + default?: unknown; maximum?: number; exclusiveMaximum?: boolean; minimum?: number; diff --git a/src/openApi/v2/parser/getOperationParameter.ts b/src/openApi/v2/parser/getOperationParameter.ts index 3e43c4ba0..5c2c264e6 100644 --- a/src/openApi/v2/parser/getOperationParameter.ts +++ b/src/openApi/v2/parser/getOperationParameter.ts @@ -1,4 +1,5 @@ import type { OperationParameter } from '../../common/interfaces/client'; +import { getDefault } from '../../common/parser/getDefault'; import { getEnums } from '../../common/parser/getEnums'; import { getPattern } from '../../common/parser/getPattern'; import { getRef } from '../../common/parser/getRef'; @@ -8,7 +9,6 @@ import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { getModel } from './getModel'; -import { getOperationParameterDefault } from './getOperationParameterDefault'; export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => { const operationParameter: OperationParameter = { @@ -52,7 +52,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.base = definitionRef.base; operationParameter.template = definitionRef.template; operationParameter.imports.push(...definitionRef.imports); - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } @@ -63,7 +63,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.enum.push(...enums); operationParameter.export = 'enum'; operationParameter.type = 'string'; - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } } @@ -75,7 +75,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.base = items.base; operationParameter.template = items.template; operationParameter.imports.push(...items.imports); - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } @@ -86,7 +86,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.base = items.base; operationParameter.template = items.template; operationParameter.imports.push(...items.imports); - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } @@ -102,7 +102,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.base = model.base; operationParameter.template = model.template; operationParameter.imports.push(...model.imports); - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } else { const model = getModel(openApi, schema); @@ -115,7 +115,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.enum.push(...model.enum); operationParameter.enums.push(...model.enums); operationParameter.properties.push(...model.properties); - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } } @@ -128,7 +128,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.base = definitionType.base; operationParameter.template = definitionType.template; operationParameter.imports.push(...definitionType.imports); - operationParameter.default = getOperationParameterDefault(parameter, operationParameter); + operationParameter.default = getDefault(parameter, operationParameter); return operationParameter; } diff --git a/src/openApi/v2/parser/getOperationParameterDefault.ts b/src/openApi/v2/parser/getOperationParameterDefault.ts deleted file mode 100644 index 674fd72a4..000000000 --- a/src/openApi/v2/parser/getOperationParameterDefault.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { OperationParameter } from '../../common/interfaces/client'; -import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; - -export const getOperationParameterDefault = ( - parameter: OpenApiParameter, - operationParameter: OperationParameter -): string | undefined => { - if (parameter.default === undefined) { - return undefined; - } - - if (parameter.default === null) { - return 'null'; - } - - const type = parameter.type || typeof parameter.default; - - switch (type) { - case 'int': - case 'integer': - case 'number': - if (operationParameter.export === 'enum' && operationParameter.enum?.[parameter.default]) { - const { value } = operationParameter.enum[parameter.default]; - return typeof value === 'string' ? `'${value}'` : String(value); - } - return String(parameter.default); - - case 'boolean': - return JSON.stringify(parameter.default); - - case 'string': - return `'${parameter.default}'`; - - case 'object': - try { - return JSON.stringify(parameter.default, null, 4); - } catch (e) { - // Ignore - } - } - - return undefined; -}; diff --git a/src/openApi/v3/interfaces/OpenApiSchema.ts b/src/openApi/v3/interfaces/OpenApiSchema.ts index d1492e955..0a3f7e973 100644 --- a/src/openApi/v3/interfaces/OpenApiSchema.ts +++ b/src/openApi/v3/interfaces/OpenApiSchema.ts @@ -13,7 +13,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension { allOf?: OpenApiSchema[]; anyOf?: OpenApiSchema[]; const?: string | number | boolean | null; - default?: number; + default?: unknown; deprecated?: boolean; description?: string; discriminator?: OpenApiDiscriminator; diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index 58bc069c9..71af26743 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -1,11 +1,11 @@ import type { Model } from '../../common/interfaces/client'; +import { getDefault } from '../../common/parser/getDefault'; import { getEnums } from '../../common/parser/getEnums'; import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { findModelComposition, getModelComposition } from './getModelComposition'; -import { getModelDefault } from './getModelDefault'; import { getAdditionalPropertiesModel, getModelProperties } from './getModelProperties'; import { inferType } from './inferType'; @@ -59,7 +59,7 @@ export const getModel = ( model.imports = [...model.imports, ...definitionRef.imports]; model.template = definitionRef.template; model.type = definitionRef.type; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); return model; } @@ -70,7 +70,7 @@ export const getModel = ( model.enum = [...model.enum, ...enums]; model.export = 'enum'; model.type = 'string'; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); return model; } } @@ -84,7 +84,7 @@ export const getModel = ( model.imports = [...model.imports, ...arrayItems.imports]; model.template = arrayItems.template; model.type = arrayItems.type; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); return model; } @@ -112,7 +112,7 @@ export const getModel = ( model.link = arrayItems; model.template = arrayItems.template; model.type = arrayItems.type; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); return model; } @@ -133,7 +133,7 @@ export const getModel = ( model.base = 'any'; model.export = 'interface'; model.type = 'any'; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); const modelProperties = getModelProperties(openApi, definition, getModel, model); modelProperties.forEach(modelProperty => { @@ -176,7 +176,7 @@ export const getModel = ( model.isNullable = definitionType.isNullable || model.isNullable; model.template = definitionType.template; model.type = definitionType.type; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); return model; } diff --git a/src/openApi/v3/parser/getModelProperties.ts b/src/openApi/v3/parser/getModelProperties.ts index 45091d311..2be023fae 100644 --- a/src/openApi/v3/parser/getModelProperties.ts +++ b/src/openApi/v3/parser/getModelProperties.ts @@ -1,12 +1,12 @@ import { escapeName } from '../../../utils/escapeName'; import type { Model } from '../../common/interfaces/client'; +import { getDefault } from '../../common/parser/getDefault'; import { getPattern } from '../../common/parser/getPattern'; import { getType } from '../../common/parser/type'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { findOneOfParentDiscriminator, mapPropertyValue } from './discriminator'; import type { getModel } from './getModel'; -import { getModelDefault } from './getModelDefault'; // Fix for circular dependency export type GetModelFn = typeof getModel; @@ -21,7 +21,7 @@ export const getAdditionalPropertiesModel = ( const apModel = getModel(openApi, ap); if (definition.additionalProperties === true && definition.properties) { - apModel.default = getModelDefault(definition, model); + apModel.default = getDefault(definition, model); apModel.export = 'generic'; apModel.isRequired = true; apModel.name = '[key: string]'; @@ -31,7 +31,7 @@ export const getAdditionalPropertiesModel = ( if (ap.$ref) { const apType = getType(ap.$ref); model.base = apType.base; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); model.export = 'dictionary'; model.imports.push(...apType.imports); model.template = apType.template; @@ -40,7 +40,7 @@ export const getAdditionalPropertiesModel = ( } model.base = apModel.base; - model.default = getModelDefault(definition, model); + model.default = getDefault(definition, model); model.export = 'dictionary'; model.imports.push(...apModel.imports); model.link = apModel; @@ -78,6 +78,7 @@ export const getModelProperties = ( > = { deprecated: property.deprecated === true, description: property.description || null, + default: property.default, exclusiveMaximum: property.exclusiveMaximum, exclusiveMinimum: property.exclusiveMinimum, format: property.format, @@ -138,6 +139,7 @@ export const getModelProperties = ( enum: model.enum, enums: model.enums, export: model.export, + default: model.default, imports: model.imports, isNullable: model.isNullable || property.nullable === true, link: model.link, diff --git a/src/openApi/v3/parser/getOperationParameter.ts b/src/openApi/v3/parser/getOperationParameter.ts index ffd96f837..0783a3e08 100644 --- a/src/openApi/v3/parser/getOperationParameter.ts +++ b/src/openApi/v3/parser/getOperationParameter.ts @@ -1,4 +1,5 @@ import type { OperationParameter } from '../../common/interfaces/client'; +import { getDefault } from '../../common/parser/getDefault'; import { getPattern } from '../../common/parser/getPattern'; import { getRef } from '../../common/parser/getRef'; import { getOperationParameterName } from '../../common/parser/operation'; @@ -7,7 +8,6 @@ import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { getModel } from './getModel'; -import { getModelDefault } from './getModelDefault'; export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => { const operationParameter: OperationParameter = { @@ -57,7 +57,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame operationParameter.template = model.template; operationParameter.$refs = [...operationParameter.$refs, ...model.$refs]; operationParameter.imports = [...operationParameter.imports, ...model.imports]; - operationParameter.default = getModelDefault(schema); + operationParameter.default = getDefault(schema); return operationParameter; } else { const model = getModel(openApi, schema); diff --git a/src/templates/partials/schemaArray.hbs b/src/templates/partials/schemaArray.hbs index 44871265b..f49c04bb0 100644 --- a/src/templates/partials/schemaArray.hbs +++ b/src/templates/partials/schemaArray.hbs @@ -7,6 +7,9 @@ type: '{{{base}}}', }, {{/if}} +{{#notEquals default undefined}} + default: {{{default}}}, +{{/notEquals}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/src/templates/partials/schemaComposition.hbs b/src/templates/partials/schemaComposition.hbs index 050f6696d..c11f6bb2b 100644 --- a/src/templates/partials/schemaComposition.hbs +++ b/src/templates/partials/schemaComposition.hbs @@ -4,6 +4,9 @@ description: `{{{escapeDescription description}}}`, {{/if}} contains: [{{#each properties}}{{>schema}}{{#unless @last}}, {{/unless}}{{/each}}], +{{#notEquals default undefined}} + default: {{{default}}}, +{{/notEquals}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/src/templates/partials/schemaDictionary.hbs b/src/templates/partials/schemaDictionary.hbs index 1e755f7af..41a7441dd 100644 --- a/src/templates/partials/schemaDictionary.hbs +++ b/src/templates/partials/schemaDictionary.hbs @@ -7,6 +7,9 @@ type: '{{{base}}}', }, {{/if}} +{{#notEquals default undefined}} + default: {{{default}}}, +{{/notEquals}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/src/templates/partials/schemaEnum.hbs b/src/templates/partials/schemaEnum.hbs index eaba3fc5f..0ad81c5c6 100644 --- a/src/templates/partials/schemaEnum.hbs +++ b/src/templates/partials/schemaEnum.hbs @@ -1,5 +1,11 @@ { type: 'Enum', +{{#if enum}} + enum: [{{#each enum}}{{{enumValue value}}},{{/each}}], +{{/if}} +{{#notEquals default undefined}} + default: {{{default}}}, +{{/notEquals}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/src/templates/partials/schemaGeneric.hbs b/src/templates/partials/schemaGeneric.hbs index 92cbbe51b..a9d32ba98 100644 --- a/src/templates/partials/schemaGeneric.hbs +++ b/src/templates/partials/schemaGeneric.hbs @@ -5,6 +5,9 @@ {{#if description}} description: `{{{escapeDescription description}}}`, {{/if}} +{{#notEquals default undefined}} + default: {{{default}}}, +{{/notEquals}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/src/templates/partials/schemaInterface.hbs b/src/templates/partials/schemaInterface.hbs index d5a7def82..fc9216fad 100644 --- a/src/templates/partials/schemaInterface.hbs +++ b/src/templates/partials/schemaInterface.hbs @@ -11,6 +11,9 @@ {{/each}} {{/if}} }, +{{#notEquals default undefined}} + default: {{{default}}}, +{{/notEquals}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, {{/if}} diff --git a/test/__snapshots__/v2/schemas.ts.snap b/test/__snapshots__/v2/schemas.ts.snap index 1e4bb11d8..77ac1586b 100644 --- a/test/__snapshots__/v2/schemas.ts.snap +++ b/test/__snapshots__/v2/schemas.ts.snap @@ -70,10 +70,12 @@ export const $SimpleStringWithPattern = { export const $EnumWithStrings = { type: 'Enum', + enum: ['Success', 'Warning', 'Error', "'Single Quote'", '"Double Quotes"', 'Non-ascii: øæåôöØÆÅÔÖ字符串'], } as const; export const $EnumWithNumbers = { type: 'Enum', + enum: [1, 2, 3, 1.1, 1.2, 1.3, 100, 200, 300, -100, -200, -300, -1.1, -1.2, -1.3], } as const; export const $EnumFromDescription = { @@ -83,6 +85,7 @@ export const $EnumFromDescription = { export const $EnumWithExtensions = { type: 'Enum', + enum: [200, 400, 500], } as const; export const $ArrayWithNumbers = { @@ -242,9 +245,11 @@ export const $ModelWithEnum = { properties: { test: { type: 'Enum', + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], }, statusCode: { type: 'Enum', + enum: ['100', '200 FOO', '300 FOO_BAR', '400 foo-bar', '500 foo.bar', '600 foo&bar'], }, bool: { type: 'boolean', @@ -270,6 +275,7 @@ export const $ModelWithNestedEnums = { type: 'dictionary', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, dictionaryWithEnumFromDescription: { @@ -283,6 +289,7 @@ export const $ModelWithNestedEnums = { type: 'array', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, arrayWithDescription: { diff --git a/test/__snapshots__/v3/schemas.ts.snap b/test/__snapshots__/v3/schemas.ts.snap index ca5a0e98f..d68433294 100644 --- a/test/__snapshots__/v3/schemas.ts.snap +++ b/test/__snapshots__/v3/schemas.ts.snap @@ -71,14 +71,18 @@ export const $SimpleStringWithPattern = { export const $EnumWithStrings = { type: 'Enum', + enum: ['Success', 'Warning', 'Error', "'Single Quote'", '"Double Quotes"', 'Non-ascii: øæåôöØÆÅÔÖ字符串'], } as const; export const $EnumWithReplacedCharacters = { type: 'Enum', + enum: ["'Single Quote'", '"Double Quotes"', 'øæåôöØÆÅÔÖ字符串', 3.1, ''], } as const; export const $EnumWithNumbers = { type: 'Enum', + enum: [1, 2, 3, 1.1, 1.2, 1.3, 100, 200, 300, -100, -200, -300, -1.1, -1.2, -1.3], + default: 200, } as const; export const $EnumFromDescription = { @@ -88,6 +92,7 @@ export const $EnumFromDescription = { export const $EnumWithExtensions = { type: 'Enum', + enum: [200, 400, 500], } as const; export const $ArrayWithNumbers = { @@ -109,6 +114,7 @@ export const $ArrayWithStrings = { contains: { type: 'string', }, + default: ['test'], } as const; export const $ArrayWithReferences = { @@ -151,6 +157,7 @@ export const $ArrayWithAnyOfProperties = { properties: { foo: { type: 'string', + default: 'test', }, }, }, @@ -320,9 +327,11 @@ export const $ModelWithEnum = { properties: { test: { type: 'Enum', + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], }, statusCode: { type: 'Enum', + enum: ['100', '200 FOO', '300 FOO_BAR', '400 foo-bar', '500 foo.bar', '600 foo&bar'], }, bool: { type: 'boolean', @@ -336,6 +345,8 @@ export const $ModelWithEnumWithHyphen = { properties: { 'foo-bar-baz-qux': { type: 'Enum', + enum: ['3.0'], + default: '3.0', }, }, } as const; @@ -357,6 +368,7 @@ export const $ModelWithNestedEnums = { type: 'dictionary', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, dictionaryWithEnumFromDescription: { @@ -370,6 +382,7 @@ export const $ModelWithNestedEnums = { type: 'array', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, arrayWithDescription: { @@ -650,6 +663,7 @@ export const $CompositionWithNestedAnyAndTypeNull = { export const $Enum1 = { type: 'Enum', + enum: ['Bird', 'Dog'], } as const; export const $ConstValue = { @@ -1126,6 +1140,7 @@ export const $Pageable = { properties: { page: { type: 'number', + default: 0, format: 'int32', minimum: 0, }, @@ -1273,6 +1288,7 @@ export const $NullableObject = { type: 'string', }, }, + default: null, isNullable: true, } as const; @@ -1291,6 +1307,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Bar'], isRequired: true, }, }, @@ -1299,6 +1316,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Baz'], isRequired: true, }, }, @@ -1307,6 +1325,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Qux'], isRequired: true, }, }, @@ -1320,6 +1339,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Quux'], isRequired: true, }, }, @@ -1344,6 +1364,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Corge'], isRequired: true, }, }, @@ -1353,10 +1374,12 @@ export const $ModelWithOneOfEnum = { export const $ModelWithNestedArrayEnumsDataFoo = { type: 'Enum', + enum: ['foo', 'bar'], } as const; export const $ModelWithNestedArrayEnumsDataBar = { type: 'Enum', + enum: ['baz', 'qux'], } as const; export const $ModelWithNestedArrayEnumsData = { diff --git a/test/__snapshots__/v3_angular/schemas.ts.snap b/test/__snapshots__/v3_angular/schemas.ts.snap index ca5a0e98f..d68433294 100644 --- a/test/__snapshots__/v3_angular/schemas.ts.snap +++ b/test/__snapshots__/v3_angular/schemas.ts.snap @@ -71,14 +71,18 @@ export const $SimpleStringWithPattern = { export const $EnumWithStrings = { type: 'Enum', + enum: ['Success', 'Warning', 'Error', "'Single Quote'", '"Double Quotes"', 'Non-ascii: øæåôöØÆÅÔÖ字符串'], } as const; export const $EnumWithReplacedCharacters = { type: 'Enum', + enum: ["'Single Quote'", '"Double Quotes"', 'øæåôöØÆÅÔÖ字符串', 3.1, ''], } as const; export const $EnumWithNumbers = { type: 'Enum', + enum: [1, 2, 3, 1.1, 1.2, 1.3, 100, 200, 300, -100, -200, -300, -1.1, -1.2, -1.3], + default: 200, } as const; export const $EnumFromDescription = { @@ -88,6 +92,7 @@ export const $EnumFromDescription = { export const $EnumWithExtensions = { type: 'Enum', + enum: [200, 400, 500], } as const; export const $ArrayWithNumbers = { @@ -109,6 +114,7 @@ export const $ArrayWithStrings = { contains: { type: 'string', }, + default: ['test'], } as const; export const $ArrayWithReferences = { @@ -151,6 +157,7 @@ export const $ArrayWithAnyOfProperties = { properties: { foo: { type: 'string', + default: 'test', }, }, }, @@ -320,9 +327,11 @@ export const $ModelWithEnum = { properties: { test: { type: 'Enum', + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], }, statusCode: { type: 'Enum', + enum: ['100', '200 FOO', '300 FOO_BAR', '400 foo-bar', '500 foo.bar', '600 foo&bar'], }, bool: { type: 'boolean', @@ -336,6 +345,8 @@ export const $ModelWithEnumWithHyphen = { properties: { 'foo-bar-baz-qux': { type: 'Enum', + enum: ['3.0'], + default: '3.0', }, }, } as const; @@ -357,6 +368,7 @@ export const $ModelWithNestedEnums = { type: 'dictionary', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, dictionaryWithEnumFromDescription: { @@ -370,6 +382,7 @@ export const $ModelWithNestedEnums = { type: 'array', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, arrayWithDescription: { @@ -650,6 +663,7 @@ export const $CompositionWithNestedAnyAndTypeNull = { export const $Enum1 = { type: 'Enum', + enum: ['Bird', 'Dog'], } as const; export const $ConstValue = { @@ -1126,6 +1140,7 @@ export const $Pageable = { properties: { page: { type: 'number', + default: 0, format: 'int32', minimum: 0, }, @@ -1273,6 +1288,7 @@ export const $NullableObject = { type: 'string', }, }, + default: null, isNullable: true, } as const; @@ -1291,6 +1307,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Bar'], isRequired: true, }, }, @@ -1299,6 +1316,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Baz'], isRequired: true, }, }, @@ -1307,6 +1325,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Qux'], isRequired: true, }, }, @@ -1320,6 +1339,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Quux'], isRequired: true, }, }, @@ -1344,6 +1364,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Corge'], isRequired: true, }, }, @@ -1353,10 +1374,12 @@ export const $ModelWithOneOfEnum = { export const $ModelWithNestedArrayEnumsDataFoo = { type: 'Enum', + enum: ['foo', 'bar'], } as const; export const $ModelWithNestedArrayEnumsDataBar = { type: 'Enum', + enum: ['baz', 'qux'], } as const; export const $ModelWithNestedArrayEnumsData = { diff --git a/test/__snapshots__/v3_enums_typescript/schemas.ts.snap b/test/__snapshots__/v3_enums_typescript/schemas.ts.snap index ca5a0e98f..d68433294 100644 --- a/test/__snapshots__/v3_enums_typescript/schemas.ts.snap +++ b/test/__snapshots__/v3_enums_typescript/schemas.ts.snap @@ -71,14 +71,18 @@ export const $SimpleStringWithPattern = { export const $EnumWithStrings = { type: 'Enum', + enum: ['Success', 'Warning', 'Error', "'Single Quote'", '"Double Quotes"', 'Non-ascii: øæåôöØÆÅÔÖ字符串'], } as const; export const $EnumWithReplacedCharacters = { type: 'Enum', + enum: ["'Single Quote'", '"Double Quotes"', 'øæåôöØÆÅÔÖ字符串', 3.1, ''], } as const; export const $EnumWithNumbers = { type: 'Enum', + enum: [1, 2, 3, 1.1, 1.2, 1.3, 100, 200, 300, -100, -200, -300, -1.1, -1.2, -1.3], + default: 200, } as const; export const $EnumFromDescription = { @@ -88,6 +92,7 @@ export const $EnumFromDescription = { export const $EnumWithExtensions = { type: 'Enum', + enum: [200, 400, 500], } as const; export const $ArrayWithNumbers = { @@ -109,6 +114,7 @@ export const $ArrayWithStrings = { contains: { type: 'string', }, + default: ['test'], } as const; export const $ArrayWithReferences = { @@ -151,6 +157,7 @@ export const $ArrayWithAnyOfProperties = { properties: { foo: { type: 'string', + default: 'test', }, }, }, @@ -320,9 +327,11 @@ export const $ModelWithEnum = { properties: { test: { type: 'Enum', + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], }, statusCode: { type: 'Enum', + enum: ['100', '200 FOO', '300 FOO_BAR', '400 foo-bar', '500 foo.bar', '600 foo&bar'], }, bool: { type: 'boolean', @@ -336,6 +345,8 @@ export const $ModelWithEnumWithHyphen = { properties: { 'foo-bar-baz-qux': { type: 'Enum', + enum: ['3.0'], + default: '3.0', }, }, } as const; @@ -357,6 +368,7 @@ export const $ModelWithNestedEnums = { type: 'dictionary', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, dictionaryWithEnumFromDescription: { @@ -370,6 +382,7 @@ export const $ModelWithNestedEnums = { type: 'array', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, arrayWithDescription: { @@ -650,6 +663,7 @@ export const $CompositionWithNestedAnyAndTypeNull = { export const $Enum1 = { type: 'Enum', + enum: ['Bird', 'Dog'], } as const; export const $ConstValue = { @@ -1126,6 +1140,7 @@ export const $Pageable = { properties: { page: { type: 'number', + default: 0, format: 'int32', minimum: 0, }, @@ -1273,6 +1288,7 @@ export const $NullableObject = { type: 'string', }, }, + default: null, isNullable: true, } as const; @@ -1291,6 +1307,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Bar'], isRequired: true, }, }, @@ -1299,6 +1316,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Baz'], isRequired: true, }, }, @@ -1307,6 +1325,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Qux'], isRequired: true, }, }, @@ -1320,6 +1339,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Quux'], isRequired: true, }, }, @@ -1344,6 +1364,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Corge'], isRequired: true, }, }, @@ -1353,10 +1374,12 @@ export const $ModelWithOneOfEnum = { export const $ModelWithNestedArrayEnumsDataFoo = { type: 'Enum', + enum: ['foo', 'bar'], } as const; export const $ModelWithNestedArrayEnumsDataBar = { type: 'Enum', + enum: ['baz', 'qux'], } as const; export const $ModelWithNestedArrayEnumsData = { diff --git a/test/__snapshots__/v3_experimental/schemas.ts.snap b/test/__snapshots__/v3_experimental/schemas.ts.snap index ca5a0e98f..d68433294 100644 --- a/test/__snapshots__/v3_experimental/schemas.ts.snap +++ b/test/__snapshots__/v3_experimental/schemas.ts.snap @@ -71,14 +71,18 @@ export const $SimpleStringWithPattern = { export const $EnumWithStrings = { type: 'Enum', + enum: ['Success', 'Warning', 'Error', "'Single Quote'", '"Double Quotes"', 'Non-ascii: øæåôöØÆÅÔÖ字符串'], } as const; export const $EnumWithReplacedCharacters = { type: 'Enum', + enum: ["'Single Quote'", '"Double Quotes"', 'øæåôöØÆÅÔÖ字符串', 3.1, ''], } as const; export const $EnumWithNumbers = { type: 'Enum', + enum: [1, 2, 3, 1.1, 1.2, 1.3, 100, 200, 300, -100, -200, -300, -1.1, -1.2, -1.3], + default: 200, } as const; export const $EnumFromDescription = { @@ -88,6 +92,7 @@ export const $EnumFromDescription = { export const $EnumWithExtensions = { type: 'Enum', + enum: [200, 400, 500], } as const; export const $ArrayWithNumbers = { @@ -109,6 +114,7 @@ export const $ArrayWithStrings = { contains: { type: 'string', }, + default: ['test'], } as const; export const $ArrayWithReferences = { @@ -151,6 +157,7 @@ export const $ArrayWithAnyOfProperties = { properties: { foo: { type: 'string', + default: 'test', }, }, }, @@ -320,9 +327,11 @@ export const $ModelWithEnum = { properties: { test: { type: 'Enum', + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], }, statusCode: { type: 'Enum', + enum: ['100', '200 FOO', '300 FOO_BAR', '400 foo-bar', '500 foo.bar', '600 foo&bar'], }, bool: { type: 'boolean', @@ -336,6 +345,8 @@ export const $ModelWithEnumWithHyphen = { properties: { 'foo-bar-baz-qux': { type: 'Enum', + enum: ['3.0'], + default: '3.0', }, }, } as const; @@ -357,6 +368,7 @@ export const $ModelWithNestedEnums = { type: 'dictionary', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, dictionaryWithEnumFromDescription: { @@ -370,6 +382,7 @@ export const $ModelWithNestedEnums = { type: 'array', contains: { type: 'Enum', + enum: ['Success', 'Warning', 'Error'], }, }, arrayWithDescription: { @@ -650,6 +663,7 @@ export const $CompositionWithNestedAnyAndTypeNull = { export const $Enum1 = { type: 'Enum', + enum: ['Bird', 'Dog'], } as const; export const $ConstValue = { @@ -1126,6 +1140,7 @@ export const $Pageable = { properties: { page: { type: 'number', + default: 0, format: 'int32', minimum: 0, }, @@ -1273,6 +1288,7 @@ export const $NullableObject = { type: 'string', }, }, + default: null, isNullable: true, } as const; @@ -1291,6 +1307,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Bar'], isRequired: true, }, }, @@ -1299,6 +1316,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Baz'], isRequired: true, }, }, @@ -1307,6 +1325,7 @@ export const $ModelWithOneOfEnum = { properties: { foo: { type: 'Enum', + enum: ['Qux'], isRequired: true, }, }, @@ -1320,6 +1339,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Quux'], isRequired: true, }, }, @@ -1344,6 +1364,7 @@ export const $ModelWithOneOfEnum = { }, foo: { type: 'Enum', + enum: ['Corge'], isRequired: true, }, }, @@ -1353,10 +1374,12 @@ export const $ModelWithOneOfEnum = { export const $ModelWithNestedArrayEnumsDataFoo = { type: 'Enum', + enum: ['foo', 'bar'], } as const; export const $ModelWithNestedArrayEnumsDataBar = { type: 'Enum', + enum: ['baz', 'qux'], } as const; export const $ModelWithNestedArrayEnumsData = { diff --git a/test/spec/v3.json b/test/spec/v3.json index 4258cf089..a32dc03fb 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -1799,7 +1799,8 @@ -1.1, -1.20, -1.300 - ] + ], + "default": 200 }, "EnumFromDescription": { "description": "Success=1,Warning=2,Error=3", @@ -1842,7 +1843,8 @@ "type": "array", "items": { "type": "string" - } + }, + "default": ["test"] }, "ArrayWithReferences": { "description": "This is a simple array with references", @@ -1885,7 +1887,8 @@ "type": "object", "properties": { "foo": { - "type": "string" + "type": "string", + "default": "test" } } }, @@ -2930,7 +2933,8 @@ "page": { "minimum": 0, "type": "integer", - "format": "int32" + "format": "int32", + "default": 0 }, "size": { "minimum": 1, @@ -3067,7 +3071,8 @@ "foo": { "type": "string" } - } + }, + "default": null }, "ModelWithNullableObject": { "type": "object",