Skip to content

NotImplementedError: anyOf is not supported as bare property #51

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

Closed
hramasub opened this issue Aug 31, 2016 · 12 comments
Closed

NotImplementedError: anyOf is not supported as bare property #51

hramasub opened this issue Aug 31, 2016 · 12 comments
Labels

Comments

@hramasub
Copy link

This code
``
with open('ComputerSystemCollection.json') as schema_file:
root_schema = json.load(schema_file)

builder = pjso.ObjectBuilder (root_schema)
ns = builder.build_classes()
``

to generate binding for the following schema:
http://redfish.dmtf.org/schemas/v1/ComputerSystemCollection.json

results in the following error:

Traceback (most recent call last):
File "./test.py", line 14, in
ns = builder.build_classes()
File "/usr/lib/python2.7/site-packages/python_jsonschema_objects/init.py", line 78, in build_classes
builder.construct(uri, defn)
File "/usr/lib/python2.7/site-packages/python_jsonschema_objects/classbuilder.py", line 418, in construct
ret = self._construct(uri, _args, *_kw)
File "/usr/lib/python2.7/site-packages/python_jsonschema_objects/classbuilder.py", line 426, in _construct
"anyOf is not supported as bare property")
NotImplementedError: anyOf is not supported as bare property

@cwacek
Copy link
Owner

cwacek commented Oct 8, 2016

@hramasub I'm not likely to fix this in the near future. The problem is that anyOf has a particularly terrible meaning for generating class instances: it allows the data to match "one or more of the provided schemas". Since python_jsonschema_objects effectively generates a class wrapper for each schema, it's not clear how to handle the anyOf property, since which class wrapper you get back is going to be non-deterministic.

For most uses where you would use anyOf, you can use oneOf, and oneOf has much improved semantics because it designates a 1-1 link between generated class instance and schema.

anyOf is a useful motif for validation, because you all you care about is whether or not there's a match, but it doesn't make much sense for class/wrapper generation.

@cwacek cwacek closed this as completed Oct 8, 2016
@rmspeers
Copy link
Contributor

rmspeers commented Dec 1, 2023

@cwacek I ran into this today and started looking at how to patch it in and submit a PR to you, before I found this ticket. I agree it's a pain for reliable serialization. Any ideas since the original note here about how it could be supported, at least with a 'dangerous' flag or something that allowed a 1-way load of it (I know, I don't like how that sounds, but trying to think how to support).

@cwacek
Copy link
Owner

cwacek commented Dec 1, 2023

The only thing I can think that would be reasonable is a flag like: any of=“first-match”, which would cause the resolver to generate to the first matching schema in the list.

That would not be the default behavior, and obviously wouldn’t actually match the jsonschema semantics, but would allow you to use the library for schemas with this keyword if you understood what it would do.

@rmspeers
Copy link
Contributor

rmspeers commented Dec 3, 2023

Yes, that makes sense. I tried a hack outside the library to monkey-patch my schema with a similar effect for now - with plenty of warnings given. I think that behavior would be a reasonable one to at least handle the keyword. I'll try to take a look at the lib code more and see if I can figure out where to add that in the architecture when I have a chance.

@cwacek
Copy link
Owner

cwacek commented Dec 3, 2023 via email

@rmspeers
Copy link
Contributor

rmspeers commented Dec 4, 2023

@cwacek I can move this to a new issue if useful, but in working on this I ran into an issue in my test creation, where when I set strict=True, which I did to sanity check if that mattered, it doesn't seem to see the property in the below example:

def test_simple_array_anyOf():
    basicSchemaDefn = {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "title": "Test",
        "properties": {
            "ExampleAnyOf": {"$ref": "#/definitions/exampleAnyOf"}
        },
        "required": ["ExampleAnyOf"],
        "type": "object",
        "definitions": {
            "exampleAnyOf": {
                "oneOf":
                    {"type": "string", "format": "email"},
                    {"type": "string", "maxlength": 0},
                ]
            }
        },
    }

    builder = pjo.ObjectBuilder(basicSchemaDefn)

    ns = builder.build_classes(strict=True)
    ns.Test().from_json('{"ExampleAnyOf" : "[email protected]"}')

@cwacek
Copy link
Owner

cwacek commented Dec 4, 2023 via email

@rmspeers
Copy link
Contributor

rmspeers commented Dec 4, 2023

Created #266. That does not have a test for the strict issue mentioned above, it just is for anyOf simplifying to oneOf with the first anyOf item being used. Feedback/tweaks welcome if you had something else in mind.

@rmspeers
Copy link
Contributor

rmspeers commented Dec 7, 2023

@hramasub and others, this should now be fixed (well, let's say 'work-around-supported') with #266, whenever the next release cuts.

@cwacek
Copy link
Owner

cwacek commented Dec 7, 2023

It was included in release 0.5.1 cut yesterday. @rmspeers @hramasub

@rmspeers
Copy link
Contributor

When I integrated this upstream, I found that although it worked for my simple example, it didn't work when it was nested in a real-world example. My fault. It looks like kw needs to be passed down to calls to construct inside def _build_object(self, nm, clsdata, parents, **kw):.

rmspeers added a commit to rmspeers/python-jsonschema-objects that referenced this issue Feb 29, 2024
@rmspeers
Copy link
Contributor

@cwacek - I had a chance to look at this per my above note and submit a brief PR for your review to see if that should fix this in nested/edge cases. #280

rmspeers added a commit to rmspeers/python-jsonschema-objects that referenced this issue Feb 29, 2024
cwacek added a commit that referenced this issue Mar 1, 2024
#51 handle nested anyof, ensure that data gets passed down via object creators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants