Skip to content

feature: add support for constant values #1835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/client/interfaces/Model.d.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/openApi/v3/interfaces/OpenApiSchema.d.ts
Original file line number Diff line number Diff line change
@@ -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[];
9 changes: 9 additions & 0 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
@@ -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;
};
41 changes: 41 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -3710,6 +3710,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';
@@ -3780,6 +3781,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';
@@ -4555,6 +4557,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 */
@@ -5866,6 +5883,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 */
17 changes: 17 additions & 0 deletions test/spec/v3.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}