Skip to content

Commit

Permalink
feat: support for const types (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Butler authored Jan 24, 2024
1 parent 48a1b6e commit ee5334f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/generators/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const generateInterface = ({
}

if (
!generalJSTypesWithArray.includes(scalar.value) &&
scalar.type === 'object' &&
!context?.output.override?.useTypeOverInterfaces
) {
model += `export interface ${name} ${scalar.value}\n`;
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/getters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { combineSchemas } from './combine';
import { getKey } from './keys';
import { getRefInfo } from './ref';

interface SchemaWithConst extends SchemaObject {
const: string;
}

/**
* Return the output type from an object
*
Expand Down Expand Up @@ -185,6 +189,19 @@ export const getObject = ({
};
}

const itemWithConst = item as SchemaWithConst;
if (itemWithConst.const) {
return {
value: `'${itemWithConst.const}'` + nullable,
imports: [],
schemas: [],
isEnum: false,
type: 'string',
isRef: false,
hasReadonlyProps: item.readOnly || false,
};
}

return {
value:
(item.type === 'object' ? '{ [key: string]: any }' : 'unknown') +
Expand Down
7 changes: 7 additions & 0 deletions tests/specifications/example-v3-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ components:
type: string
type: array
title: Example tuple
example_const:
const: this_is_a_const
example_enum:
type: string
enum:
- enum1
- enum2
title: Test
type: object

0 comments on commit ee5334f

Please sign in to comment.