Skip to content

Commit cecfb47

Browse files
authored
fix(core): an object member cannot be declared optional (#2111)
Co-authored-by: Javier Garmon <javiergarmon@users.noreply.github.com>
1 parent 6914ccc commit cecfb47

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, expect, it } from 'vitest';
2-
import { ContextSpecs, GeneratorSchema } from '../types';
2+
import type { ContextSpecs, GeneratorSchema } from '../types';
33
import { generateInterface } from './interface';
4-
import { SchemaObject as SchemaObject31 } from 'openapi3-ts/oas31';
5-
import { SchemaObject as SchemaObject30 } from 'openapi3-ts/oas30';
4+
import type { SchemaObject as SchemaObject31 } from 'openapi3-ts/oas31';
5+
import type { SchemaObject as SchemaObject30 } from 'openapi3-ts/oas30';
66

77
describe('generateInterface', () => {
88
const context: ContextSpecs = {
@@ -26,6 +26,10 @@ describe('generateInterface', () => {
2626
type: 'integer',
2727
const: 1,
2828
},
29+
isError: {
30+
type: 'boolean',
31+
const: false,
32+
},
2933
},
3034
required: ['message', 'code'],
3135
};
@@ -42,6 +46,7 @@ describe('generateInterface', () => {
4246
model: `export const TestSchemaValue = {
4347
message: 'Invalid data',
4448
code: 1,
49+
isError: false,
4550
} as const;
4651
export type TestSchema = typeof TestSchemaValue;
4752
`,

packages/core/src/generators/interface.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { SchemaObject } from 'openapi3-ts/oas30';
1+
import type { SchemaObject } from 'openapi3-ts/oas30';
22
import { getScalar } from '../getters';
3-
import { ContextSpecs } from '../types';
3+
import type { ContextSpecs } from '../types';
44
import { jsDoc } from '../utils';
55

66
/**
@@ -51,7 +51,9 @@ export const generateInterface = ({
5151
Object.values(schema.properties).length > 0 &&
5252
Object.values(schema.properties).every((item) => 'const' in item)
5353
) {
54-
const mappedScalarValue = scalar.value.replaceAll(';', ',');
54+
const mappedScalarValue = scalar.value
55+
.replaceAll(';', ',')
56+
.replaceAll('?:', ':');
5557

5658
model += `export const ${name}Value = ${mappedScalarValue} as const;\nexport type ${name} = typeof ${name}Value;\n`;
5759
} else {

0 commit comments

Comments
 (0)