Skip to content

Commit

Permalink
- Fix rjsf-team#2538 by fixing additionalProperties to deal with allO…
Browse files Browse the repository at this point in the history
…f/anyOf/oneOf
  • Loading branch information
heath-freenome committed Jan 22, 2023
1 parent df49d1e commit c39279c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
ADDITIONAL_PROPERTIES_KEY,
ADDITIONAL_PROPERTY_FLAG,
ALL_OF_KEY,
ANY_OF_KEY,
DEPENDENCIES_KEY,
ONE_OF_KEY,
REF_KEY,
} from "../constants";
import findSchemaDefinition, {
Expand Down Expand Up @@ -205,6 +207,15 @@ export function stubExistingAdditionalProperties<
);
} else if ("type" in schema.additionalProperties!) {
additionalProperties = { ...schema.additionalProperties };
} else if (
ALL_OF_KEY in schema.additionalProperties! ||
ANY_OF_KEY in schema.additionalProperties! ||
ONE_OF_KEY in schema.additionalProperties!
) {
additionalProperties = {
type: "object",
...schema.additionalProperties,
};
} else {
additionalProperties = { type: guessType(get(formData, [key])) };
}
Expand Down
87 changes: 87 additions & 0 deletions packages/utils/test/schema/retrieveSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,93 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
});
});

it("should `resolve` and stub out a schema which contains an `additionalProperties` with oneOf", () => {
const oneOf: RJSFSchema[] = [
{
type: "string",
},
{
type: "number",
},
];
const schema: RJSFSchema = {
additionalProperties: {
oneOf,
},
type: "object",
};

const formData = { newKey: {} };
expect(retrieveSchema(testValidator, schema, {}, formData)).toEqual({
...schema,
properties: {
newKey: {
type: "object",
oneOf,
[ADDITIONAL_PROPERTY_FLAG]: true,
},
},
});
});

it("should `resolve` and stub out a schema which contains an `additionalProperties` with anyOf", () => {
const anyOf: RJSFSchema[] = [
{
type: "string",
},
{
type: "number",
},
];
const schema: RJSFSchema = {
additionalProperties: {
anyOf,
},
type: "object",
};

const formData = { newKey: {} };
expect(retrieveSchema(testValidator, schema, {}, formData)).toEqual({
...schema,
properties: {
newKey: {
type: "object",
anyOf,
[ADDITIONAL_PROPERTY_FLAG]: true,
},
},
});
});

it("should `resolve` and stub out a schema which contains an `additionalProperties` with allOf", () => {
const allOf: RJSFSchema[] = [
{
type: "string",
},
{
type: "number",
},
];
const schema: RJSFSchema = {
additionalProperties: {
allOf,
},
type: "object",
};

const formData = { newKey: {} };
expect(retrieveSchema(testValidator, schema, {}, formData)).toEqual({
...schema,
properties: {
newKey: {
type: "object",
allOf,
[ADDITIONAL_PROPERTY_FLAG]: true,
},
},
});
});

it("should handle null formData for schema which contains additionalProperties", () => {
const schema: RJSFSchema = {
additionalProperties: {
Expand Down

0 comments on commit c39279c

Please sign in to comment.