Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow schema $refs to not have to start with #/definitions #954

Closed
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export function optionsList(schema) {

function findSchemaDefinition($ref, definitions = {}) {
// Extract and use the referenced definition if we have it.
const match = /^#\/definitions\/(.*)$/.exec($ref);
const match = /^#(?:\/definitions)?\/(.*)$/.exec($ref);
if (match && match[1]) {
const parts = match[1].split("/");
let current = definitions;
Expand Down
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--timeout 5000
--compilers js:babel-register
--require ./test/setup-jsdom.js
16 changes: 16 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,22 @@ describe("utils", () => {
expect(retrieveSchema(schema, definitions)).eql(address);
});

it("should 'resolve' a schema which contains definitions not in `#/definitions`", () => {
const schema = { $ref: "#/components/schemas/address" };
const address = {
type: "object",
properties: {
street_address: { type: "string" },
city: { type: "string" },
state: { type: "string" },
},
required: ["street_address", "city", "state"],
};
const definitions = { components: { schemas: { address } } };

expect(retrieveSchema(schema, definitions)).eql(address);
});

it("should 'resolve' escaped JSON Pointers", () => {
const schema = { $ref: "#/definitions/a~0complex~1name" };
const address = { type: "string" };
Expand Down