Skip to content

Commit e4cfa9e

Browse files
committed
feat: respect titles for oneOf variants
1 parent d41fd46 commit e4cfa9e

5 files changed

Lines changed: 59 additions & 19 deletions

File tree

src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
300300
"pattern": undefined,
301301
"pointer": "#/components/schemas/Dog/properties/packSize",
302302
"rawSchema": {
303-
"default": undefined,
304303
"type": "number",
305304
},
306305
"readOnly": false,
@@ -309,7 +308,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
309308
"#/components/schemas/Dog/properties/packSize",
310309
],
311310
"schema": {
312-
"default": undefined,
313311
"type": "number",
314312
},
315313
"title": "",
@@ -576,7 +574,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
576574
"pattern": undefined,
577575
"pointer": "#/components/schemas/Dog/properties/type",
578576
"rawSchema": {
579-
"default": undefined,
580577
"type": "string",
581578
"x-refsStack": [
582579
"#/components/schemas/Dog",
@@ -593,7 +590,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
593590
"#/components/schemas/Dog/properties/type",
594591
],
595592
"schema": {
596-
"default": undefined,
597593
"type": "string",
598594
"x-refsStack": [
599595
"#/components/schemas/Dog",
@@ -1164,7 +1160,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
11641160
"pattern": undefined,
11651161
"pointer": "#/components/schemas/Cat/properties/type",
11661162
"rawSchema": {
1167-
"default": undefined,
11681163
"type": "string",
11691164
"x-refsStack": [
11701165
"#/components/schemas/Cat",
@@ -1181,7 +1176,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
11811176
"#/components/schemas/Cat/properties/type",
11821177
],
11831178
"schema": {
1184-
"default": undefined,
11851179
"type": "string",
11861180
"x-refsStack": [
11871181
"#/components/schemas/Cat",
@@ -1452,7 +1446,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
14521446
"pattern": undefined,
14531447
"pointer": "#/components/schemas/Cat/properties/packSize",
14541448
"rawSchema": {
1455-
"default": undefined,
14561449
"type": "number",
14571450
"x-refsStack": [
14581451
"#/components/schemas/Cat",
@@ -1466,7 +1459,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
14661459
"#/components/schemas/Cat/properties/packSize",
14671460
],
14681461
"schema": {
1469-
"default": undefined,
14701462
"type": "number",
14711463
"x-refsStack": [
14721464
"#/components/schemas/Cat",
@@ -2309,7 +2301,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
23092301
"pattern": undefined,
23102302
"pointer": "#/components/schemas/Dog/properties/packSize",
23112303
"rawSchema": {
2312-
"default": undefined,
23132304
"type": "number",
23142305
},
23152306
"readOnly": false,
@@ -2318,7 +2309,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
23182309
"#/components/schemas/Dog/properties/packSize",
23192310
],
23202311
"schema": {
2321-
"default": undefined,
23222312
"type": "number",
23232313
},
23242314
"title": "",
@@ -2585,7 +2575,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
25852575
"pattern": undefined,
25862576
"pointer": "#/components/schemas/Dog/properties/type",
25872577
"rawSchema": {
2588-
"default": undefined,
25892578
"type": "string",
25902579
"x-refsStack": [
25912580
"#/components/schemas/Dog",
@@ -2602,7 +2591,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
26022591
"#/components/schemas/Dog/properties/type",
26032592
],
26042593
"schema": {
2605-
"default": undefined,
26062594
"type": "string",
26072595
"x-refsStack": [
26082596
"#/components/schemas/Dog",
@@ -2945,7 +2933,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
29452933
"pattern": undefined,
29462934
"pointer": "#/components/schemas/Dog/properties/packSize",
29472935
"rawSchema": {
2948-
"default": undefined,
29492936
"type": "number",
29502937
},
29512938
"readOnly": false,
@@ -2954,7 +2941,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
29542941
"#/components/schemas/Dog/properties/packSize",
29552942
],
29562943
"schema": {
2957-
"default": undefined,
29582944
"type": "number",
29592945
},
29602946
"title": "",
@@ -3008,7 +2994,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
30082994
"pattern": undefined,
30092995
"pointer": "#/components/schemas/Dog/properties/type",
30102996
"rawSchema": {
3011-
"default": undefined,
30122997
"type": "string",
30132998
"x-refsStack": [
30142999
"#/components/schemas/Dog",
@@ -3025,7 +3010,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
30253010
"#/components/schemas/Dog/properties/type",
30263011
],
30273012
"schema": {
3028-
"default": undefined,
30293013
"type": "string",
30303014
"x-refsStack": [
30313015
"#/components/schemas/Dog",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "Test",
5+
"version": "1.0.0"
6+
},
7+
"components": {
8+
"schemas": {
9+
"Response": {
10+
"type": "object",
11+
"properties": {
12+
"data": {
13+
"$ref": "#/components/schemas/Data"
14+
}
15+
}
16+
},
17+
"Data": {
18+
"type": "object",
19+
"oneOf": [
20+
{ "$ref": "#/components/schemas/WithTitle" },
21+
{ "$ref": "#/components/schemas/NoTitle" },
22+
{
23+
"title": "InlineWithTitle",
24+
"type": "object",
25+
"properties": {
26+
"inline": { "type": "string" }
27+
}
28+
}
29+
]
30+
},
31+
"WithTitle": {
32+
"title": "ExplicitTitle",
33+
"type": "object"
34+
},
35+
"NoTitle": {
36+
"type": "object"
37+
}
38+
}
39+
}
40+
}

src/services/__tests__/models/Schema.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ describe('Models', () => {
5656
expect(schema.displayType).toBe('(Array of strings or numbers) or string');
5757
});
5858

59+
test('nested oneOf titles: explicit, inferred from $ref, and inline with title', () => {
60+
const spec = require('../fixtures/nestedOneOf.json');
61+
parser = new OpenAPIParser(spec, undefined, opts);
62+
const schema = new SchemaModel(parser, spec.components.schemas.Response, '', opts);
63+
64+
const dataField = schema.fields?.find(f => f.name === 'data');
65+
expect(dataField).toBeDefined();
66+
expect(dataField!.schema.oneOf).toHaveLength(3);
67+
68+
expect(dataField!.schema.oneOf![0].title).toBe('ExplicitTitle');
69+
expect(dataField!.schema.oneOf![1].title).toBe('NoTitle');
70+
expect(dataField!.schema.oneOf![2].title).toBe('InlineWithTitle');
71+
});
72+
5973
test('schemaDefinition should resolve double ref', () => {
6074
const spec = require('../fixtures/3.1/schemaDefinition.json');
6175
parser = new OpenAPIParser(spec, undefined, opts);

src/services/__tests__/models/__snapshots__/Schema.test.ts.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
exports[`Models Schema schemaDefinition should resolve field with conditional operators 1`] = `
44
{
55
"allOf": undefined,
6-
"default": undefined,
76
"description": undefined,
87
"items": {
98
"allOf": undefined,
@@ -31,7 +30,6 @@ exports[`Models Schema schemaDefinition should resolve field with conditional op
3130
exports[`Models Schema schemaDefinition should resolve field with conditional operators 2`] = `
3231
{
3332
"allOf": undefined,
34-
"default": undefined,
3533
"description": undefined,
3634
"items": {
3735
"allOf": undefined,

src/services/models/Schema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,18 @@ function buildFields(
447447
const required =
448448
schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1;
449449

450+
const defaultValue = field.default === undefined && defaults ? defaults[fieldName] : field.default;
451+
450452
return new FieldModel(
451453
parser,
452454
{
453455
name: schema.properties ? fieldName : `[${fieldName}]`,
454456
required,
455457
schema: {
456458
...field,
457-
default: field.default === undefined && defaults ? defaults[fieldName] : field.default,
459+
// Only include default if it has an actual value; adding `default: undefined`
460+
// triggers allOf wrapping in OpenAPI 3.1's mergeRefs, breaking oneOf handling
461+
...(defaultValue !== undefined ? { default: defaultValue } : {}),
458462
},
459463
},
460464
$ref + '/properties/' + fieldName,

0 commit comments

Comments
 (0)