Forum Discussion

Chloe1990's avatar
Chloe1990
New Contributor
31 days ago

How to retrieve the content in $ref

My team composed the api spec documentation in a layered structure to prevent the long page. 

For example, in global.yaml, we have

openapi: "3.0.3"

info:

tags:

components:

paths:

    /api/v1/abc:

        $ref: "./controller/abc.yaml#/GetAbc"

in abc.yam, we have

GetAbc:

     get:

    parameters: 

        -name: "searchParam"

         in : "query"

         schema:

        example:

            $ref: "../xyz.yaml#/Example1"

and the real example of the request is stored in xyz.yaml/Example1

 

We want to parse from the root node to prevent some paths are not consistent in the yaml files. 

The solution we have currently is the follwing

  1. use File to parse the yaml file,
  2. Use JsonNode to retrieve the first layer node:
  3. JsonNode node = rootSpecNode
    .at("/paths")
    .get("/api/v1/abc)
    .get("$ref");
  4. String secondLayerPath = rootPath +......+ node.toString().replace(...)

     

    And loop the above steps for the inner $ref layers. In this way, if I have n layers, I need to parse n times and will have at least n*4 lines of code. 

Does openApi provide an easier way to parse the api spec yaml file especially for $ref rather than using the above way?

 

No RepliesBe the first to reply