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

Resolve an array of schema objects in parameters #209

Closed
cvlong opened this issue May 11, 2018 · 2 comments

Comments

@cvlong
Copy link
Contributor

commented May 11, 2018

I see that the marshmallow plugin currently resolves an array of schema items in the response, but is there existing functionality to also do this in the request?

I'd like to be able to define my request parameters a docstring like this:

spec.definition('Schema', schema=Schema)

def example():
    """Example view that accepts an array objects 
    ---
    post:
        parameters:
            - in: body
              schema:
                type: array
                items: Schema
    """

So that ['parameters'][0]['schema'] will produce more explicit results like {'type': 'array', 'items': {'$ref': '#/definitions/Schema'}} rather than {'type': 'array', 'items': 'Schema'}.

Does this functionality already exist? If not, I am happy to submit a PR to address this. Thank you!

@sloria

This comment has been minimized.

Copy link
Member

commented May 14, 2018

Good catch @cvlong . I wrote a quick-n-dirty test to check this...

    def test_resolves_array_of_schema_in_parameters(self, spec):
        spec.definition('Pet', schema=PetSchema)

        def pet_view():
            """Not much to see here.

            ---
            post:
              parameters:
              - in: body
                schema:
                  type: array
                  items: Schema
            """
            return '...'

        spec.add_path(path='/pet', view=pet_view)
        p = spec._paths['/pet']
        post = p['post']
        assert 'parameters' in post
        assert post['parameters']
        assert len(post['parameters']) == 1
        assert post['parameters'][0]['schema']['type'] == 'array'
        assert post['parameters'][0]['schema']['items'] == {'$ref': '#/definitions/Schema'}

And it indeed fails.

|| =========================================================================================== test session starts ============================================================================================
|| platform darwin -- Python 3.6.4, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 -- /Users/sloria/.local/share/virtualenvs/apispec/bin/python3.6
|| rootdir: /Users/sloria, inifile: pytest.ini
|| collected 1 item                                                                                                                                                                                           
|| 
|| tests/test_ext_marshmallow.py::TestOperationHelper::test_resolves_array_of_schema_in_parameters FAILED
|| 
|| ================================================================================================= FAILURES =================================================================================================
|| _____________________________________________________________________ TestOperationHelper.test_resolves_array_of_schema_in_parameters ______________________________________________________________________
|| tests/test_ext_marshmallow.py:407: in test_resolves_array_of_schema_in_parameters
||     assert post['parameters'][0]['schema']['items'] == {'$ref': '#/definitions/Schema'}
|| E   AssertionError: assert 'Schema' == {'$ref': '#/definitions/Schema'}
|| ========================================================================================= 1 failed in 0.14 seconds =========================================================================================

I would definitely review and merge a PR for this.

@sloria sloria added the help wanted label May 14, 2018

@cvlong

This comment has been minimized.

Copy link
Contributor Author

commented May 14, 2018

Thanks @sloria. Here's a first stab at the PR: #211 let me know what you think about generalizing the function to handle arrays defined in parameters or responses.

@sloria sloria removed the help wanted label May 14, 2018

@sloria sloria closed this May 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.