whitlockjc / json-refs

Various utilities for JSON Pointers (http://tools.ietf.org/html/rfc6901) and JSON References (http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't be solved reference by root file alone.

nao50 opened this issue · comments

commented

Hi, thank you for the wonderful library.
I have one question.

My environment

node: 12.6.0
json-refs: 3.0.15

Problems

I tried to convert the following root.yaml file to a yaml file with following index.ts that resolves $ref.
However, I can't solve the problem the way I want.

root.yaml

aaa:
  $ref: "#/bbb"
bbb:
  hoge: "huga"

index.ts

import * as YAML from "js-yaml";
import * as fs from "fs";
import * as jsonRefs from "json-refs";

const root = YAML.safeLoad(fs.readFileSync("root.yaml").toString());
const options = {
  filter: ["relative", "remote", "invalid"],
  resolveCirculars: true,
  includeInvalid: true,
  loaderOptions: {
    processContent: (res: any, callback: any) => {
      callback(undefined, YAML.safeLoad(res.text));
    },
  },
};

jsonRefs
  .resolveRefs(root, options)
  .then((results) => {
    console.log(YAML.safeDump(results.resolved, { noRefs: true }));
  });

Expected Output

aaa:
  hoge: huga
bbb:
  hoge: huga

Actual Output

aaa:
  $ref: '#/bbb'
bbb:
  hoge: huga

If I create another file like the following ref.yaml and pass it to the above typescript like fs.readFileSync("ref.yaml"), I got the expected behavior.

ref.yaml

$ref: "root.yaml"

Actual Output

aaa:
  hoge: huga
bbb:
  hoge: huga

If possible, I would like to use only root.yaml to solve the problem.
Is there a shortage or mistake in my JsonRefsOptions specification?

Thanks.

You're using filter wrong, and only asking json-refs to resolve invalid, relative and remote references, but there are none. Maybe filter is improperly named but that is the list of references types to resolve. filter is documented here: https://github.com/whitlockjc/json-refs/blob/master/docs/API.md#module_json-refs.JsonRefsOptions

Closing this as it's not a json-refs bug. Feel free to communicate here and we can reopen if we find a bug.