Skip to content

Commit

Permalink
fix: solve refs without path
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousme committed Oct 9, 2021
1 parent 9d5823c commit 34f425c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
7 changes: 7 additions & 0 deletions resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ function resolveUri(uri, anchors) {
if (!anchors[prefix]) {
throw err;
}

if (path === undefined){
return anchors[prefix];
}

const paths = path.split("/").slice(1);
try {
const result = paths.reduce(
Expand All @@ -53,6 +58,8 @@ function resolveUri(uri, anchors) {
} catch (_) {
throw err;
}


}

function resolve(tree) {
Expand Down
14 changes: 13 additions & 1 deletion test/test-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const invalidSpec = require(`./validation/invalid-spec.json`);
const yamlFileName = localFile(`./validation/petstore-openapi.v3.yaml`);
const mainSpecYamlFileName = localFile(`./validation/main-spec.v3.yaml`);
const subSpecYamlFileName = localFile(`./validation/sub-spec.v3.yaml`);
const subSpec2YamlFileName = localFile(`./validation/sub-spec2.v3.yaml`);
const subSpecUri = "http://www.example.com/subspec";
const subSpecUri2 = "subspec2";
const inlinedRefs = "x-inlined-refs";

async function testVersion(version) {
Expand Down Expand Up @@ -198,20 +200,30 @@ test(`addSpecRef: Invalid filename returns an error`, (t) => {
});

test(`addSpecRef works`, async (t) => {
t.plan(3);
t.plan(5);
const validator = new Validator();
await validator.addSpecRef(subSpecUri, subSpecYamlFileName);
await validator.addSpecRef(subSpecUri2, subSpec2YamlFileName);
const res = await validator.validate(mainSpecYamlFileName);
t.equal(res.valid, true, "main spec + subspec is valid");
t.equal(
validator.specification[inlinedRefs][subSpecUri].components.requestBodies
.Pet.required,
true,
);
t.equal(
validator.specification[inlinedRefs][subSpecUri2].get.summary,
"Finds Pets by status",
);
const resolvedSpec = validator.resolveRefs();
t.equal(
resolvedSpec.paths["/pet"].post.requestBody.required,
true,
"$refs from main spec to sub spec are correctly resolved",
);
t.equal(
resolvedSpec.paths["/pet/findByStatus"].get.summary,
"Finds Pets by status",
"$refs from main spec to sub2 spec are correctly resolved",
);
});
4 changes: 3 additions & 1 deletion test/validation/main-spec.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ paths:
'405':
description: Invalid input
requestBody:
$ref: 'http://www.example.com/subspec#/components/requestBodies/Pet'
$ref: 'http://www.example.com/subspec#/components/requestBodies/Pet'
/pet/findByStatus:
$ref: 'subspec2'
39 changes: 39 additions & 0 deletions test/validation/sub-spec2.v3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

get:
tags:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: true
style: form
explode: false
schema:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
responses:
'200':
description: successful operation
content:
application/xml:
schema:
type: array
items:
$ref: 'http://www.example.com/subspec#/components/schemas/Pet'
application/json:
schema:
type: array
items:
$ref: 'http://www.example.com/subspec#/components/schemas/Pet'
'400':
description: Invalid status value

0 comments on commit 34f425c

Please sign in to comment.