APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
942 stars 226 forks source link

Unable to de-reference nested array in parsed JSON response #343

Closed RagaviMuthukrishnan closed 4 months ago

RagaviMuthukrishnan commented 4 months ago

Hello Team ,

I am developing an automation code which would validate the API response with json structure defined in OpenSpec YAML file

I am trying to use the parser to parse one of our OpenSpec YAML files and decode the response values as proper JSON but the parsed value comes back as "[Object]" instead of the value. It is part of "$ref" attribute which I believe is not getting resolved properly. Sharing the snippet of response which I would to receive as parsed JSON in proper structure

Snippet from YAML

FavouritesEnvelopedResponse:
      type: object
      description: A response body contract for a collection of favourites
      properties:
        favourites:
          type: array
          items:
            $ref: "#/components/schemas/FavouriteResponse"
 FavouriteResponse:
      type: object
      description: A response body contract for a favourite workspace item
      properties:
        id:
          type: string
          description: The ID of a user favourite
          example: "9090"
        user_id:
          type: string
          format: UUID
          description: The ID of a user
          example: "e13f885c-cf07-480f-a17c-8de1a5940ad9"
        workspace_item:
          $ref: "#/components/schemas/WorkspaceItemResponse"

Javascript code using the json-schema-ref-parse

const fs = require("fs");
const path = require("path");
const $RefParser = require("json-schema-ref-parser");
const schemaFilePath = path.join(__dirname, "xxx.yaml");
resolveSchema();
async function resolveSchema() {
  try {
      const dereferencePromise = await $RefParser.dereference(schemaFilePath);
      console.log(dereferencePromise.components.schemas.FavouritesEnvelopedResponse);
  } catch (error) {
    console.error("Error resolving schema:", error);
  }
}

Received Output

{
  type: 'object',
  description: 'A response body contract for a collection of favourites',
  properties: { favourites: { type: 'array', items: [Object] } }
}

Expected Output

Instead of items: [Object] would like to see the actual resolved array Object resolving to appropriate JSON structure

jonluca commented 4 months ago

Is that just your default object output?

Try doing console.log(JSON.stringify(...)) instead

RagaviMuthukrishnan commented 4 months ago

Is that just your default object output?

Try doing console.log(JSON.stringify(...)) instead

Thanks much @jonluca for the response.

I am getting the below error message if i try to wrap it using JSON.stringify()


    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    --- property 'parent_folder_workspace_item' closes the circle
    at JSON.stringify (<anonymous>)
    at resolveSchema (/Users/ragavimuthukrishnan/Apromore-playwright-tests/tests/api/schemas/sample.test.js:15:24)
Error: No tests found.
Make sure that arguments are regular expressions matching test files.
You may need to escape symbols like "$" or "*" and quote the arguments.```

Can you please guide me how to get rid of that in order to extract the actual object contents instead of [Object] ?