Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

fix: Add OpenAPI 3.1 $ref overrides #2

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options);
result.circular = dereferenced.circular;
result.value = dereferenced.value;
// OpenAPI 3.1 $ref overrides: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#fixed-fields-19
if (obj.description) {
result.value.description = obj.description;
}
if (obj.summary) {
result.value.summary = obj.summary;
}
}
else {
for (const key of Object.keys(obj)) {
Expand All @@ -64,6 +71,13 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
// Avoid pointless mutations; breaks frozen objects to no profit
if (obj[key] !== dereferenced.value) {
obj[key] = dereferenced.value;
// OpenAPI 3.1 $ref overrides: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#fixed-fields-19
if (value.description) {
obj[key].description = value.description;
}
if (value.summary) {
obj[key].summary = value.summary;
}
}
}
else {
Expand Down
26 changes: 26 additions & 0 deletions test/specs/oas31/dereferenced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";

module.exports =
{
required: [
"name"
],
type: "object",
definitions: {
name: {
type: "string",
description: "Someone's name"
}
},
"properties": {
"name": {
"description": "Someone's name",
"type": "string"
},
"secretName": {
"description": "Someone's secret name",
"type": "string"
}
},
title: "Person"
};
39 changes: 39 additions & 0 deletions test/specs/oas31/oas31.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

const { expect } = require("chai");
const $RefParser = require("../../../lib");
const helper = require("../../utils/helper");
const path = require("../../utils/path");
const parsedSchema = require("./parsed");
const dereferencedSchema = require("./dereferenced");

describe("Schema with OpenAPI 3.1 $ref description/schema overrides", () => {
it("should parse successfully", async () => {
let parser = new $RefParser();
const schema = await parser.parse(path.rel("specs/oas31/oas31.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(parsedSchema);
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/oas31/oas31.yaml")]);
});

it("should resolve successfully", helper.testResolve(
path.rel("specs/oas31/oas31.yaml"),
path.abs("specs/oas31/oas31.yaml"), parsedSchema
));

it("should dereference successfully", async () => {
let parser = new $RefParser();
const schema = await parser.dereference(path.rel("specs/oas31/oas31.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(dereferencedSchema);
// The "circular" flag should NOT be set
expect(parser.$refs.circular).to.equal(false);
});

it("should bundle successfully", async () => {
let parser = new $RefParser();
const schema = await parser.bundle(path.rel("specs/oas31/oas31.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(parsedSchema);
});
});
14 changes: 14 additions & 0 deletions test/specs/oas31/oas31.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: Person
type: object
required:
- name
properties:
name:
$ref: "#/definitions/name"
secretName:
$ref: "#/definitions/name"
description: "Someone's secret name"
definitions:
name:
description: "Someone's name"
type: string
25 changes: 25 additions & 0 deletions test/specs/oas31/parsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

module.exports =
{
required: [
"name"
],
type: "object",
definitions: {
name: {
type: "string",
description: "Someone's name"
}
},
"properties": {
"name": {
"$ref": "#/definitions/name"
},
"secretName": {
"$ref": "#/definitions/name",
"description": "Someone's secret name"
}
},
title: "Person"
};