stoplightio / json

Useful functions when working with JSON.
Apache License 2.0
22 stars 6 forks source link

fix: documents that include # as a ref #120

Closed EdVinyard closed 1 year ago

EdVinyard commented 1 year ago

based on api-design outage, starting with PCRecruiter-supplied example OpenAPI document

"$ref": "#" is our kryptonite.

see https://stoplight-internal.slack.com/archives/CUBL8DMRP/p1675872847844529

P0lip commented 1 year ago

I'll give it a look. As a quick workaround, we could just use decycle in the api-design to fully mitigate the risk.

P0lip commented 1 year ago

I believe what we'd want is the following:

const input = {
  openapi: '3.0.0',
  paths: {},
  components: {
    schemas: {
      User: {
        $ref: '#',
      },
    },
  },
};

const output = bundleTarget({ document: input, path: '#/components', bundleRoot: '#/$defs' });

expect(output).toStrictEqual({
  schemas: {
    User: {
      $ref: '#/$defs/root',
    },
  },
  $defs: {
    root: {
      openapi: '3.0.0',
      paths: {},
      components: {
        $ref: '#'
      },
    },
  },
});

this would be valid for both OAS ("valid" in the sense that semantics would be unchanged, but obviously it still wouldn't make any sense because you cannot really reference an oas document like that) & JSON Schemas

EdVinyard commented 1 year ago

superseded by https://github.com/stoplightio/json/pull/121