Skip to content

Commit

Permalink
fix: Add support for Open API discriminator on anyOf/oneOf
Browse files Browse the repository at this point in the history
Fixes rjsf-team#3512 by updating the `getFirstMatchingOption()` and `getClosestMatchingOption()` to take an optional discriminator property to use for drilling into one-of Objects to simplify selection detection
Fixes rjsf-team#2044 Fixing the `toIdSchema()` generation to use the `getSchemaType()` function rather than just using `schema.type`

- In `@rjsf/utils`, updated the `SchemaUtilsType`, its associated class implementation and several functions to fix both bug as follows:
  - Updated `SchemaUtilsType` to add an optional `discriminatorField?: string` parameter to the `get(First|Closest|)MatchingOption()` functions
  - Updated `createSchemaUtils()` to take the new `discriminatorField` parameter and forward it onto the underlying function `get(First|Closest|)MatchingOption() implementations
  - Updated the `getClosestMatchingOption()` and `getFirstMatchingOption()` to pass the new `discriminatorField` to the `getMatchingOption()` function
  - Updated `getMatchingOption()` to use `discriminatorField` when it is present in the `options` object properties to drill into the object to detect if that one field is valid
  - Also updated the `toIdSchemaInternal()` function to use `getSchemaType(schema) === 'object'` rather than `schema.type === 'object'` to get the proper pathing for ids
- In `@rjsf/core`, updated the `MultiSchemaField`, to get the `discriminator.propertyName` value for a `anyOf/oneOf` object and pass it into `getClosestMatchingOption()`
  - Updated the `oneOf` and `anyOf` tests to verify the `discriminator` property works
  - Also updated the `oneOf` and `form` tests to update the few ids that were fixed by the `toIdSchema()` fix
  - Moved a test that was once just in the `bootstrap-4` theme related to `additionalProperties` being defined as true for a non-object type schema
- In the other themes, updated the snapshots to include the new test that was moved from `bootstrap-4` into the `objectTests`
  - Updated `bootstrap-4` to delete the `AdditionalProperties.test.tsx` and its associated snapshot since it was moved to `core`
- Updated the `utility-functions` documentation to add the new `discriminatorField` prop to the `get(First|Closest|)MatchingOption()` functions
- Updated the `CHANGELOG.md` file accordingly
  • Loading branch information
heath-freenome committed Apr 11, 2023
1 parent d8034a7 commit ff61332
Show file tree
Hide file tree
Showing 27 changed files with 15,982 additions and 5,492 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/core

- Switched `Form` to use the new `validatorDataMerge()` function instead of the new deprecated `schemaUtils.mergeValidatorData()`
- Updated `MultiSchemaField` to handle the OpenAPI `discriminator` extension on `anyOf/oneOf` fields by passing it into `getClosestMatchingOption()` if it exists, fixing [#3512](https://github.com/rjsf-team/react-jsonschema-form/issues/3512)

## @rjsf/utils

- Refactored the `createErrorHandler()`, `toErrorList()`, `toErrorSchema()` and `unwrapErrorHandler()` functions from the `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8` implementations since they were identical
- As a result, the `mergeValidationData()` function was deprecated in favor of the new `validationDataMerge()` function that uses the refactored `toErrorList()` function
- Refactored the `ROOT_SCHEMA_PREFIX` constant as well
- Updated `ValidatorType` and `SchemaUtilsType` to deprecate the `toErrorList()` and `mergeValidationData()` functions, respectively
- Updated the `getClosestMatchingOption()` and `getFirstMatchingOption()` to pass the new `discriminatorField` to the `getMatchingOption()` function
- Updated `getMatchingOption()` to use `discriminatorField` when it is present in the `options` object properties to drill into the object to detect if that one field is valid
- Updated `SchemaUtilsType` and the associated forward functions in `createSchemaUtils` to add the new `discriminatorField?: string` optional parameter
- Updated `toIdSchema()` function to use `getSchemaType(schema) === 'object'` rather than `schema.type === 'object'` to get the proper pathing for ids, fixing [#2044](https://github.com/rjsf-team/react-jsonschema-form/issues/2044)

## @rjsf/validator-ajv6

Expand Down
Loading

0 comments on commit ff61332

Please sign in to comment.