Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes #3692 - simple discriminator now bypasses isValid() calls in getMatchingOption and getClosestMatchingOption for improved performance #3845

Conversation

michal-kurz
Copy link
Contributor

@michal-kurz michal-kurz commented Aug 29, 2023

Reasons for making this change

fixes #3692

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests. I've run npm run test:update to update snapshots, if needed.
    • I've updated docs if needed
    • I've updated the changelog with a description of the PR
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

…() calls in getMatchingOption and getClosestMatchingOption.ts for improved performance
Comment on lines +301 to +303
Compares the value of `discriminatorField` within `formData` against the value of `discriminatorField` within schema for each `option`. Returns index of first `option` whose discriminator matches formData. Returns `undefined` if there is no match.

This function does not work with discriminators of `"type": "object"` and `"type": "array"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite struggled trying to make this description as understandable and straght-forwards as possible.

Comment on lines +39 to +58
test('const discriminator', () => {
expect(
getOptionMatchingSimpleDiscriminator(
{ foo: 'foo' },
[{}, { type: 'object', properties: { foo: { const: 'foo' } } }],
'foo'
)
).toEqual(1);
});

test('enum discriminator', () => {
expect(
getOptionMatchingSimpleDiscriminator(
{ foo: 'foo' },
[{}, { type: 'object', properties: { foo: { enum: ['bar', 'foo'] } } }],
'foo'
)
).toEqual(1);
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps there are some other relevant cases that I'm missing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need to figure out the test coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@michal-kurz michal-kurz changed the title fix: fixes - simple discriminator now bypasses isValid() calls in getMatchingOption and getClosestMatchingOption.ts for improved performance fix: fixes - simple discriminator now bypasses isValid() calls in getMatchingOption and getClosestMatchingOption for improved performance Aug 29, 2023
@michal-kurz michal-kurz changed the title fix: fixes - simple discriminator now bypasses isValid() calls in getMatchingOption and getClosestMatchingOption for improved performance fix: fixes #3692 - simple discriminator now bypasses isValid() calls in getMatchingOption and getClosestMatchingOption for improved performance Aug 29, 2023
import { PROPERTIES_KEY } from './constants';
import { RJSFSchema, StrictRJSFSchema } from './types';

export default function getOptionMatchingSimpleDiscriminator<T = any, S extends StrictRJSFSchema = RJSFSchema>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the same documentation you put into the docs above here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@heath-freenome
Copy link
Member

@michal-kurz It looks like your changes causes line 51 in getMatchingOption not to be covered. I'm guessing the short-circuit no longer covers the other discriminator case

@heath-freenome heath-freenome merged commit 208b148 into rjsf-team:main Oct 5, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terrible performance with big anyOf field
2 participants