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

Schema generation fails inside JsonContent #1638

Open
stevesutton-hds opened this issue Aug 5, 2024 · 2 comments
Open

Schema generation fails inside JsonContent #1638

stevesutton-hds opened this issue Aug 5, 2024 · 2 comments

Comments

@stevesutton-hds
Copy link

stevesutton-hds commented Aug 5, 2024

(Edit: This is now a feature request, not a failure - see comment chain)

Schema generation fails, when inside a JsonContent element.

Steps to reproduce:

With composer.json containing "zircote/swagger-php": "^4.10", (4.10.6 in composer.lock)

With code

class myclass {
/**
 * @OA\Info(
 *   title="API",
 *   version="1.1",
 *   description="API documentation",
 * )
 *
 */

    /**
     * @OA\Post(
     *   path="/path",
     *   @OA\Response(
     *     response="200",
     *     description="response",
     *     @OA\JsonContent(
     *         @OA\Schema(
     *           title="title",
     *           @OA\Property(
     *             property = "success",
     *             type = "boolean",
     *             example = true
     *           ),
     *         ),
     *     )
     *   )
     * )
     *

     */
    protected function post(array $extraParams, array $queries) {
      return;
    }
}

Calling:

$oa = \OpenApi\Generator::scan(["src/"]);

echo $oa->toJson();

Results in output:

{
    "openapi": "3.0.0",
    "paths": {
        "/path": {
            "post": {
                "operationId": "dd9f210a8294662e079e1c11b03617d8",
                "responses": {
                    "200": {
                        "description": "response",
                        "content": {
                            "application/json": {
                                "schema": {}
                            }
                        }
                    }
                }
            }
        }
    }
}

Notice that schema value is empty "{}"

Expected ouput::

{
    "openapi": "3.0.0",
    "paths": {
        "/path": {
            "post": {
                "operationId": "dd9f210a8294662e079e1c11b03617d8",
                "responses": {
                    "200": {
                        "description": "response",
                        "content": {
                            "application/json": {
                                "schema": {
                                            "title": "title",
                                            "properties": {
                                                "success": {
                                                    "type": "boolean",
                                                    "example": true
                                                }
                                            },
                                            "type": "object"
                                        }
                            }
                        }
                    }
                }
            }
        }
    }}

Note that if inside the OA\JsonContent I enclose the schema inside a oneOf = {} and add a second dummy schema, I then see the correct schema output in the json.

Also note that the behaviour is the same if the schema is included as a reference, rather than inline.

@DerManoMann
Copy link
Collaborator

Almost got it. JsonContent is a shorthand version of a Schema nested inside a MediaType. It extends Schema, so there is no need to nest another Schema.

Removing the inner Schema should be all you need to change.

@stevesutton-hds
Copy link
Author

stevesutton-hds commented Aug 5, 2024

Aha, thanks! That's really not obvious :D

I guess it would still be very useful if the generator could detect and warn about this instead of just generating an empty schema without explanation - I wasted a few hours today and never did solve it myself - so I'll leave this open as a feature request, but yes, removing the inner Schema did result in the expected output.

edit: even generating:

"schema" : {
  "schema": {}
}

Might have help me get to that conclusion myself.

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

No branches or pull requests

2 participants