Skip to content

Commit e70a61e

Browse files
author
Robert Adkins
committed
add support for constant values
1 parent 4de57e7 commit e70a61e

File tree

8 files changed

+147
-1
lines changed

8 files changed

+147
-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/v2/interfaces/OpenApiSchema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension, With
2828
required?: string[];
2929
enum?: (string | number)[];
3030
type?: string;
31+
const?: string | number | boolean | null;
3132
format?:
3233
| 'int32'
3334
| 'int64'

src/openApi/v2/parser/getModel.ts

+9
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,14 @@ export const getModel = (
149149
return model;
150150
}
151151

152+
if (definition.const !== undefined) {
153+
model.export = 'const';
154+
const definitionConst = definition.const;
155+
const modelConst = typeof definitionConst === 'string' ? `"${definitionConst}"` : `${definitionConst}`;
156+
model.type = modelConst;
157+
model.base = modelConst;
158+
return model;
159+
}
160+
152161
return model;
153162
};

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

+82
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
598598
export type { ModelWithArray } from './models/ModelWithArray';
599599
export type { ModelWithBoolean } from './models/ModelWithBoolean';
600600
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
601+
export type { ModelWithConst } from './models/ModelWithConst';
601602
export type { ModelWithDictionary } from './models/ModelWithDictionary';
602603
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
603604
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
@@ -647,6 +648,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends';
647648
export { $ModelWithArray } from './schemas/$ModelWithArray';
648649
export { $ModelWithBoolean } from './schemas/$ModelWithBoolean';
649650
export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference';
651+
export { $ModelWithConst } from './schemas/$ModelWithConst';
650652
export { $ModelWithDictionary } from './schemas/$ModelWithDictionary';
651653
export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports';
652654
export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties';
@@ -1097,6 +1099,21 @@ export type ModelWithCircularReference = {
10971099
"
10981100
`;
10991101

1102+
exports[`v2 should generate: test/generated/v2/models/ModelWithConst.ts 1`] = `
1103+
"/* generated using openapi-typescript-codegen -- do no edit */
1104+
/* istanbul ignore file */
1105+
/* tslint:disable */
1106+
/* eslint-disable */
1107+
export type ModelWithConst = {
1108+
string?: "string";
1109+
number?: 0;
1110+
boolean?: false;
1111+
null?: null;
1112+
};
1113+
1114+
"
1115+
`;
1116+
11001117
exports[`v2 should generate: test/generated/v2/models/ModelWithDictionary.ts 1`] = `
11011118
"/* generated using openapi-typescript-codegen -- do no edit */
11021119
/* istanbul ignore file */
@@ -1896,6 +1913,30 @@ export const $ModelWithCircularReference = {
18961913
"
18971914
`;
18981915

1916+
exports[`v2 should generate: test/generated/v2/schemas/$ModelWithConst.ts 1`] = `
1917+
"/* generated using openapi-typescript-codegen -- do no edit */
1918+
/* istanbul ignore file */
1919+
/* tslint:disable */
1920+
/* eslint-disable */
1921+
export const $ModelWithConst = {
1922+
properties: {
1923+
string: {
1924+
type: '"string"',
1925+
},
1926+
number: {
1927+
type: '0',
1928+
},
1929+
boolean: {
1930+
type: 'false',
1931+
},
1932+
null: {
1933+
type: 'null',
1934+
},
1935+
},
1936+
} as const;
1937+
"
1938+
`;
1939+
18991940
exports[`v2 should generate: test/generated/v2/schemas/$ModelWithDictionary.ts 1`] = `
19001941
"/* generated using openapi-typescript-codegen -- do no edit */
19011942
/* istanbul ignore file */
@@ -3710,6 +3751,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
37103751
export type { ModelWithArray } from './models/ModelWithArray';
37113752
export type { ModelWithBoolean } from './models/ModelWithBoolean';
37123753
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
3754+
export type { ModelWithConst } from './models/ModelWithConst';
37133755
export type { ModelWithDictionary } from './models/ModelWithDictionary';
37143756
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
37153757
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
@@ -3780,6 +3822,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends';
37803822
export { $ModelWithArray } from './schemas/$ModelWithArray';
37813823
export { $ModelWithBoolean } from './schemas/$ModelWithBoolean';
37823824
export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference';
3825+
export { $ModelWithConst } from './schemas/$ModelWithConst';
37833826
export { $ModelWithDictionary } from './schemas/$ModelWithDictionary';
37843827
export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports';
37853828
export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties';
@@ -4555,6 +4598,21 @@ export type ModelWithCircularReference = {
45554598
"
45564599
`;
45574600

4601+
exports[`v3 should generate: test/generated/v3/models/ModelWithConst.ts 1`] = `
4602+
"/* generated using openapi-typescript-codegen -- do no edit */
4603+
/* istanbul ignore file */
4604+
/* tslint:disable */
4605+
/* eslint-disable */
4606+
export type ModelWithConst = {
4607+
string?: "string";
4608+
number?: 0;
4609+
boolean?: false;
4610+
null?: null;
4611+
};
4612+
4613+
"
4614+
`;
4615+
45584616
exports[`v3 should generate: test/generated/v3/models/ModelWithDictionary.ts 1`] = `
45594617
"/* generated using openapi-typescript-codegen -- do no edit */
45604618
/* istanbul ignore file */
@@ -5866,6 +5924,30 @@ export const $ModelWithCircularReference = {
58665924
"
58675925
`;
58685926

5927+
exports[`v3 should generate: test/generated/v3/schemas/$ModelWithConst.ts 1`] = `
5928+
"/* generated using openapi-typescript-codegen -- do no edit */
5929+
/* istanbul ignore file */
5930+
/* tslint:disable */
5931+
/* eslint-disable */
5932+
export const $ModelWithConst = {
5933+
properties: {
5934+
string: {
5935+
type: '"string"',
5936+
},
5937+
number: {
5938+
type: '0',
5939+
},
5940+
boolean: {
5941+
type: 'false',
5942+
},
5943+
null: {
5944+
type: 'null',
5945+
},
5946+
},
5947+
} as const;
5948+
"
5949+
`;
5950+
58695951
exports[`v3 should generate: test/generated/v3/schemas/$ModelWithDictionary.ts 1`] = `
58705952
"/* generated using openapi-typescript-codegen -- do no edit */
58715953
/* istanbul ignore file */

test/spec/v2.json

+17
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,23 @@
15131513
"pattern": "^[a-zA-Z0-9']*$"
15141514
}
15151515
}
1516+
},
1517+
"ModelWithConst": {
1518+
"type": "object",
1519+
"properties": {
1520+
"string": {
1521+
"const": "string"
1522+
},
1523+
"number": {
1524+
"const": 0
1525+
},
1526+
"boolean": {
1527+
"const": false
1528+
},
1529+
"null": {
1530+
"const": null
1531+
}
1532+
}
15161533
}
15171534
}
15181535
}

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)