Skip to content

Commit 0e324e8

Browse files
author
Robert Adkins
committed
add support for constant values
1 parent 32fde13 commit 0e324e8

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

src/client/interfaces/Model.d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import type { Schema } from './Schema';
33

44
export interface Model extends Schema {
55
name: string;
6-
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface' | 'one-of' | 'any-of' | 'all-of';
6+
export:
7+
| 'reference'
8+
| 'generic'
9+
| 'enum'
10+
| 'array'
11+
| 'dictionary'
12+
| 'interface'
13+
| 'one-of'
14+
| 'any-of'
15+
| 'all-of'
16+
| 'const';
717
type: string;
818
base: string;
919
template: string | null;

src/openApi/v3/interfaces/OpenApiSchema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension {
2626
required?: string[];
2727
enum?: (string | number)[];
2828
type?: string | string[];
29+
const?: string | number | boolean | null;
2930
allOf?: OpenApiSchema[];
3031
oneOf?: OpenApiSchema[];
3132
anyOf?: OpenApiSchema[];

src/openApi/v3/parser/getModel.ts

+9
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,14 @@ export const getModel = (
192192
return model;
193193
}
194194

195+
if (definition.const !== undefined) {
196+
model.export = 'const';
197+
const definitionConst = definition.const;
198+
const modelConst = typeof definitionConst === 'string' ? `"${definitionConst}"` : `${definitionConst}`;
199+
model.type = modelConst;
200+
model.base = modelConst;
201+
return model;
202+
}
203+
195204
return model;
196205
};

test/__snapshots__/index.spec.ts.snap

+41
Original file line numberDiff line numberDiff line change
@@ -3710,6 +3710,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
37103710
export type { ModelWithArray } from './models/ModelWithArray';
37113711
export type { ModelWithBoolean } from './models/ModelWithBoolean';
37123712
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
3713+
export type { ModelWithConst } from './models/ModelWithConst';
37133714
export type { ModelWithDictionary } from './models/ModelWithDictionary';
37143715
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
37153716
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
@@ -3780,6 +3781,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends';
37803781
export { $ModelWithArray } from './schemas/$ModelWithArray';
37813782
export { $ModelWithBoolean } from './schemas/$ModelWithBoolean';
37823783
export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference';
3784+
export { $ModelWithConst } from './schemas/$ModelWithConst';
37833785
export { $ModelWithDictionary } from './schemas/$ModelWithDictionary';
37843786
export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports';
37853787
export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties';
@@ -4555,6 +4557,21 @@ export type ModelWithCircularReference = {
45554557
"
45564558
`;
45574559

4560+
exports[`v3 should generate: test/generated/v3/models/ModelWithConst.ts 1`] = `
4561+
"/* generated using openapi-typescript-codegen -- do no edit */
4562+
/* istanbul ignore file */
4563+
/* tslint:disable */
4564+
/* eslint-disable */
4565+
export type ModelWithConst = {
4566+
string?: "string";
4567+
number?: 0;
4568+
boolean?: false;
4569+
null?: null;
4570+
};
4571+
4572+
"
4573+
`;
4574+
45584575
exports[`v3 should generate: test/generated/v3/models/ModelWithDictionary.ts 1`] = `
45594576
"/* generated using openapi-typescript-codegen -- do no edit */
45604577
/* istanbul ignore file */
@@ -5866,6 +5883,30 @@ export const $ModelWithCircularReference = {
58665883
"
58675884
`;
58685885

5886+
exports[`v3 should generate: test/generated/v3/schemas/$ModelWithConst.ts 1`] = `
5887+
"/* generated using openapi-typescript-codegen -- do no edit */
5888+
/* istanbul ignore file */
5889+
/* tslint:disable */
5890+
/* eslint-disable */
5891+
export const $ModelWithConst = {
5892+
properties: {
5893+
string: {
5894+
type: '"string"',
5895+
},
5896+
number: {
5897+
type: '0',
5898+
},
5899+
boolean: {
5900+
type: 'false',
5901+
},
5902+
null: {
5903+
type: 'null',
5904+
},
5905+
},
5906+
} as const;
5907+
"
5908+
`;
5909+
58695910
exports[`v3 should generate: test/generated/v3/schemas/$ModelWithDictionary.ts 1`] = `
58705911
"/* generated using openapi-typescript-codegen -- do no edit */
58715912
/* istanbul ignore file */

test/spec/v3.json

+17
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,23 @@
25532553
"description": "This is a free-form object with additionalProperties: {}.",
25542554
"type": "object",
25552555
"additionalProperties": {}
2556+
},
2557+
"ModelWithConst": {
2558+
"type": "object",
2559+
"properties": {
2560+
"string": {
2561+
"const": "string"
2562+
},
2563+
"number": {
2564+
"const": 0
2565+
},
2566+
"boolean": {
2567+
"const": false
2568+
},
2569+
"null": {
2570+
"const": null
2571+
}
2572+
}
25562573
}
25572574
}
25582575
}

0 commit comments

Comments
 (0)