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

Fix stale request object when an explicit serializer type is passed #1103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Server, Model, JSONAPISerializer, Response } from "miragejs";

describe("Integration | Server | Regressions | 885 Serializer stale request bug test", function () {
let server;

beforeEach(function () {
server = new Server({
environment: "test",
models: {
eel: Model.extend(),
},
serializers: {
clawlessEel: JSONAPISerializer,
},
routes() {
this.get("/tigers/:id", new Response());
this.get("/eels/:id", function (schema, request) {
let eel = schema.eels.find(request.params.id);
return this.serialize(eel, "clawlessEel");
});
},
});
});

afterEach(function () {
server.shutdown();
});

test("it works", async () => {
let eel = server.create("eel");

expect.assertions(1);

// Make sure there is a request with an `include` query param that refers to a relationship that
// doesn't exist on the `eel` resource, so that `JSONAPISerializer` would throw if the request
// inside Mirage ends up staying the same when fetching `/eels/:id`.
await fetch(`/tigers/1?include=claws`);

let res = await fetch(`/eels/${eel.id}`);

expect(res.status).toEqual(200);
});
});
10 changes: 9 additions & 1 deletion lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,17 @@ class Serializer {
@param request
@return { Object } the json response
*/
serialize(primaryResource /* , request */) {
serialize(primaryResource, request) {
this.primaryResource = primaryResource;

// The if statement only guards agains not passing the request object when overriding the
// method, to avoid breaking changes.
//
// See: https://github.com/miragejs/miragejs/pull/1103
if (request) {
this.request = request;
}

return this.buildPayload(primaryResource);
}

Expand Down