zircote/swagger-php

MediaType issue with JsonContent

Closed this issue · 14 comments

   $content = new OpenApi\Attributes\JsonContent();
   $content->ref = "#/components/schemas/{$singular}";
   $response->content = [$content];

I keep getting this error:
Property "mediaType" doesn't exist in a @OA\JsonContent(), existing properties: "x", "attachables", "_context", "_unmerged", "ref", "schema", "title", "description", "maxProperties", "minProperties", "required", "properties", "type", "format", "items", "collectionFormat", "default", "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxLength", "minLength", "pattern", "maxItems", "minItems", "uniqueItems", "enum", "multipleOf", "discriminator", "readOnly", "writeOnly", "xml", "externalDocs", "example", "examples", "nullable", "deprecated", "allOf", "anyOf", "oneOf", "not", "additionalProperties", "additionalItems", "contains", "patternProperties", "dependencies", "propertyNames", "const" in

 But im defining a schema in the ref as instructed. 
 
 Trying to append it into a response:
 
 ```
                                     $response = new OpenApi\Attributes\Response(
                                    response: 200,
                                    description: "{$modelName} found",
                                    // content: new OpenApi\Attributes\JsonContent(ref: "#/components/schemas/{$modelName}")
                                );
                                ```
                                
                                And I can't have it where content: is because it returns
                                
                                > Unexpected @OA\JsonContent() in

Starting to stress me out now.

Note that im not in attributes or annotation commenting as im building something dynamic.

I need to know how to assign my schema as a reference to JsonContent then into the Response. Using Analysis

OA commented

yeah thanks for tagging me lmao

Why would you have your name as that haha

                                    $json = new OpenApi\Attributes\JsonContent(ref: "#/components/schemas/{$singular}");

                                    $response = new OpenApi\Attributes\Response(
                                        response: 200,
                                        description: "{$modelName} found",
                                        // content: new OpenApi\Attributes\JsonContent(ref: "#/components/schemas/{$singular}")
                                    );
                                    $response->content = $json;
                                    ```
                                    
                                    Even something like this, returns "Attempt to read property "mediaType" on string". And im so f.... confused. 

So trying to break it down step by step;

This is the attribute approach in the controller:
// #[OA\Response(response: 200, description: 'Thing updated', content: new JsonContent(ref: '#/components/schemas/Thing'))]

So, I need a JsonContent Object, then a Response object, and attach a ref to the JsonCOntent and a content into the Response.

                                    $jsonContent = new OpenApi\Attributes\JsonContent(
                                        // Define the schema if needed, currently commented
                                        ref: "#/components/schemas/{$singular}",
                                    );

                                    // Create Response object
                                    $response = new OpenApi\Attributes\Response(
                                        response: 200,
                                        description: "{$modelName} found",
                                        content: $jsonContent
                                    );

What am I missing here?:

Unexpected @OA\JsonContent() in

Slowly debugging, so with the approach im using, I dont think it's going through the merge json part?

As items in the _unmerged is spitting the error

After I create the response, Im doing $operation->responses = [$responses]; but that doesn't appear to be passing the JsonContent into the MergeJsonContent class.

Not sure what I should be doing instead?

@DerManoMann Don't suppose you'd have any insight into what im missing in this process?

Does look reasonable to me. Only thing I can think of right now (busy times) is that JsonContent is just a shortcut for MediaType + Schema although I cannot see why that would matter here.
(I might have more time later this week)

Yeah its been stressing me out loads. Ill park it up for now and come back to it when you have some spare time to help assist. Thank you :)

Just at a quick test, it looks like adding the schema to the MediaType, then supplying MediaType as the content in the response works. It looks like Shortcuts doesn't work in my approach then

So yeah, break through after a full day of stress yesterday.

It wont allow JsonContent so I had to define the schema directly in the MediaType, looking like this

$response = new OpenApi\Attributes\Response(
    response: 200,
    description: "{$modelName} found",
    content: new OpenApi\Attributes\MediaType(
          schema: new OpenApi\Attributes\Schema(
                 ref: "#/components/schemas/{$singular}"
          ),
          mediaType: 'application/json'
   ),
);

Seems this is resolved, so closing...