Skip to content

Commit 827581d

Browse files
authored
Merge branch 'master' into feat/response-204
2 parents 927cb35 + 1f8316c commit 827581d

File tree

6 files changed

+305
-2
lines changed

6 files changed

+305
-2
lines changed

src/client/interfaces/Model.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import type { Schema } from './Schema';
33

44
export interface Model extends Schema {
55
name: string;
6-
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface' | 'one-of' | 'any-of' | 'all-of';
6+
export:
7+
| 'reference'
8+
| 'generic'
9+
| 'enum'
10+
| 'array'
11+
| 'dictionary'
12+
| 'interface'
13+
| 'one-of'
14+
| 'any-of'
15+
| 'all-of'
16+
| 'const';
717
type: string;
818
base: string;
919
template: string | null;

src/openApi/v3/interfaces/OpenApiSchema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface OpenApiSchema extends OpenApiReference, WithEnumExtension {
2626
required?: string[];
2727
enum?: (string | number)[];
2828
type?: string | string[];
29+
const?: string | number | null;
2930
allOf?: OpenApiSchema[];
3031
oneOf?: OpenApiSchema[];
3132
anyOf?: OpenApiSchema[];
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { reservedWords } from '../../../utils/reservedWords';
2+
import { getModel } from './getModel';
3+
import { getType } from './getType';
4+
5+
const openApi = {
6+
openapi: '3.0',
7+
info: {
8+
title: 'dummy',
9+
version: '1.0',
10+
},
11+
paths: {},
12+
servers: [
13+
{
14+
url: 'https://localhost:8080/api',
15+
},
16+
],
17+
components: {
18+
schemas: {
19+
Enum1: {
20+
enum: ['Bird', 'Dog'],
21+
type: 'string',
22+
},
23+
ConstValue: {
24+
type: 'string',
25+
const: 'ConstValue',
26+
},
27+
CompositionWithAnyOfAndNull: {
28+
description:
29+
"This is a model with one property with a 'any of' relationship where the options are not $ref",
30+
type: 'object',
31+
properties: {
32+
propA: {
33+
anyOf: [
34+
{
35+
items: {
36+
anyOf: [
37+
{
38+
$ref: '#/components/schemas/Enum1',
39+
},
40+
{
41+
$ref: '#/components/schemas/ConstValue',
42+
},
43+
],
44+
},
45+
type: 'array',
46+
},
47+
{
48+
type: 'null',
49+
},
50+
],
51+
},
52+
},
53+
},
54+
CompositionWithAny: {
55+
description:
56+
"This is a model with one property with a 'any of' relationship where the options are not $ref",
57+
type: 'object',
58+
properties: {
59+
propA: {
60+
anyOf: [
61+
{
62+
$ref: '#/components/schemas/Enum1',
63+
},
64+
{
65+
$ref: '#/components/schemas/ConstValue',
66+
},
67+
{
68+
type: 'null',
69+
},
70+
],
71+
},
72+
},
73+
},
74+
},
75+
},
76+
};
77+
78+
describe('getModel', () => {
79+
it('Parses any of', () => {
80+
const definition = openApi.components.schemas.CompositionWithAnyOfAndNull;
81+
const definitionType = getType('CompositionWithAnyOfAndNull');
82+
const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1'));
83+
expect(model.properties[0].properties.length).toBe(2);
84+
});
85+
86+
it('Parses any of 2', () => {
87+
const definition = openApi.components.schemas.CompositionWithAny;
88+
const definitionType = getType('CompositionWithAny');
89+
const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1'));
90+
expect(model.properties[0].properties.length).toBe(3);
91+
});
92+
});

src/openApi/v3/parser/getModel.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export const getModel = (
8282
model.imports.push(...arrayItems.imports);
8383
model.default = getModelDefault(definition, model);
8484
return model;
85+
} else if (definition.items.anyOf) {
86+
return getModel(openApi, definition.items);
8587
} else {
8688
const arrayItems = getModel(openApi, definition.items);
8789
model.export = 'array';
@@ -179,6 +181,15 @@ export const getModel = (
179181
}
180182
}
181183

184+
if (definition.const !== undefined) {
185+
model.export = 'const';
186+
const definitionConst = definition.const;
187+
const modelConst = typeof definitionConst === 'string' ? `"${definitionConst}"` : `${definitionConst}`;
188+
model.type = modelConst;
189+
model.base = modelConst;
190+
return model;
191+
}
192+
182193
// If the schema has a type than it can be a basic or generic type.
183194
if (definition.type) {
184195
const definitionType = getType(definition.type, definition.format);

test/__snapshots__/index.spec.ts.snap

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,19 +3706,22 @@ export type { CompositionWithAllOfAndNullable } from './models/CompositionWithAl
37063706
export type { CompositionWithAnyOf } from './models/CompositionWithAnyOf';
37073707
export type { CompositionWithAnyOfAndNullable } from './models/CompositionWithAnyOfAndNullable';
37083708
export type { CompositionWithAnyOfAnonymous } from './models/CompositionWithAnyOfAnonymous';
3709+
export type { CompositionWithNestedAnyOfAndNull } from './models/CompositionWithNestedAnyOfAndNull';
37093710
export type { CompositionWithOneOf } from './models/CompositionWithOneOf';
37103711
export type { CompositionWithOneOfAndComplexArrayDictionary } from './models/CompositionWithOneOfAndComplexArrayDictionary';
37113712
export type { CompositionWithOneOfAndNullable } from './models/CompositionWithOneOfAndNullable';
37123713
export type { CompositionWithOneOfAndSimpleArrayDictionary } from './models/CompositionWithOneOfAndSimpleArrayDictionary';
37133714
export type { CompositionWithOneOfAndSimpleDictionary } from './models/CompositionWithOneOfAndSimpleDictionary';
37143715
export type { CompositionWithOneOfAnonymous } from './models/CompositionWithOneOfAnonymous';
37153716
export type { CompositionWithOneOfDiscriminator } from './models/CompositionWithOneOfDiscriminator';
3717+
export type { ConstValue } from './models/ConstValue';
37163718
export type { DeprecatedModel } from './models/DeprecatedModel';
37173719
export type { DictionaryWithArray } from './models/DictionaryWithArray';
37183720
export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary';
37193721
export type { DictionaryWithProperties } from './models/DictionaryWithProperties';
37203722
export type { DictionaryWithReference } from './models/DictionaryWithReference';
37213723
export type { DictionaryWithString } from './models/DictionaryWithString';
3724+
export { Enum1 } from './models/Enum1';
37223725
export type { EnumFromDescription } from './models/EnumFromDescription';
37233726
export { EnumWithExtensions } from './models/EnumWithExtensions';
37243727
export { EnumWithNumbers } from './models/EnumWithNumbers';
@@ -3734,6 +3737,7 @@ export type { ModelThatExtendsExtends } from './models/ModelThatExtendsExtends';
37343737
export type { ModelWithArray } from './models/ModelWithArray';
37353738
export type { ModelWithBoolean } from './models/ModelWithBoolean';
37363739
export type { ModelWithCircularReference } from './models/ModelWithCircularReference';
3740+
export type { ModelWithConst } from './models/ModelWithConst';
37373741
export type { ModelWithDictionary } from './models/ModelWithDictionary';
37383742
export type { ModelWithDuplicateImports } from './models/ModelWithDuplicateImports';
37393743
export type { ModelWithDuplicateProperties } from './models/ModelWithDuplicateProperties';
@@ -3776,19 +3780,22 @@ export { $CompositionWithAllOfAndNullable } from './schemas/$CompositionWithAllO
37763780
export { $CompositionWithAnyOf } from './schemas/$CompositionWithAnyOf';
37773781
export { $CompositionWithAnyOfAndNullable } from './schemas/$CompositionWithAnyOfAndNullable';
37783782
export { $CompositionWithAnyOfAnonymous } from './schemas/$CompositionWithAnyOfAnonymous';
3783+
export { $CompositionWithNestedAnyOfAndNull } from './schemas/$CompositionWithNestedAnyOfAndNull';
37793784
export { $CompositionWithOneOf } from './schemas/$CompositionWithOneOf';
37803785
export { $CompositionWithOneOfAndComplexArrayDictionary } from './schemas/$CompositionWithOneOfAndComplexArrayDictionary';
37813786
export { $CompositionWithOneOfAndNullable } from './schemas/$CompositionWithOneOfAndNullable';
37823787
export { $CompositionWithOneOfAndSimpleArrayDictionary } from './schemas/$CompositionWithOneOfAndSimpleArrayDictionary';
37833788
export { $CompositionWithOneOfAndSimpleDictionary } from './schemas/$CompositionWithOneOfAndSimpleDictionary';
37843789
export { $CompositionWithOneOfAnonymous } from './schemas/$CompositionWithOneOfAnonymous';
37853790
export { $CompositionWithOneOfDiscriminator } from './schemas/$CompositionWithOneOfDiscriminator';
3791+
export { $ConstValue } from './schemas/$ConstValue';
37863792
export { $DeprecatedModel } from './schemas/$DeprecatedModel';
37873793
export { $DictionaryWithArray } from './schemas/$DictionaryWithArray';
37883794
export { $DictionaryWithDictionary } from './schemas/$DictionaryWithDictionary';
37893795
export { $DictionaryWithProperties } from './schemas/$DictionaryWithProperties';
37903796
export { $DictionaryWithReference } from './schemas/$DictionaryWithReference';
37913797
export { $DictionaryWithString } from './schemas/$DictionaryWithString';
3798+
export { $Enum1 } from './schemas/$Enum1';
37923799
export { $EnumFromDescription } from './schemas/$EnumFromDescription';
37933800
export { $EnumWithExtensions } from './schemas/$EnumWithExtensions';
37943801
export { $EnumWithNumbers } from './schemas/$EnumWithNumbers';
@@ -3804,6 +3811,7 @@ export { $ModelThatExtendsExtends } from './schemas/$ModelThatExtendsExtends';
38043811
export { $ModelWithArray } from './schemas/$ModelWithArray';
38053812
export { $ModelWithBoolean } from './schemas/$ModelWithBoolean';
38063813
export { $ModelWithCircularReference } from './schemas/$ModelWithCircularReference';
3814+
export { $ModelWithConst } from './schemas/$ModelWithConst';
38073815
export { $ModelWithDictionary } from './schemas/$ModelWithDictionary';
38083816
export { $ModelWithDuplicateImports } from './schemas/$ModelWithDuplicateImports';
38093817
export { $ModelWithDuplicateProperties } from './schemas/$ModelWithDuplicateProperties';
@@ -4125,6 +4133,23 @@ export type CompositionWithAnyOfAnonymous = {
41254133
"
41264134
`;
41274135

4136+
exports[`v3 should generate: test/generated/v3/models/CompositionWithNestedAnyOfAndNull.ts 1`] = `
4137+
"/* generated using openapi-typescript-codegen -- do no edit */
4138+
/* istanbul ignore file */
4139+
/* tslint:disable */
4140+
/* eslint-disable */
4141+
import type { ConstValue } from './ConstValue';
4142+
import type { Enum1 } from './Enum1';
4143+
/**
4144+
* This is a model with one property with a 'any of' relationship where the options are not $ref
4145+
*/
4146+
export type CompositionWithNestedAnyOfAndNull = {
4147+
propA?: ((Enum1 | ConstValue) | null);
4148+
};
4149+
4150+
"
4151+
`;
4152+
41284153
exports[`v3 should generate: test/generated/v3/models/CompositionWithOneOf.ts 1`] = `
41294154
"/* generated using openapi-typescript-codegen -- do no edit */
41304155
/* istanbul ignore file */
@@ -4241,6 +4266,15 @@ export type CompositionWithOneOfDiscriminator = (ModelCircle | ModelSquare);
42414266
"
42424267
`;
42434268

4269+
exports[`v3 should generate: test/generated/v3/models/ConstValue.ts 1`] = `
4270+
"/* generated using openapi-typescript-codegen -- do no edit */
4271+
/* istanbul ignore file */
4272+
/* tslint:disable */
4273+
/* eslint-disable */
4274+
export type ConstValue = "ConstValue";
4275+
"
4276+
`;
4277+
42444278
exports[`v3 should generate: test/generated/v3/models/DeprecatedModel.ts 1`] = `
42454279
"/* generated using openapi-typescript-codegen -- do no edit */
42464280
/* istanbul ignore file */
@@ -4326,6 +4360,18 @@ export type DictionaryWithString = Record<string, string>;
43264360
"
43274361
`;
43284362

4363+
exports[`v3 should generate: test/generated/v3/models/Enum1.ts 1`] = `
4364+
"/* generated using openapi-typescript-codegen -- do no edit */
4365+
/* istanbul ignore file */
4366+
/* tslint:disable */
4367+
/* eslint-disable */
4368+
export enum Enum1 {
4369+
BIRD = 'Bird',
4370+
DOG = 'Dog',
4371+
}
4372+
"
4373+
`;
4374+
43294375
exports[`v3 should generate: test/generated/v3/models/EnumFromDescription.ts 1`] = `
43304376
"/* generated using openapi-typescript-codegen -- do no edit */
43314377
/* istanbul ignore file */
@@ -4579,6 +4625,21 @@ export type ModelWithCircularReference = {
45794625
"
45804626
`;
45814627

4628+
exports[`v3 should generate: test/generated/v3/models/ModelWithConst.ts 1`] = `
4629+
"/* generated using openapi-typescript-codegen -- do no edit */
4630+
/* istanbul ignore file */
4631+
/* tslint:disable */
4632+
/* eslint-disable */
4633+
export type ModelWithConst = {
4634+
String?: "String";
4635+
number?: 0;
4636+
null?: null;
4637+
withType?: "Some string";
4638+
};
4639+
4640+
"
4641+
`;
4642+
45824643
exports[`v3 should generate: test/generated/v3/models/ModelWithDictionary.ts 1`] = `
45834644
"/* generated using openapi-typescript-codegen -- do no edit */
45844645
/* istanbul ignore file */
@@ -5323,6 +5384,32 @@ export const $CompositionWithAnyOfAnonymous = {
53235384
"
53245385
`;
53255386

5387+
exports[`v3 should generate: test/generated/v3/schemas/$CompositionWithNestedAnyOfAndNull.ts 1`] = `
5388+
"/* generated using openapi-typescript-codegen -- do no edit */
5389+
/* istanbul ignore file */
5390+
/* tslint:disable */
5391+
/* eslint-disable */
5392+
export const $CompositionWithNestedAnyOfAndNull = {
5393+
description: \`This is a model with one property with a 'any of' relationship where the options are not $ref\`,
5394+
properties: {
5395+
propA: {
5396+
type: 'any-of',
5397+
contains: [{
5398+
type: 'any-of',
5399+
contains: [{
5400+
type: 'Enum1',
5401+
}, {
5402+
type: 'ConstValue',
5403+
}],
5404+
}, {
5405+
type: 'null',
5406+
}],
5407+
},
5408+
},
5409+
} as const;
5410+
"
5411+
`;
5412+
53265413
exports[`v3 should generate: test/generated/v3/schemas/$CompositionWithOneOf.ts 1`] = `
53275414
"/* generated using openapi-typescript-codegen -- do no edit */
53285415
/* istanbul ignore file */
@@ -5508,6 +5595,17 @@ export const $CompositionWithOneOfDiscriminator = {
55085595
"
55095596
`;
55105597

5598+
exports[`v3 should generate: test/generated/v3/schemas/$ConstValue.ts 1`] = `
5599+
"/* generated using openapi-typescript-codegen -- do no edit */
5600+
/* istanbul ignore file */
5601+
/* tslint:disable */
5602+
/* eslint-disable */
5603+
export const $ConstValue = {
5604+
type: '"ConstValue"',
5605+
} as const;
5606+
"
5607+
`;
5608+
55115609
exports[`v3 should generate: test/generated/v3/schemas/$DeprecatedModel.ts 1`] = `
55125610
"/* generated using openapi-typescript-codegen -- do no edit */
55135611
/* istanbul ignore file */
@@ -5608,6 +5706,17 @@ export const $DictionaryWithString = {
56085706
"
56095707
`;
56105708

5709+
exports[`v3 should generate: test/generated/v3/schemas/$Enum1.ts 1`] = `
5710+
"/* generated using openapi-typescript-codegen -- do no edit */
5711+
/* istanbul ignore file */
5712+
/* tslint:disable */
5713+
/* eslint-disable */
5714+
export const $Enum1 = {
5715+
type: 'Enum',
5716+
} as const;
5717+
"
5718+
`;
5719+
56115720
exports[`v3 should generate: test/generated/v3/schemas/$EnumFromDescription.ts 1`] = `
56125721
"/* generated using openapi-typescript-codegen -- do no edit */
56135722
/* istanbul ignore file */
@@ -5890,6 +5999,30 @@ export const $ModelWithCircularReference = {
58905999
"
58916000
`;
58926001

6002+
exports[`v3 should generate: test/generated/v3/schemas/$ModelWithConst.ts 1`] = `
6003+
"/* generated using openapi-typescript-codegen -- do no edit */
6004+
/* istanbul ignore file */
6005+
/* tslint:disable */
6006+
/* eslint-disable */
6007+
export const $ModelWithConst = {
6008+
properties: {
6009+
String: {
6010+
type: '"String"',
6011+
},
6012+
number: {
6013+
type: '0',
6014+
},
6015+
null: {
6016+
type: 'null',
6017+
},
6018+
withType: {
6019+
type: '"Some string"',
6020+
},
6021+
},
6022+
} as const;
6023+
"
6024+
`;
6025+
58936026
exports[`v3 should generate: test/generated/v3/schemas/$ModelWithDictionary.ts 1`] = `
58946027
"/* generated using openapi-typescript-codegen -- do no edit */
58956028
/* istanbul ignore file */

0 commit comments

Comments
 (0)