Skip to content

Commit fe9b06a

Browse files
committed
fix: not merge the allOf.contains schemas
1 parent 9d1ff6d commit fe9b06a

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

packages/utils/src/schema/retrieveSchema.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,24 @@ export function retrieveSchemaInternal<
412412
return [...(allOf as S[]), restOfSchema as S];
413413
}
414414
try {
415+
const withContainsSchemas = [] as S[];
416+
const withoutContainsSchemas = [] as S[];
417+
resolvedSchema.allOf?.forEach((s) => {
418+
if (typeof s === 'object' && s.contains) {
419+
withContainsSchemas.push(s as S);
420+
} else {
421+
withoutContainsSchemas.push(s as S);
422+
}
423+
});
424+
if (withContainsSchemas.length) {
425+
resolvedSchema = { ...resolvedSchema, allOf: withoutContainsSchemas };
426+
}
415427
resolvedSchema = mergeAllOf(resolvedSchema, {
416428
deep: false,
417429
} as Options) as S;
430+
if (withContainsSchemas.length) {
431+
resolvedSchema.allOf = withContainsSchemas;
432+
}
418433
} catch (e) {
419434
console.warn('could not merge subschemas in allOf:\n', e);
420435
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;

packages/utils/test/schema/retrieveSchemaTest.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,81 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
759759
type: 'string',
760760
});
761761
});
762+
it('should not merge `allOf.contains` schemas', () => {
763+
// https://github.com/rjsf-team/react-jsonschema-form/issues/2923#issuecomment-1946034240
764+
const schema: RJSFSchema = {
765+
type: 'array',
766+
items: {
767+
type: 'object',
768+
properties: {
769+
a: {
770+
type: 'string',
771+
},
772+
},
773+
},
774+
allOf: [
775+
{
776+
maxItems: 5,
777+
},
778+
{
779+
contains: {
780+
type: 'object',
781+
properties: {
782+
a: {
783+
pattern: '1',
784+
},
785+
},
786+
},
787+
},
788+
{
789+
contains: {
790+
type: 'object',
791+
properties: {
792+
a: {
793+
pattern: '2',
794+
},
795+
},
796+
},
797+
},
798+
],
799+
};
800+
const rootSchema: RJSFSchema = { definitions: {} };
801+
const formData = {};
802+
expect(retrieveSchema(testValidator, schema, rootSchema, formData)).toEqual({
803+
type: 'array',
804+
items: {
805+
type: 'object',
806+
properties: {
807+
a: {
808+
type: 'string',
809+
},
810+
},
811+
},
812+
maxItems: 5,
813+
allOf: [
814+
{
815+
contains: {
816+
type: 'object',
817+
properties: {
818+
a: {
819+
pattern: '1',
820+
},
821+
},
822+
},
823+
},
824+
{
825+
contains: {
826+
type: 'object',
827+
properties: {
828+
a: {
829+
pattern: '2',
830+
},
831+
},
832+
},
833+
},
834+
],
835+
});
836+
});
762837
it('should not merge incompatible types', () => {
763838
const schema: RJSFSchema = {
764839
allOf: [{ type: 'string' }, { type: 'boolean' }],

0 commit comments

Comments
 (0)