Skip to content

Commit

Permalink
fix: not merge the allOf.contains schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
AlimovSV authored and nickgros committed Sep 27, 2024
1 parent d3af307 commit f92f9f6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,24 @@ export function retrieveSchemaInternal<
return [...(allOf as S[]), restOfSchema as S];
}
try {
const withContainsSchemas = [] as S[];
const withoutContainsSchemas = [] as S[];
resolvedSchema.allOf?.forEach((s) => {
if (typeof s === 'object' && s.contains) {
withContainsSchemas.push(s as S);
} else {
withoutContainsSchemas.push(s as S);
}
});
if (withContainsSchemas.length) {
resolvedSchema = { ...resolvedSchema, allOf: withoutContainsSchemas };
}
resolvedSchema = mergeAllOf(resolvedSchema, {
deep: false,
} as Options) as S;
if (withContainsSchemas.length) {
resolvedSchema.allOf = withContainsSchemas;
}
} catch (e) {
console.warn('could not merge subschemas in allOf:\n', e);
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
Expand Down
75 changes: 75 additions & 0 deletions packages/utils/test/schema/retrieveSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,81 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
type: 'string',
});
});
it('should not merge `allOf.contains` schemas', () => {
// https://github.com/rjsf-team/react-jsonschema-form/issues/2923#issuecomment-1946034240
const schema: RJSFSchema = {
type: 'array',
items: {
type: 'object',
properties: {
a: {
type: 'string',
},
},
},
allOf: [
{
maxItems: 5,
},
{
contains: {
type: 'object',
properties: {
a: {
pattern: '1',
},
},
},
},
{
contains: {
type: 'object',
properties: {
a: {
pattern: '2',
},
},
},
},
],
};
const rootSchema: RJSFSchema = { definitions: {} };
const formData = {};
expect(retrieveSchema(testValidator, schema, rootSchema, formData)).toEqual({
type: 'array',
items: {
type: 'object',
properties: {
a: {
type: 'string',
},
},
},
maxItems: 5,
allOf: [
{
contains: {
type: 'object',
properties: {
a: {
pattern: '1',
},
},
},
},
{
contains: {
type: 'object',
properties: {
a: {
pattern: '2',
},
},
},
},
],
});
});
it('should not merge incompatible types', () => {
const schema: RJSFSchema = {
allOf: [{ type: 'string' }, { type: 'boolean' }],
Expand Down

0 comments on commit f92f9f6

Please sign in to comment.