thim81 / openapi-format

Format an OpenAPI document by ordering, formatting and filtering fields.
MIT License
77 stars 14 forks source link

Keep $ref dereference when writing the update files #71

Open thim81 opened 1 year ago

thim81 commented 1 year ago

Currently, openapi-format dereferences the OpenAPI documents to do its formatting, sorting, ... and write a dereferenced result.

Investigate the option to leverage: https://www.npmjs.com/package/json-refs to format the OpenAPI documents and update the $ref values in different files.

const jsonRefs = require('json-refs');

const json = {
  "foo": {
    "$ref": "#/bar"
  },
  "bar": {
    "baz": "qux"
  }
};

const options = {
  filter: ['relative', 'remote'],
  loaderOptions: {
    processContent: (res, callback) => callback(undefined, res.text)
  }
};

jsonRefs.resolveRefs(json, options).then(
  (results) => {
    // make changes to the resolved object
    results.resolved.bar.baz = "newValue";
    // write the changes to the $ref locations
    jsonRefs.updateRefs(results.refs, results.resolved).then(
      (updated) => console.log(updated),
      (err) => console.log(err)
    );
  },
  (err) => console.log(err)
);
rngtng commented 1 year ago

yes this would be great. in addition we make heavily use of $ref to other files what the best solution to tackle those? format one file after the other?

thim81 commented 1 year ago

You could try to do them one by one, although it is not ideal and I m not sure it it would work fully.

Another approach could be to use a good diff tool and diff the final, formatted result with the various files. Also not ideal.

Ideally, would be to have openapi-format support referenced files. I need to find time to play with the json-ref library. If you or anybody else are interested, I always welcome PRs.

rngtng commented 1 year ago

Cool thanks a lot. I‘ll explore myself options too..