Skip to content

Commit 2a205fc

Browse files
Add support for anyOf/allOf/oneOf when node has explicit primitive type (#919)
* Add support for anyOf/allOf/oneOf for primitives * Add tests for type string with allOf * Update format of test examples
1 parent 8ac7465 commit 2a205fc

File tree

7 files changed

+181
-8
lines changed

7 files changed

+181
-8
lines changed

src/utils.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ export function nodeType(obj: any): SchemaObjectType {
151151
return "enum";
152152
}
153153

154+
// Treat any node with allOf/ anyOf/ oneOf as object
155+
if (obj.hasOwnProperty("allOf") || obj.hasOwnProperty("anyOf") || obj.hasOwnProperty("oneOf")) {
156+
return "object";
157+
}
158+
154159
// boolean
155160
if (obj.type === "boolean") {
156161
return "boolean";
@@ -179,14 +184,7 @@ export function nodeType(obj: any): SchemaObjectType {
179184
}
180185

181186
// object
182-
if (
183-
obj.type === "object" ||
184-
obj.hasOwnProperty("allOf") ||
185-
obj.hasOwnProperty("anyOf") ||
186-
obj.hasOwnProperty("oneOf") ||
187-
obj.hasOwnProperty("properties") ||
188-
obj.hasOwnProperty("additionalProperties")
189-
) {
187+
if (obj.type === "object" || obj.hasOwnProperty("properties") || obj.hasOwnProperty("additionalProperties")) {
190188
return "object";
191189
}
192190

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
'/test': {
8+
get: {
9+
responses: {
10+
/** A list of types. */
11+
200: unknown
12+
}
13+
}
14+
}
15+
}
16+
17+
export interface components {
18+
schemas: {
19+
/** @description Object with one property that is a string enum */
20+
Example: {
21+
status?: components['schemas']['ExampleStatus'] & { [key: string]: unknown }
22+
} & { [key: string]: unknown }
23+
/** @enum {string} */
24+
ExampleStatus: 'ACTIVE' | 'INACTIVE'
25+
}
26+
}
27+
28+
export interface operations {}
29+
30+
export interface external {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export type paths = {
7+
'/test': {
8+
get: {
9+
responses: {
10+
/** A list of types. */
11+
200: unknown
12+
}
13+
}
14+
}
15+
}
16+
17+
export type components = {
18+
schemas: {
19+
/** @description Object with one property that is a string enum */
20+
Example: {
21+
status?: components['schemas']['ExampleStatus']
22+
}
23+
/** @enum {string} */
24+
ExampleStatus: 'ACTIVE' | 'INACTIVE'
25+
}
26+
}
27+
28+
export type operations = {}
29+
30+
export type external = {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
readonly '/test': {
8+
readonly get: {
9+
readonly responses: {
10+
/** A list of types. */
11+
readonly 200: unknown
12+
}
13+
}
14+
}
15+
}
16+
17+
export interface components {
18+
readonly schemas: {
19+
/** @description Object with one property that is a string enum */
20+
readonly Example: {
21+
readonly status?: components['schemas']['ExampleStatus']
22+
}
23+
/** @enum {string} */
24+
readonly ExampleStatus: 'ACTIVE' | 'INACTIVE'
25+
}
26+
}
27+
28+
export interface operations {}
29+
30+
export interface external {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
'/test': {
8+
get: {
9+
responses: {
10+
/** A list of types. */
11+
200: unknown
12+
}
13+
}
14+
}
15+
}
16+
17+
export interface components {
18+
schemas: {
19+
/** @description Object with one property that is a string enum */
20+
Example: {
21+
status?: components['schemas']['ExampleStatus']
22+
}
23+
/** @enum {string} */
24+
ExampleStatus: 'ACTIVE' | 'INACTIVE'
25+
}
26+
}
27+
28+
export interface operations {}
29+
30+
export interface external {}

test/v3/expected/all-of-string.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
'/test': {
8+
get: {
9+
responses: {
10+
/** A list of types. */
11+
200: unknown
12+
}
13+
}
14+
}
15+
}
16+
17+
export interface components {
18+
schemas: {
19+
/** @description Object with one property that is a string enum */
20+
Example: {
21+
status?: components['schemas']['ExampleStatus']
22+
}
23+
/** @enum {string} */
24+
ExampleStatus: 'ACTIVE' | 'INACTIVE'
25+
}
26+
}
27+
28+
export interface operations {}
29+
30+
export interface external {}

test/v3/specs/all-of-string.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0
2+
paths:
3+
/test:
4+
get:
5+
tags:
6+
- test
7+
summary: "Just a test path"
8+
responses:
9+
200:
10+
description: A list of types.
11+
components:
12+
schemas:
13+
Example:
14+
description: Object with one property that is a string enum
15+
type: object
16+
properties:
17+
status:
18+
type: string
19+
allOf:
20+
- $ref: '#/components/schemas/ExampleStatus'
21+
ExampleStatus:
22+
type: string
23+
enum:
24+
- ACTIVE
25+
- INACTIVE

0 commit comments

Comments
 (0)