Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

anyOf #19

Draft
wants to merge 22 commits into
base: merging
Choose a base branch
from
Draft

anyOf #19

wants to merge 22 commits into from

Conversation

patrickkwang
Copy link
Contributor

No description provided.

@codecov-io
Copy link

codecov-io commented Feb 18, 2021

Codecov Report

❗ No coverage uploaded for pull request base (merging@b45a141). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             merging      #19   +/-   ##
==========================================
  Coverage           ?   97.64%           
==========================================
  Files              ?        2           
  Lines              ?       85           
  Branches           ?        0           
==========================================
  Hits               ?       83           
  Misses             ?        2           
  Partials           ?        0           

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 b45a141...2d0c69f. Read the comment docs.

@patrickkwang patrickkwang changed the title any of anyOf Feb 18, 2021
@patrickkwang
Copy link
Contributor Author

This is a simplified treatment. The documentation is not super clear in this area, but I'm pretty sure that you can actually have type and anyOf defined in the same schema. What I don't understand is what that's supposed to mean. However, it doesn't seem like a very common use case, so I'm inclined to handle that later when we're able to generate some test cases.

@patrickkwang patrickkwang linked an issue Feb 18, 2021 that may be closed by this pull request
@alongreyber
Copy link
Contributor

I added another test for a nested anyOf, and it doesn't seem to work:

def test_anyof_nested():
    """Test generating an object from an anyOf schema."""
    schema = {
        "properties": {
            "a": {
                "anyOf": [
                    {
                        "type": "string",
                    },
                    {
                        "type": "integer",
                    },
                ],
            }
        },
    }
    output = generate_json(schema)
    assert isinstance(output["a"], (str, int))

Is this a use case we need to support? It seems like it should be, but I'm not sure how we would go about implementing this.

@patrickkwang
Copy link
Contributor Author

It looks like that fails because the top-level schema ought to have "type": "object" and doesn't. To ensure the test passes, you should probably also add "required": ["a"].

@patrickkwang
Copy link
Contributor Author

After digging a bit more, I think I understand. This:

{
    "type": "object",
    "properties": {
        "a": {},
        "b": {}
    },
    "anyOf": [
        {"required": ["a"]},
        {"required": ["b"]}
    ]
}

is equivalent to:

{
    "anyOf": [
        {
            "type": "object",
            "properties": {
                "a": {},
                "b": {}
            },
            "required": ["a"]
        },
        {    
            "type": "object",
            "properties": {
                "a": {},
                "b": {}
            },
            "required": ["b"]
        }
    ]
}

So I think we ought to do basically that transformation, followed by the simple anyOf procedure I've implemented here. @alongreyber has already taken a swing at schema merging in #20, so perhaps we should finish that up first.

@patrickkwang patrickkwang marked this pull request as draft February 19, 2021 03:23
@alongreyber alongreyber changed the base branch from main to merging February 22, 2021 15:56
@alongreyber
Copy link
Contributor

This isn't exactly how you were planning to do this, but I think it is equivalent. The idea is to sample the list of anyOf schemas provided to pick a sublist (because anyOf can be one or more of the provided schemas). We merge those subschemas together and check if those subschemas were merged without error. If they were merged without error, we merge the combined schemas back into the base. If not, we pick a new sample. There will always be a valid sample because picking a single subschema is always valid. Let me know if you have questions about this process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for anyOf
3 participants