seriousme / fastify-openapi-glue

A plugin for the Fastify webserver to autogenerate a Fastify configuration based on a OpenApi(v2/v3) specification.
MIT License
196 stars 33 forks source link

References getting stripped from response #572

Closed alexborisov closed 2 months ago

alexborisov commented 3 months ago

Hello, I'm trying to debug a problem and identify the source. I have a Schema component "Category". This is an object with a property parent which is also a Category. My definition looks like so:

components:
  schemas:
    Category:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        slug:
          type: string
        parent:
          $ref: "#/components/schemas/Category"

I have two endpoints - getCategoryById which returns a category object and getAllCategories which returns an object with an array of category objects {results: [{...}]}

The first one (getCategoryById) returns an object with a correctly nested parent. Everything is resolved and all the data is there. The second case (getAllCategories) returns an empty object for the parent of every category.

If i console log the raw db data nested parents are all there. If i change the definition of parent in my Category schema from:

 parent:
        $ref: "#/components/schemas/Category"

to

 parent:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        slug:
          type: string

The response to getAllCategories comes back with the parent object present and populated.

At first i thought it might be some sort of recursion protection, but in that case i should be getting an empty object for both use cases... so this makes me think it might be a bug given how this only applies to arrays of objects.

Please help! 🥺

seriousme commented 3 months ago

Hi,

thanks for asking. Removing items from responses typically happens if fastify sees a response schema that is more strict than the response that is being provided.

Recursive schema should work, see: https://github.com/seriousme/fastify-openapi-glue/blob/master/test/test-recursive.v3.js

Could you make a minimal example based on this test, but then with your object structure to see if that works ?

If that doesn't work then we can debug that to fix the issue, and add it to the test set to avoid regressions.

Kind regards, Hans

github-actions[bot] commented 2 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days'