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

Fails with 500 error when returning HTTPStatus and default response is configured #1917

Closed
chbndrhnns opened this issue Apr 30, 2024 · 0 comments · Fixed by #1918
Closed

Comments

@chbndrhnns
Copy link
Contributor

Description

Connexion accepts return statements like return {}, HTTPStatus.CREATED but it fails with 500 error if response validation is enabled and if there is a default response with required fields configured in the schema.

Expected behaviour

Just use HTTPStatus members like integers

Actual behaviour

See above.

Steps to reproduce

from http import HTTPStatus

import pytest
from connexion import AsyncApp


async def fails():
    return {"data": []}, HTTPStatus.CREATED


async def ok():
    return {"data": []}, 201


@pytest.fixture
def app():
    app = AsyncApp(__name__)
    app.add_api(
        validate_responses=True,
        specification={
            "openapi": "3.0.0",
            "info": {"title": "test", "version": "v1"},
            "paths": {
                "/fails": {
                    "get": {
                        "x-openapi-router-controller": __name__,
                        "operationId": "fails",
                        "responses": {
                            "201": {
                                "description": "ok",
                            },
                            "default": {
                                "description": "default",
                                "content": {
                                    "application/json": {
                                        "schema": {
                                            "type": "object",
                                            "properties": {
                                                "error_code": {"type": "integer"},
                                            },
                                            "required": ["error_code"],
                                        },
                                    }
                                },
                            },
                        },
                    }
                },
                "/ok": {
                    "get": {
                        "x-openapi-router-controller": __name__,
                        "operationId": "ok",
                        "responses": {
                            "201": {
                                "description": "ok",
                            }
                        },
                    }
                },
            },
        },
    )
    return app


def test_ok(app):
    with app.test_client() as client:
        res = client.get("/ok")
        assert res.status_code == HTTPStatus.CREATED


def test_fails(app):
    # SHOULD NOT FAIL
    with app.test_client() as client:
        res = client.get("/fails")
        assert res.status_code == HTTPStatus.CREATED

Additional info:

Output of the commands:

  • python --version: 3.9.16
  • pip show connexion | grep "^Version\:": 3.0.6
Ruwann added a commit that referenced this issue May 30, 2024
Fixes #1917  .



Changes proposed in this pull request:

 - Look at enum value for return status code if given
 -
 -

---------

Co-authored-by: Ruwann <ruwan.lambrichts@ml6.eu>
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 a pull request may close this issue.

1 participant