From e70a61e40fe62e039643da0c28e4030302b97a70 Mon Sep 17 00:00:00 2001 From: Robert Adkins Date: Fri, 13 Oct 2023 18:18:17 -0400 Subject: [PATCH] add support for constant values --- src/client/interfaces/Model.d.ts | 12 ++- src/openApi/v2/interfaces/OpenApiSchema.d.ts | 1 + src/openApi/v2/parser/getModel.ts | 9 +++ src/openApi/v3/interfaces/OpenApiSchema.d.ts | 1 + src/openApi/v3/parser/getModel.ts | 9 +++ test/__snapshots__/index.spec.ts.snap | 82 ++++++++++++++++++++ test/spec/v2.json | 17 ++++ test/spec/v3.json | 17 ++++ 8 files changed, 147 insertions(+), 1 deletion(-) diff --git a/src/client/interfaces/Model.d.ts b/src/client/interfaces/Model.d.ts index 5f0318942..90ddb26f4 100644 --- a/src/client/interfaces/Model.d.ts +++ b/src/client/interfaces/Model.d.ts @@ -3,7 +3,17 @@ import type { Schema } from './Schema'; export interface Model extends Schema { name: string; - export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface' | 'one-of' | 'any-of' | 'all-of'; + export: + | 'reference' + | 'generic' + | 'enum' + | 'array' + | 'dictionary' + | 'interface' + | 'one-of' + | 'any-of' + | 'all-of' + | 'const'; type: string; base: string; template: string | null; diff --git a/src/openApi/v2/interfaces/OpenApiSchema.d.ts b/src/openApi/v2/interfaces/OpenApiSchema.d.ts index 09970dbb6..07c34dba3 100644 --- a/src/openApi/v2/interfaces/OpenApiSchema.d.ts +++ b/src/openApi/v2/interfaces/OpenApiSchema.d.ts @@ -28,6 +28,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension, With required?: string[]; enum?: (string | number)[]; type?: string; + const?: string | number | boolean | null; format?: | 'int32' | 'int64' diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 22f3528aa..0cffd97f9 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -149,5 +149,14 @@ export const getModel = ( return model; } + if (definition.const !== undefined) { + model.export = 'const'; + const definitionConst = definition.const; + const modelConst = typeof definitionConst === 'string' ? `"${definitionConst}"` : `${definitionConst}`; + model.type = modelConst; + model.base = modelConst; + return model; + } + return model; }; diff --git a/src/openApi/v3/interfaces/OpenApiSchema.d.ts b/src/openApi/v3/interfaces/OpenApiSchema.d.ts index a51456f3b..ff1b63b59 100644 --- a/src/openApi/v3/interfaces/OpenApiSchema.d.ts +++ b/src/openApi/v3/interfaces/OpenApiSchema.d.ts @@ -26,6 +26,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension { required?: string[]; enum?: (string | number)[]; type?: string | string[]; + const?: string | number | boolean | null; allOf?: OpenApiSchema[]; oneOf?: OpenApiSchema[]; anyOf?: OpenApiSchema[]; diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index 9e9c60a98..f9b0b43a3 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -192,5 +192,14 @@ export const getModel = ( return model; } + if (definition.const !== undefined) { + model.export = 'const'; + const definitionConst = definition.const; + const modelConst = typeof definitionConst === 'string' ? `"${definitionConst}"` : `${definitionConst}`; + model.type = modelConst; + model.base = modelConst; + return model; + } + return model; }; diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index ef6b18554..4d9c55f8e 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -598,6 +598,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends'; export type { ModelWithArray } from './models/ModelWithArray'; export type { ModelWithBoolean } from './models/ModelWithBoolean'; export type { ModelWithCircularReference } from './models/ModelWithCircularReference'; +export type { ModelWithConst } from './models/ModelWithConst'; export type { ModelWithDictionary } from './models/ModelWithDictionary'; export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports'; export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties'; @@ -647,6 +648,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends'; export { $ModelWithArray } from './schemas/$ModelWithArray'; export { $ModelWithBoolean } from './schemas/$ModelWithBoolean'; export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference'; +export { $ModelWithConst } from './schemas/$ModelWithConst'; export { $ModelWithDictionary } from './schemas/$ModelWithDictionary'; export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports'; export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties'; @@ -1097,6 +1099,21 @@ export type ModelWithCircularReference = { " `; +exports[`v2 should generate: test/generated/v2/models/ModelWithConst.ts 1`] = ` +"/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export type ModelWithConst = { + string?: "string"; + number?: 0; + boolean?: false; + null?: null; +}; + +" +`; + exports[`v2 should generate: test/generated/v2/models/ModelWithDictionary.ts 1`] = ` "/* generated using openapi-typescript-codegen -- do no edit */ /* istanbul ignore file */ @@ -1896,6 +1913,30 @@ export const $ModelWithCircularReference = { " `; +exports[`v2 should generate: test/generated/v2/schemas/$ModelWithConst.ts 1`] = ` +"/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $ModelWithConst = { + properties: { + string: { + type: '"string"', + }, + number: { + type: '0', + }, + boolean: { + type: 'false', + }, + null: { + type: 'null', + }, + }, +} as const; +" +`; + exports[`v2 should generate: test/generated/v2/schemas/$ModelWithDictionary.ts 1`] = ` "/* generated using openapi-typescript-codegen -- do no edit */ /* istanbul ignore file */ @@ -3710,6 +3751,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends'; export type { ModelWithArray } from './models/ModelWithArray'; export type { ModelWithBoolean } from './models/ModelWithBoolean'; export type { ModelWithCircularReference } from './models/ModelWithCircularReference'; +export type { ModelWithConst } from './models/ModelWithConst'; export type { ModelWithDictionary } from './models/ModelWithDictionary'; export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports'; export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties'; @@ -3780,6 +3822,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends'; export { $ModelWithArray } from './schemas/$ModelWithArray'; export { $ModelWithBoolean } from './schemas/$ModelWithBoolean'; export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference'; +export { $ModelWithConst } from './schemas/$ModelWithConst'; export { $ModelWithDictionary } from './schemas/$ModelWithDictionary'; export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports'; export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties'; @@ -4555,6 +4598,21 @@ export type ModelWithCircularReference = { " `; +exports[`v3 should generate: test/generated/v3/models/ModelWithConst.ts 1`] = ` +"/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export type ModelWithConst = { + string?: "string"; + number?: 0; + boolean?: false; + null?: null; +}; + +" +`; + exports[`v3 should generate: test/generated/v3/models/ModelWithDictionary.ts 1`] = ` "/* generated using openapi-typescript-codegen -- do no edit */ /* istanbul ignore file */ @@ -5866,6 +5924,30 @@ export const $ModelWithCircularReference = { " `; +exports[`v3 should generate: test/generated/v3/schemas/$ModelWithConst.ts 1`] = ` +"/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $ModelWithConst = { + properties: { + string: { + type: '"string"', + }, + number: { + type: '0', + }, + boolean: { + type: 'false', + }, + null: { + type: 'null', + }, + }, +} as const; +" +`; + exports[`v3 should generate: test/generated/v3/schemas/$ModelWithDictionary.ts 1`] = ` "/* generated using openapi-typescript-codegen -- do no edit */ /* istanbul ignore file */ diff --git a/test/spec/v2.json b/test/spec/v2.json index e8eb19b51..cc43d7b2d 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -1513,6 +1513,23 @@ "pattern": "^[a-zA-Z0-9']*$" } } + }, + "ModelWithConst": { + "type": "object", + "properties": { + "string": { + "const": "string" + }, + "number": { + "const": 0 + }, + "boolean": { + "const": false + }, + "null": { + "const": null + } + } } } } diff --git a/test/spec/v3.json b/test/spec/v3.json index cb590d0b7..a5e14faf1 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -2553,6 +2553,23 @@ "description": "This is a free-form object with additionalProperties: {}.", "type": "object", "additionalProperties": {} + }, + "ModelWithConst": { + "type": "object", + "properties": { + "string": { + "const": "string" + }, + "number": { + "const": 0 + }, + "boolean": { + "const": false + }, + "null": { + "const": null + } + } } } }