Skip to content

Commit

Permalink
feat: generate literal types for booleans and numbers (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
RinseV authored Jul 18, 2024
1 parent db1f65f commit e30c7fc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/getters/object.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { ReferenceObject, SchemaObject } from 'openapi3-ts/oas30';
import { resolveExampleRefs, resolveObject, resolveValue } from '../resolvers';
import { ContextSpecs, ScalarValue, SchemaType } from '../types';
import {
ContextSpecs,
ScalarValue,
SchemaType,
SchemaWithConst,
} from '../types';
import { isBoolean, isReference, jsDoc, pascal } from '../utils';
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
26 changes: 24 additions & 2 deletions packages/core/src/getters/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { SchemaObject } from 'openapi3-ts/oas30';
import { ContextSpecs, ScalarValue, OutputClient } from '../types';
import {
ContextSpecs,
ScalarValue,
OutputClient,
SchemaWithConst,
} from '../types';
import { escape, isString } from '../utils';
import { getArray } from './array';
import { getObject } from './object';
Expand Down Expand Up @@ -46,6 +51,11 @@ export const getScalar = ({
isEnum = true;
}

const itemWithConst = item as SchemaWithConst;
if (itemWithConst.const) {
value = itemWithConst.const;
}

return {
value: value + nullable,
isEnum,
Expand All @@ -60,8 +70,15 @@ export const getScalar = ({
}

case 'boolean':
let value = 'boolean';

const itemWithConst = item as SchemaWithConst;
if (itemWithConst.const) {
value = itemWithConst.const;
}

return {
value: 'boolean' + nullable,
value: value + nullable,
type: 'boolean',
isEnum: false,
schemas: [],
Expand Down Expand Up @@ -109,6 +126,11 @@ export const getScalar = ({
}
}

const itemWithConst = item as SchemaWithConst;
if (itemWithConst.const) {
value = `'${itemWithConst.const}'`;
}

return {
value: value + nullable,
isEnum,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,3 +1053,7 @@ export type GeneratorApiBuilder = GeneratorApiOperations & {
importsMock: GenerateMockImports;
extraFiles: ClientFileBuilder[];
};

export interface SchemaWithConst extends SchemaObject {
const: string;
}
8 changes: 8 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,12 @@ export default defineConfig({
mock: true,
},
},
const: {
input: '../specifications/const.yaml',
output: {
schemas: '../generated/default/const/model',
target: '../generated/default/const',
mock: true,
},
},
});
52 changes: 52 additions & 0 deletions tests/specifications/const.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
openapi: 3.1.0
info:
title: Const
version: 1.0.0
paths: {}
components:
schemas:
StringConst:
type: object
required:
- value
- valueWithoutType
- valueNullable
properties:
value:
type: string
const: string
valueWithoutType:
const: string
valueNullable:
type:
- string
- 'null'
const: string
BooleanConst:
type: object
required:
- value
- valueNullable
properties:
value:
type: boolean
const: true
valueNullable:
type:
- boolean
- 'null'
const: true
IntegerConst:
type: object
required:
- value
- valueNullable
properties:
value:
type: number
const: 1
valueNullable:
type:
- number
- 'null'
const: 1

0 comments on commit e30c7fc

Please sign in to comment.