Skip to content

Commit

Permalink
Merge pull request #644 from snyk/fix/allow-for-one-to-one-relationsh…
Browse files Browse the repository at this point in the history
…ip-requests

Fix: Allow for one to one relationship requests
  • Loading branch information
jac-moran authored Sep 3, 2024
2 parents 8763aa1 + 95cbcff commit 4530e3c
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,53 @@ describe("resource object rules", () => {
},
},
} as OpenAPIV3.Document;
const ruleRunner = new RuleRunner([resourceObjectRules]);
const ruleInputs = {
...TestHelpers.createRuleInputs(baseJson, afterJson),
context,
};
const results = await ruleRunner.runRulesWithFacts(ruleInputs);
expect(results.length).toBeGreaterThan(0);
expect(results.every((result) => result.passed)).toBe(true);
},
);

test.each(["uuid", "uri", "ulid"])(
"passes when PATCH request body for a relationship is of the correct form (data is a single resource object with id and type)",
async (format) => {
const afterJson = {
...baseJson,
paths: {
"/api/example/relationships/example": {
patch: {
responses: {}, // not tested here
requestBody: {
content: {
"application/vnd.api+json": {
schema: {
type: "object",
properties: {
data: {
type: "object",
properties: {
type: {
type: "string",
},
id: {
type: "string",
format: format,
},
},
},
},
},
},
},
},
},
},
},
} as OpenAPIV3.Document;

const ruleRunner = new RuleRunner([resourceObjectRules]);
const ruleInputs = {
Expand Down Expand Up @@ -227,7 +274,7 @@ describe("resource object rules", () => {
where:
"PATCH /api/example/relationships/example request body: application/vnd.api+json",
name: "request body for relationship post/patch/delete",
error: "Expected a partial match",
error: "Expected at least one partial match",
}),
]),
);
Expand Down Expand Up @@ -335,6 +382,54 @@ describe("resource object rules", () => {
},
);

test.each(["uuid", "uri", "ulid"])(
"passes when POST request body for a relationship is of the correct form (data is a single resource object with type and id)",
async (format) => {
const afterJson = {
...baseJson,
paths: {
"/api/example/relationships/example": {
post: {
responses: {}, // not tested here
requestBody: {
content: {
"application/vnd.api+json": {
schema: {
type: "object",
properties: {
data: {
type: "object",
properties: {
type: {
type: "string",
},
id: {
type: "string",
format: format,
},
},
},
},
},
},
},
},
},
},
},
} as OpenAPIV3.Document;

const ruleRunner = new RuleRunner([resourceObjectRules]);
const ruleInputs = {
...TestHelpers.createRuleInputs(baseJson, afterJson),
context,
};
const results = await ruleRunner.runRulesWithFacts(ruleInputs);
expect(results.length).toBeGreaterThan(0);
expect(results.every((result) => result.passed)).toBe(true);
},
);

test("fails when POST request body for a relationship is of incorrect form (missing id from resource objects in data array)", async () => {
const afterJson = {
...baseJson,
Expand Down Expand Up @@ -383,7 +478,7 @@ describe("resource object rules", () => {
where:
"POST /api/example/relationships/example request body: application/vnd.api+json",
name: "request body for relationship post/patch/delete",
error: "Expected a partial match",
error: "Expected at least one partial match",
}),
]),
);
Expand Down Expand Up @@ -1168,6 +1263,54 @@ describe("resource object rules", () => {
},
);

test.each(["uuid", "uri", "ulid"])(
"passes when DELETE request body for a relationship is of the correct form (data is a resource object)",
async (format) => {
const afterJson = {
...baseJson,
paths: {
"/api/example/relationships/example": {
delete: {
responses: {}, // not tested here
requestBody: {
content: {
"application/vnd.api+json": {
schema: {
type: "object",
properties: {
data: {
type: "object",
properties: {
type: {
type: "string",
},
id: {
type: "string",
format: format,
},
},
},
},
},
},
},
},
},
},
},
} as OpenAPIV3.Document;

const ruleRunner = new RuleRunner([resourceObjectRules]);
const ruleInputs = {
...TestHelpers.createRuleInputs(baseJson, afterJson),
context,
};
const results = await ruleRunner.runRulesWithFacts(ruleInputs);
expect(results.length).toBeGreaterThan(0);
expect(results.every((result) => result.passed)).toBe(true);
},
);

test("fails when DELETE request body for a relationship is of incorrect form (resource objects in collection missing id)", async () => {
const afterJson = {
...baseJson,
Expand Down Expand Up @@ -1216,7 +1359,7 @@ describe("resource object rules", () => {
where:
"DELETE /api/example/relationships/example request body: application/vnd.api+json",
name: "request body for relationship post/patch/delete",
error: "Expected a partial match",
error: "Expected at least one partial match",
}),
]),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ const requestDataForPost = new RequestRule({
},
});

// Relationship POST, PATCH, and DELETE requests must have
// Relationship POST, PATCH, and DELETE requests can have
// a request body with resource objects for the relationships
// to be added/patched/deleted.
const matchRelationshipModificationRequestData = {
const matchRelationshipModificationRequestArrayData = {
data: {
type: "array",
items: {
Expand All @@ -142,6 +142,24 @@ const matchRelationshipModificationRequestData = {
},
};

// Relationship POST, PATCH, and DELETE requests can have
// a request body with a resource object for the single relationship
// to be added (set)/patched/deleted.
const matchRelationshipModificationRequestSingleData = {
data: {
type: "object",
properties: {
type: {
type: Matchers.string,
},
id: {
type: "string",
format: resourceIDFormat,
},
},
},
};

const requestDataForRelationshipModification = new RequestRule({
name: "request body for relationship post/patch/delete",
docsLink: links.jsonApi.postRequests,
Expand All @@ -150,18 +168,34 @@ const requestDataForRelationshipModification = new RequestRule({
request.contentType === "application/vnd.api+json" &&
["patch", "delete", "post"].includes(rulesContext.operation.method),
rule: (requestAssertions) => {
requestAssertions.body.added.matches({
schema: {
type: "object",
properties: matchRelationshipModificationRequestData,
requestAssertions.body.added.matchesOneOf([
{
schema: {
type: "object",
properties: matchRelationshipModificationRequestArrayData,
},
},
});
requestAssertions.body.changed.matches({
schema: {
type: "object",
properties: matchRelationshipModificationRequestData,
{
schema: {
type: "object",
properties: matchRelationshipModificationRequestSingleData,
},
},
});
]);
requestAssertions.body.changed.matchesOneOf([
{
schema: {
type: "object",
properties: matchRelationshipModificationRequestArrayData,
},
},
{
schema: {
type: "object",
properties: matchRelationshipModificationRequestSingleData,
},
},
]);
},
});

Expand Down

0 comments on commit 4530e3c

Please sign in to comment.