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 schema utils handling of nested arrays #5685

Merged
merged 6 commits into from
Jul 28, 2022
Merged

Conversation

cognifloyd
Copy link
Member

@cognifloyd cognifloyd commented Jul 22, 2022

Fixed schema utils to more reliably handle schemas that define nested arrays (object-array-object-array-string) as discovered in some of the ansible installer RBAC tests. This includes a test that reproduced the error so we don't hit this again.

Closes #5684

@cognifloyd cognifloyd added this to the 3.8.0 milestone Jul 22, 2022
@cognifloyd cognifloyd self-assigned this Jul 22, 2022
@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review. and removed size/L PR that changes 100-499 lines. Requires some effort to review. labels Jul 22, 2022
@cognifloyd cognifloyd force-pushed the schema-defaults branch 3 times, most recently from 27bf94e to 6e9d0d6 Compare July 23, 2022 04:12
@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review. labels Jul 23, 2022
@cognifloyd cognifloyd force-pushed the schema-defaults branch 6 times, most recently from 3c652b5 to 0f49782 Compare July 23, 2022 17:55
@cognifloyd cognifloyd force-pushed the schema-defaults branch 2 times, most recently from 3ef0c78 to 09486a7 Compare July 24, 2022 03:32
@pull-request-size pull-request-size bot added size/M PR that changes 30-99 lines. Good size to review. and removed size/L PR that changes 100-499 lines. Requires some effort to review. labels Jul 24, 2022
@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/M PR that changes 30-99 lines. Good size to review. labels Jul 24, 2022
@cognifloyd cognifloyd changed the title fix schema defaults fix schema utils handling of nested arrays Jul 24, 2022
@cognifloyd cognifloyd changed the title fix schema utils handling of nested arrays Fix schema utils handling of nested arrays Jul 24, 2022
@cognifloyd cognifloyd marked this pull request as ready for review July 24, 2022 06:02
@cognifloyd cognifloyd requested review from amanda11 and a team July 24, 2022 06:02
@cognifloyd
Copy link
Member Author

cognifloyd commented Jul 24, 2022

I added a reproducer test for #5684 in 3413a82 which you can see failing in CI here:
https://github.com/StackStorm/st2/runs/7485827132?check_suite_focus=true#step:15:1223

======================================================================
2) ERROR: test_nested_schemas (tests.unit.test_json_schema.JSONSchemaTestCase)
----------------------------------------------------------------------
   Traceback (most recent call last):
st2common/tests/unit/test_json_schema.py line 450 in test_nested_schemas
      instance=instance, schema=TEST_SCHEMA_6, cls=validator, use_default=True
st2common/st2common/util/schema/__init__.py line 370 in validate
      instance = assign_default_values(instance=instance, schema=schema)
st2common/st2common/util/schema/__init__.py line 256 in assign_default_values
      instance=array_instance, schema=schema_items
st2common/st2common/util/schema/__init__.py line 249 in assign_default_values
      array_instance = instance.get(property_name, None)
AttributeError: 'list' object has no attribute 'get'

Then I refactored assign_default_values to separate object vs array handling in 9000680 which passes all the tests including the reproducer. With the array and object stuff smashed together, I was having a hard time understanding which pieces were supposed to handle which types. So, splitting that into separate type-specific functions clarified that. It should be much easier to follow the assign_default_values logic now.

I also left a few TODOs where I noticed some schema features like additionalProperties that this code does not handle.

edit: well, that makes this bug fix sound more straightforward than it was. See #5684 (comment) for more on the other things I tried before coming up with this.

Copy link
Contributor

@amanda11 amanda11 left a comment

Choose a reason for hiding this comment

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

LGTM, and great to have the test case so we don't suffer problem again.

@cognifloyd cognifloyd requested a review from a team July 25, 2022 14:03
Copy link
Contributor

@nzlosh nzlosh left a comment

Choose a reason for hiding this comment

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

LGTM

@cognifloyd cognifloyd merged commit c0b1a68 into master Jul 28, 2022
@cognifloyd cognifloyd deleted the schema-defaults branch July 28, 2022 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API bug fix output schema RBAC refactor size/L PR that changes 100-499 lines. Requires some effort to review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RBAC on unstable fails to load
3 participants