Skip to content

Commit ee5334f

Browse files
author
Eric Butler
authoredJan 24, 2024
feat: support for const types (#1180)
1 parent 48a1b6e commit ee5334f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
 

‎packages/core/src/generators/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const generateInterface = ({
4343
}
4444

4545
if (
46-
!generalJSTypesWithArray.includes(scalar.value) &&
46+
scalar.type === 'object' &&
4747
!context?.output.override?.useTypeOverInterfaces
4848
) {
4949
model += `export interface ${name} ${scalar.value}\n`;

‎packages/core/src/getters/object.ts

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { combineSchemas } from './combine';
66
import { getKey } from './keys';
77
import { getRefInfo } from './ref';
88

9+
interface SchemaWithConst extends SchemaObject {
10+
const: string;
11+
}
12+
913
/**
1014
* Return the output type from an object
1115
*
@@ -185,6 +189,19 @@ export const getObject = ({
185189
};
186190
}
187191

192+
const itemWithConst = item as SchemaWithConst;
193+
if (itemWithConst.const) {
194+
return {
195+
value: `'${itemWithConst.const}'` + nullable,
196+
imports: [],
197+
schemas: [],
198+
isEnum: false,
199+
type: 'string',
200+
isRef: false,
201+
hasReadonlyProps: item.readOnly || false,
202+
};
203+
}
204+
188205
return {
189206
value:
190207
(item.type === 'object' ? '{ [key: string]: any }' : 'unknown') +

‎tests/specifications/example-v3-1.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ components:
2626
type: string
2727
type: array
2828
title: Example tuple
29+
example_const:
30+
const: this_is_a_const
31+
example_enum:
32+
type: string
33+
enum:
34+
- enum1
35+
- enum2
2936
title: Test
3037
type: object

0 commit comments

Comments
 (0)
Please sign in to comment.