Skip to content

Commit

Permalink
add support for constant values
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Adkins committed Oct 13, 2023
1 parent 4de57e7 commit e70a61e
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/client/interfaces/Model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v2/interfaces/OpenApiSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions src/openApi/v2/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions src/openApi/v3/interfaces/OpenApiSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
9 changes: 9 additions & 0 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
82 changes: 82 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
17 changes: 17 additions & 0 deletions test/spec/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
}
17 changes: 17 additions & 0 deletions test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
}
Expand Down

0 comments on commit e70a61e

Please sign in to comment.