Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/packSize",
"rawSchema": {
"default": undefined,
"type": "number",
},
"readOnly": false,
Expand All @@ -309,7 +308,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"#/components/schemas/Dog/properties/packSize",
],
"schema": {
"default": undefined,
"type": "number",
},
"title": "",
Expand Down Expand Up @@ -576,7 +574,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/type",
"rawSchema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Dog",
Expand All @@ -593,7 +590,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"#/components/schemas/Dog/properties/type",
],
"schema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Dog",
Expand Down Expand Up @@ -1164,7 +1160,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pattern": undefined,
"pointer": "#/components/schemas/Cat/properties/type",
"rawSchema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Cat",
Expand All @@ -1181,7 +1176,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"#/components/schemas/Cat/properties/type",
],
"schema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Cat",
Expand Down Expand Up @@ -1452,7 +1446,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pattern": undefined,
"pointer": "#/components/schemas/Cat/properties/packSize",
"rawSchema": {
"default": undefined,
"type": "number",
"x-refsStack": [
"#/components/schemas/Cat",
Expand All @@ -1466,7 +1459,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"#/components/schemas/Cat/properties/packSize",
],
"schema": {
"default": undefined,
"type": "number",
"x-refsStack": [
"#/components/schemas/Cat",
Expand Down Expand Up @@ -2309,7 +2301,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/packSize",
"rawSchema": {
"default": undefined,
"type": "number",
},
"readOnly": false,
Expand All @@ -2318,7 +2309,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"#/components/schemas/Dog/properties/packSize",
],
"schema": {
"default": undefined,
"type": "number",
},
"title": "",
Expand Down Expand Up @@ -2585,7 +2575,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/type",
"rawSchema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Dog",
Expand All @@ -2602,7 +2591,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"#/components/schemas/Dog/properties/type",
],
"schema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Dog",
Expand Down Expand Up @@ -2945,7 +2933,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/packSize",
"rawSchema": {
"default": undefined,
"type": "number",
},
"readOnly": false,
Expand All @@ -2954,7 +2941,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"#/components/schemas/Dog/properties/packSize",
],
"schema": {
"default": undefined,
"type": "number",
},
"title": "",
Expand Down Expand Up @@ -3008,7 +2994,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/type",
"rawSchema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Dog",
Expand All @@ -3025,7 +3010,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"#/components/schemas/Dog/properties/type",
],
"schema": {
"default": undefined,
"type": "string",
"x-refsStack": [
"#/components/schemas/Dog",
Expand Down
40 changes: 40 additions & 0 deletions src/services/__tests__/fixtures/oneOf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"openapi": "3.0.0",
"info": {
"title": "Test",
"version": "1.0.0"
},
"components": {
"schemas": {
"Response": {
"type": "object",
"properties": {
"data": {
"$ref": "#/components/schemas/Data"
}
}
},
"Data": {
"type": "object",
"oneOf": [
{ "$ref": "#/components/schemas/WithTitle" },
{ "$ref": "#/components/schemas/NoTitle" },
{
"title": "InlineWithTitle",
"type": "object",
"properties": {
"inline": { "type": "string" }
}
}
]
},
"WithTitle": {
"title": "ExplicitTitle",
"type": "object"
},
"NoTitle": {
"type": "object"
}
}
}
}
14 changes: 14 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ describe('Models', () => {
expect(schema.displayType).toBe('(Array of strings or numbers) or string');
});

test('oneOf titles: explicit, inferred from $ref, and inline with title', () => {
const spec = require('../fixtures/oneOf.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(parser, spec.components.schemas.Response, '', opts);

const dataField = schema.fields?.find(f => f.name === 'data');
expect(dataField).toBeDefined();
expect(dataField!.schema.oneOf).toHaveLength(3);

expect(dataField!.schema.oneOf![0].title).toBe('ExplicitTitle');
expect(dataField!.schema.oneOf![1].title).toBe('NoTitle');
expect(dataField!.schema.oneOf![2].title).toBe('InlineWithTitle');
});

test('schemaDefinition should resolve double ref', () => {
const spec = require('../fixtures/3.1/schemaDefinition.json');
parser = new OpenAPIParser(spec, undefined, opts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
exports[`Models Schema schemaDefinition should resolve field with conditional operators 1`] = `
{
"allOf": undefined,
"default": undefined,
"description": undefined,
"items": {
"allOf": undefined,
Expand Down Expand Up @@ -31,7 +30,6 @@ exports[`Models Schema schemaDefinition should resolve field with conditional op
exports[`Models Schema schemaDefinition should resolve field with conditional operators 2`] = `
{
"allOf": undefined,
"default": undefined,
"description": undefined,
"items": {
"allOf": undefined,
Expand Down
6 changes: 5 additions & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,18 @@ function buildFields(
const required =
schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1;

const defaultValue = field.default === undefined && defaults ? defaults[fieldName] : field.default;

return new FieldModel(
parser,
{
name: schema.properties ? fieldName : `[${fieldName}]`,
required,
schema: {
...field,
default: field.default === undefined && defaults ? defaults[fieldName] : field.default,
// Only include default if it has an actual value; adding `default: undefined`
// triggers allOf wrapping in OpenAPI 3.1's mergeRefs, breaking oneOf handling
...(defaultValue !== undefined ? { default: defaultValue } : {}),
},
},
$ref + '/properties/' + fieldName,
Expand Down