Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit ed23dca

Browse files
authored
fix: bug with improperly placed discriminators (#1211)
1 parent 5654f19 commit ed23dca

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

packages/api-explorer/__tests__/__fixtures__/polymorphism/discriminators.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,44 @@
7272
}
7373
}
7474
},
75+
"/oneOfWithImproperlyPlacedDiscriminator": {
76+
"patch": {
77+
"summary": "oneOf with a discriminator that is referencing a property up a level",
78+
"description": "This is an improper use of discriminators, but we should ignore the discriminator if this happens instead of failing to render the operation.",
79+
"requestBody": {
80+
"content": {
81+
"application/json": {
82+
"schema": {
83+
"type": "object",
84+
"properties": {
85+
"connector_type": {
86+
"type": "string",
87+
"enum": [
88+
"s3Import",
89+
"gcsImport"
90+
]
91+
},
92+
"connector_properties": {
93+
"type": "object",
94+
"oneOf": [
95+
{
96+
"$ref": "#/components/schemas/gcsImport"
97+
},
98+
{
99+
"$ref": "#/components/schemas/s3Import"
100+
}
101+
],
102+
"discriminator": {
103+
"propertyName": "connector_type"
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
112+
},
75113
"/pets": {
76114
"patch": {
77115
"summary": "oneOf request with a nested allOf and embedded discriminator",
@@ -359,6 +397,28 @@
359397
}
360398
}
361399
]
400+
},
401+
"gcsImport": {
402+
"type": "object",
403+
"properties": {
404+
"gcs_bucket": {
405+
"type": "string"
406+
},
407+
"gcs_prefix": {
408+
"type": "string"
409+
}
410+
}
411+
},
412+
"s3Import": {
413+
"type": "object",
414+
"properties": {
415+
"s3_bucket": {
416+
"type": "string"
417+
},
418+
"s3_prefix": {
419+
"type": "string"
420+
}
421+
}
362422
}
363423
}
364424
}

packages/api-explorer/__tests__/form-components/MultiSchemaField.test.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,19 @@ describe('discriminator', () => {
194194
expect(secondChangeParamsText).toContain('bark');
195195
expect(secondChangeParamsText).not.toContain('hunts');
196196
});
197+
198+
it('should ignore the discriminator if it is improperly placed', async () => {
199+
const testOas = new Oas(discriminators);
200+
await testOas.dereference();
201+
const operation = testOas.operation('/oneOfWithImproperlyPlacedDiscriminator', 'patch');
202+
const Params = createParams(oas, operation);
203+
204+
expect(() => {
205+
return mount(
206+
<div>
207+
<Params {...props} formData={{}} oas={testOas} operation={operation} />
208+
</div>
209+
);
210+
}).not.toThrow("Cannot use 'in' operator to search for '$ref' in null");
211+
});
197212
});

packages/api-explorer/src/form-components/MultiSchemaField.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ class MultiSchemaField extends Component {
241241
const SchemaField = registry.fields.SchemaField;
242242
const { selectedIndex, selectedValue, discriminatorSchema, discriminatorFieldSchema, enumOptions } = this.state;
243243

244-
// We've got a custom path if there's a discriminator, otherwise we fall back ot the old multischema field
245-
if (discriminatorSchema) {
246-
// Find which schema we wnat to render by looking at the options prop. The order of these options matches the
244+
// We've got a custom path if there's a properly-formed discriminator, otherwise we fall back ot the old MultiSchema
245+
// field.
246+
if (discriminatorFieldSchema && discriminatorSchema) {
247+
// Find which schema we want to render by looking at the options prop. The order of these options matches the
247248
// order of the dropdown options, and so we can just stick with matching indicies.
248249
const option = options[selectedIndex] || null;
249250
let optionSchema;

0 commit comments

Comments
 (0)