diff --git a/packages/core/src/getters/object.ts b/packages/core/src/getters/object.ts index c246ed08a..ccacb0d83 100644 --- a/packages/core/src/getters/object.ts +++ b/packages/core/src/getters/object.ts @@ -1,15 +1,16 @@ import { ReferenceObject, SchemaObject } from 'openapi3-ts/oas30'; import { resolveExampleRefs, resolveObject, resolveValue } from '../resolvers'; -import { ContextSpecs, ScalarValue, SchemaType } from '../types'; +import { + ContextSpecs, + ScalarValue, + SchemaType, + SchemaWithConst, +} from '../types'; import { isBoolean, isReference, jsDoc, pascal } from '../utils'; import { combineSchemas } from './combine'; import { getKey } from './keys'; import { getRefInfo } from './ref'; -interface SchemaWithConst extends SchemaObject { - const: string; -} - /** * Return the output type from an object * diff --git a/packages/core/src/getters/scalar.ts b/packages/core/src/getters/scalar.ts index dfbf8a29b..c6a2ffdb1 100644 --- a/packages/core/src/getters/scalar.ts +++ b/packages/core/src/getters/scalar.ts @@ -1,5 +1,10 @@ import { SchemaObject } from 'openapi3-ts/oas30'; -import { ContextSpecs, ScalarValue, OutputClient } from '../types'; +import { + ContextSpecs, + ScalarValue, + OutputClient, + SchemaWithConst, +} from '../types'; import { escape, isString } from '../utils'; import { getArray } from './array'; import { getObject } from './object'; @@ -46,6 +51,11 @@ export const getScalar = ({ isEnum = true; } + const itemWithConst = item as SchemaWithConst; + if (itemWithConst.const) { + value = itemWithConst.const; + } + return { value: value + nullable, isEnum, @@ -60,8 +70,15 @@ export const getScalar = ({ } case 'boolean': + let value = 'boolean'; + + const itemWithConst = item as SchemaWithConst; + if (itemWithConst.const) { + value = itemWithConst.const; + } + return { - value: 'boolean' + nullable, + value: value + nullable, type: 'boolean', isEnum: false, schemas: [], @@ -109,6 +126,11 @@ export const getScalar = ({ } } + const itemWithConst = item as SchemaWithConst; + if (itemWithConst.const) { + value = `'${itemWithConst.const}'`; + } + return { value: value + nullable, isEnum, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 8ef641a1b..eb631e2b4 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -1053,3 +1053,7 @@ export type GeneratorApiBuilder = GeneratorApiOperations & { importsMock: GenerateMockImports; extraFiles: ClientFileBuilder[]; }; + +export interface SchemaWithConst extends SchemaObject { + const: string; +} diff --git a/tests/configs/default.config.ts b/tests/configs/default.config.ts index c8675f564..1f1cb8ed7 100644 --- a/tests/configs/default.config.ts +++ b/tests/configs/default.config.ts @@ -171,4 +171,12 @@ export default defineConfig({ mock: true, }, }, + const: { + input: '../specifications/const.yaml', + output: { + schemas: '../generated/default/const/model', + target: '../generated/default/const', + mock: true, + }, + }, }); diff --git a/tests/specifications/const.yaml b/tests/specifications/const.yaml new file mode 100644 index 000000000..eef4de41c --- /dev/null +++ b/tests/specifications/const.yaml @@ -0,0 +1,52 @@ +openapi: 3.1.0 +info: + title: Const + version: 1.0.0 +paths: {} +components: + schemas: + StringConst: + type: object + required: + - value + - valueWithoutType + - valueNullable + properties: + value: + type: string + const: string + valueWithoutType: + const: string + valueNullable: + type: + - string + - 'null' + const: string + BooleanConst: + type: object + required: + - value + - valueNullable + properties: + value: + type: boolean + const: true + valueNullable: + type: + - boolean + - 'null' + const: true + IntegerConst: + type: object + required: + - value + - valueNullable + properties: + value: + type: number + const: 1 + valueNullable: + type: + - number + - 'null' + const: 1