Skip to content

Commit f06132d

Browse files
Merge pull request ferdikoomen#1 from CanoaPBC/support-const
Adding const support
2 parents 758826e + 94fceb2 commit f06132d

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

src/client/interfaces/Model.d.ts

Lines changed: 11 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,6 +3714,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
37143714
export type { ModelWithArray } from './models/ModelWithArray';
37153715
export type { ModelWithBoolean } from './models/ModelWithBoolean';
37163716
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
3717+
export type { ModelWithConst } from './models/ModelWithConst';
37173718
export type { ModelWithDictionary } from './models/ModelWithDictionary';
37183719
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
37193720
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
@@ -3784,6 +3785,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends';
37843785
export { $ModelWithArray } from './schemas/$ModelWithArray';
37853786
export { $ModelWithBoolean } from './schemas/$ModelWithBoolean';
37863787
export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference';
3788+
export { $ModelWithConst } from './schemas/$ModelWithConst';
37873789
export { $ModelWithDictionary } from './schemas/$ModelWithDictionary';
37883790
export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports';
37893791
export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties';
@@ -4559,6 +4561,21 @@ export type ModelWithCircularReference = {
45594561
"
45604562
`;
45614563

4564+
exports[`v3 should generate: test/generated/v3/models/ModelWithConst.ts 1`] = `
4565+
"/* generated using openapi-typescript-codegen -- do no edit */
4566+
/* istanbul ignore file */
4567+
/* tslint:disable */
4568+
/* eslint-disable */
4569+
export type ModelWithConst = {
4570+
string?: "string";
4571+
number?: 0;
4572+
boolean?: false;
4573+
null?: null;
4574+
};
4575+
4576+
"
4577+
`;
4578+
45624579
exports[`v3 should generate: test/generated/v3/models/ModelWithDictionary.ts 1`] = `
45634580
"/* generated using openapi-typescript-codegen -- do no edit */
45644581
/* istanbul ignore file */
@@ -5870,6 +5887,30 @@ export const $ModelWithCircularReference = {
58705887
"
58715888
`;
58725889

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

test/spec/v3.json

Lines changed: 17 additions & 0 deletions
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)