Skip to content

Handle nullables #268

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

Merged
merged 3 commits into from
Aug 11, 2021
Merged

Handle nullables #268

merged 3 commits into from
Aug 11, 2021

Conversation

rwb27
Copy link
Contributor

@rwb27 rwb27 commented Aug 11, 2021

W3C Thing Description does not permit the type of a DataSchema object to be a list. This is valid JSON Schema, but is not valid Thing Description. This most commonly occurs when fields are declared with allow_none=True.

This PR fixes two things:

  • The updated OpenAPI conversion falls over, because converting nullable fields requires an initialised APISpec object and I was trying to fudge my way around it.
  • The Thing Description is not valid, because it uses a customised marshmallow_jsonschema and doesn't enforce the Thing Description restriction on type.

I've fixed it in two places:

  • A spec object is now passed to labthings.openapi.utilities.ensure_schema to make sure nullable fields are serialised correctly
  • I've modified marshmallow_jsonschema so that it expands list types, as described in labthings.json.marshmallow_jsonschema.base.convert_list_type_to_oneof

This closes #267

rwb27 added 3 commits August 4, 2021 21:03
The thing description is invalid if we have fields with `allow_none`
enabled.  That's because JSONSchema permits `type` to be a list, which
is the sensible way to represent a field that may be `null` or a value.

I've modified our marshmallow_jsonschema library to
expand this into one duplicated sub-schema for each type in the
list, combined using `oneOf`.  This is allowed by the thing description
(and, as far as I know, is valid JSON Schema as well).
Using the `field2property` method of the `FieldConverterMixin` from
`apispec.ext.marshmallow` is not reliable, as some methods (specifically
those dealing with nullable fields) depend on being linked to an
`APISpec` object.

I've changed our code so we now use the initialised converter object
from our current spec - it adds an argument, but makes everything
more robust.
@rwb27
Copy link
Contributor Author

rwb27 commented Aug 11, 2021

@jtc42 I'm going to merge this assuming everything comes up green - it's a minor change and is only likely to be noticed because it fixes some errors. I'd love to find a better solution than embedding marshmallow_jsonschema in our codebase, but that's not a change I've introduced here, and I'm going to leave it as-is for now ;)

@codecov
Copy link

codecov bot commented Aug 11, 2021

Codecov Report

Merging #268 (49ea82d) into master (52695d5) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #268      +/-   ##
==========================================
+ Coverage   91.19%   91.23%   +0.03%     
==========================================
  Files          41       41              
  Lines        1851     1859       +8     
  Branches      292      294       +2     
==========================================
+ Hits         1688     1696       +8     
  Misses        105      105              
  Partials       58       58              
Flag Coverage Δ
unittests 91.23% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/labthings/apispec/plugins.py 92.94% <100.00%> (-0.09%) ⬇️
src/labthings/apispec/utilities.py 96.66% <100.00%> (-0.11%) ⬇️
src/labthings/json/marshmallow_jsonschema/base.py 94.59% <100.00%> (+0.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52695d5...49ea82d. Read the comment docs.

@rwb27 rwb27 merged commit e9ee1b8 into master Aug 11, 2021
@rwb27 rwb27 deleted the handle-nullables branch August 23, 2021 16:43
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.

Invalid thing description for nullable fields
1 participant