{
    "componentChunkName": "component---src-templates-blog-post-tsx",
    "path": "/blog/2020-08-31-mirage-serializers/",
    "result": {"data":{"blogPost":{"title":"MirageJS: Choosing the right Serializer ","slug":"/blog/2020-08-31-mirage-serializers/","authorNodes":[{"name":"Min Kim","slug":"/people/min-kim/"}],"markdown":{"html":"<p>MirageJS is one of the pioneers in enabling frontend developers to use mocks systematically for their tests and development. It was originally built for Ember.js in 2015, but due to its popularity the maintainers created a standalone version compatible with any javascript framework.</p>\n<p>Mirage introduces itself as a mocking library, but at Frontside we don't think that's a good enough term to refer to its extensive power. If your application uses a RESTful API (an <a href=\"https://github.com/miragejs/graphql\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">adapter for GraphQL</a> is on the way), Mirage is a great choice to simulate your backend with high-fidelity outputs. We've used Mirage in several projects to enable frontend developers to keep advancing the UI while the backend is not yet ready. Its flexibility also makes it a perfect test companion compatible with many tools out there, including Cypress.</p>\n<p>A key element of Mirage that enables it to generate realistic responses lies on its serializer layer. The serializer takes the data stored in Mirage's seeded database and outputs it in the format that you configure for it. For an effective <a href='/blog/2020-07-29-decoupling-teams-through-simulation/' target='_blank' rel='nofollow noopener'>simulation strategy</a>, you want that output format to match exactly your backend API specifications.</p>\n<p>In this article, we'll explore the four serializers available on Mirage by default. Choosing which one is the right one for you depends on your API formats. There might be a serializer that works with your backend right out of the box or you may have to customize it from a base. After outlining how the serializer layer works in Mirage, we'll dive into the specifics of each one.</p>\n<nav class=\"table-of-contents\">\n  <h2>Already familiar with serializers? Jump to:</h2>\n  <ul>\n      <li>\n          <a href='#json-api-serializer'>JSON API Serializer</a>\n      </li>\n      <li>\n          <a href='#active-model-serializer'>Active Model Serializer</a>\n      </li>\n      <li>\n          <a href='#rest-serializer'>Rest Serializer</a>\n      </li>\n      <li>\n          <a href='#serializer'>Generic Serializer</a>\n      </li>\n      <li>\n          <a href=\"#cheat-sheet\">Cheat sheet</a>\n      </li>\n  </ul>\n</nav>\n<h2 id=\"how-mirage-serializers-work\" style=\"position:relative;\"><a href=\"#how-mirage-serializers-work\" aria-label=\"how mirage serializers work permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How Mirage Serializers Work</h2>\n<p>Understanding how Mirage’s various serializers work begins with reviewing how data flows thought Mirage. At the most basic level, Mirage allows you to create mocks in a traditional way, returning hand-crafted objects:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/blog-posts\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">title</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Beyond mocking\"</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> <span class=\"token string\">\"2\"</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">title</span><span class=\"token operator\">:</span> <span class=\"token string\">\"A lesson about Interactors\"</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">{</span> <span class=\"token literal-property property\">id</span><span class=\"token operator\">:</span> <span class=\"token string\">\"3\"</span><span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">title</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Github Actions: all about pull_request\"</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>However, hand-crafted anything is not a scalable solution, and these sorts of objects become difficult to maintain when your data relies on relationships.</p>\n<p>Instead, we recommend taking advantage of Mirage modeling and <a href=\"https://miragejs.com/docs/main-concepts/orm/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ORM</a> capabilities so you can shape your backend in Mirage and seed it with generated data. As the snipped code below illustrates, you can use the seeded models of the database directly from your route handle:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/users\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">schema<span class=\"token punctuation\">,</span> request</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> schema<span class=\"token punctuation\">.</span>users<span class=\"token punctuation\">.</span><span class=\"token function\">all</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>In this case you don't have to manually deal with IDs, formats, or the concrete user records. Instead, you use a schema query that will bring records (based on models and factories) from Mirage's database and output the format that your backend uses.</p>\n<p>In order to do that, you have to set up models, factories, and serializers in Mirage. Models deal with the relationship between records, factories generate sample data for the records' attributes, and serializers format how the records will be mapped in a JSON response. The following diagram illustrates how these pieces come together to enable you to query records in your Mirage route handles:</p>\n<p><figure class=\"figure\"><img src=\"/img/2020-08-21-mirage-serializers-setup.png\"><figcaption class=\"figure-caption\">Diagram: Set up relationships → configure data generation → specify JSON response format through serializers → Query schema on route handle</figcaption></figure></p>\n<p>The role of the serializer is to map the data to a JSON structure that matches your backend needs. The structure of the JSON refers to how the object keys are named and what's nested inside which keys.</p>\n<p>Given that the serializer's main purpose is to make it easy for you to have Mirage outputting realistic data for your frontend, we recommend discussing with your backend team how they plan to format the responses from their endpoints. Then you can compare those responses to what the different Mirage serializers offer and choose the closest one, customizing it if necessary.</p>\n<h2 id=\"model-and-factories-setup\" style=\"position:relative;\"><a href=\"#model-and-factories-setup\" aria-label=\"model and factories setup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Model and Factories Setup</h2>\n<p>The job of a serializer is to provide a format that represents the attributes and relationships of the records stored in Mirage’s database. To illustrate how the different serialiser options work, in this section we’ll describe a hypothetical set up. It’s worth noting at the outset that in Mirage models only describe relationships, not attributes; that means attributes can be defined arbitrarily when the model is instantiated in a factory.</p>\n<p>In the code below we’ll use three models to describe the relationships among our data:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token literal-property property\">models</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">author</span><span class=\"token operator\">:</span> Model<span class=\"token punctuation\">.</span><span class=\"token function\">extend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token literal-property property\">blogPosts</span><span class=\"token operator\">:</span> <span class=\"token function\">hasMany</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n\n  <span class=\"token literal-property property\">blogPost</span><span class=\"token operator\">:</span> Model<span class=\"token punctuation\">.</span><span class=\"token function\">extend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> \n\t<span class=\"token literal-property property\">author</span><span class=\"token operator\">:</span> <span class=\"token function\">belongsTo</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n\t<span class=\"token literal-property property\">post_blogComments</span><span class=\"token operator\">:</span> <span class=\"token function\">hasMany</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n\n  <span class=\"token literal-property property\">post_blogComment</span><span class=\"token operator\">:</span> Model<span class=\"token punctuation\">.</span><span class=\"token function\">extend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token literal-property property\">blogPost</span><span class=\"token operator\">:</span> <span class=\"token function\">belongsTo</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Notice the inconsistencies in case and separators in the names for the model’s fields. We've used arbitrary casing so the effect of the serializers described below is more apparent.</p>\n<p>Now for the factories, we’ll set them as described below. As we mentioned earlier, it’s in this part where the attributes for our records are declared:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token literal-property property\">factories</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">author</span><span class=\"token operator\">:</span> Factory<span class=\"token punctuation\">.</span><span class=\"token function\">extend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">author_emailAddress</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> faker<span class=\"token punctuation\">.</span>internet<span class=\"token punctuation\">.</span><span class=\"token function\">email</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">blogPost</span><span class=\"token operator\">:</span> Factory<span class=\"token punctuation\">.</span><span class=\"token function\">extend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">blogPost_title</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> faker<span class=\"token punctuation\">.</span>random<span class=\"token punctuation\">.</span><span class=\"token function\">words</span><span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n <span class=\"token literal-property property\">post_blogComment</span><span class=\"token operator\">:</span> Factory<span class=\"token punctuation\">.</span><span class=\"token function\">extend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">comment_paragraph</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> faker<span class=\"token punctuation\">.</span>random<span class=\"token punctuation\">.</span><span class=\"token function\">words</span><span class=\"token punctuation\">(</span><span class=\"token number\">6</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span>\n</code></pre></div>\n<p>Again, to highlight the effects of serializers on the attribute names, notice that in the code above the casing for each attribute is mixed. You can see we're using a library called <code class=\"language-text\">faker</code> to generate random data to populate the models attributes.</p>\n<p>To make the outputs comparable across serializers, the example responses that we provide are the result of fetching a request for all <code class=\"language-text\">blogPosts</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">schema<span class=\"token punctuation\">.</span>blogPosts<span class=\"token punctuation\">.</span><span class=\"token function\">all</span><span class=\"token punctuation\">(</span> <span class=\"token punctuation\">)</span></code></pre></div>\n<h2 id=\"data-attributes-on-all-serializers\" style=\"position:relative;\"><a href=\"#data-attributes-on-all-serializers\" aria-label=\"data attributes on all serializers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Data attributes on all serializers</h2>\n<p>Before getting into the specific serializers, let’s address first a peculiarity about attributes that all Mirage serializers share. As it turns out, Mirage doesn’t serialize all the attributes of a response evenly.</p>\n<p>When we fetch all the blog posts with <code class=\"language-text\">schema.blogPosts.all( )</code> we'll get all the records for blogs posts. If we configure the serializer to <code class=\"language-text\">include</code> relationships, we can expect the authors and comments from such blog posts too. The following diagram illustrates the relationships between blog posts, authors, and comments.</p>\n<p><figure class=\"figure\"><img src=\"/img/2020-08-21-mirage-relationships.png\"><figcaption class=\"figure-caption\">Diagram: Blog Post has a belongsTo relationship with Author and a hasMany relationship with comments</figcaption></figure></p>\n<p>Because we’re using <code class=\"language-text\">schema.blogPosts.all( )</code> to get these blog posts, we’ll say that blog posts are the <em class=\"blog-post-highlight blog-post-highlight__pink\">queried records</em>. As mentioned, included in that response we’d have the author of the blog post (through <em class=\"blog-post-highlight blog-post-highlight__navy\">belongsTo</em>). Additionally, we’ll get the blog post comments (through <em class=\"blog-post-highlight blog-post-highlight__sky\">hasMany</em>).</p>\n<p><strong>Serialization on <em class=\"blog-post-highlight blog-post-highlight__pink\">queried records</em>: attribute names are transformed</strong></p>\n<p>Serializers will only transform the attribute names of the queried records. The transformations applied vary from serializer to serializer; you’ll find the specifics in the sections below.</p>\n<p><strong>Records from <em class=\"blog-post-highlight blog-post-highlight__navy\">belongsTo</em>: attribute names are not transformed</strong></p>\n<p>Counterintuitively, the serializers in Mirage will not apply any transform to the attribute names of the records coming from a <code class=\"language-text\">belongsTo</code> relationship. This means that the inconsistent attribute name we included in our authors factory will remain as it was declared, <code class=\"language-text\">author_emailAddress</code> no matter which serializer you use.</p>\n<p><strong>Records from <em class=\"blog-post-highlight blog-post-highlight__sky\">hasMany</em>: attributes are not included</strong></p>\n<p>Perhaps to avoid loading large amounts of data, Mirage does not include the attributes of any record that has been loaded due to a hasMany relationship. This means that although we’ll get a list of the comments associated to the blog post we queried, we will not be able to get any of their attributes.</p>\n<h2 id=\"json-api-serializer\" style=\"position:relative;\"><a href=\"#json-api-serializer\" aria-label=\"json api serializer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JSON API Serializer</h2>\n<p>This serializer follows the <a href=\"https://jsonapi.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">JSON API</a> specification. A fetch for <code class=\"language-text\">blogPosts</code> with this serializer will return the following response format:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"data\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"type\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"blog-posts\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"attributes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token property\">\"blog-post-title\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"A blog post\"</span>\n      <span class=\"token punctuation\">}</span>\n      <span class=\"token property\">\"relationships\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>...<span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    ...\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"included\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>...<span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"field-names-format-dashes\" style=\"position:relative;\"><a href=\"#field-names-format-dashes\" aria-label=\"field names format dashes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Field names format: dashes</h3>\n<p>In compliance with JSON API conventions, all field names are changed to use dashes. As you can see in the example, the names that were inconsistently cased are all dasherized now; for instance, <code class=\"language-text\">blogPost_title</code> becomes <code class=\"language-text\">blog-post-title</code>.</p>\n<h3 id=\"response-structure-fixed-top-level-elements\" style=\"position:relative;\"><a href=\"#response-structure-fixed-top-level-elements\" aria-label=\"response structure fixed top level elements permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Response structure: fixed top level elements</h3>\n<p>There are two top level keys for the JSON API response: <code class=\"language-text\">data</code> and <code class=\"language-text\">included</code>.</p>\n<p>The main part of the response is <code class=\"language-text\">data</code>, an array of entries that answer to the query issued. In this case, it's providing all the blog posts available.</p>\n<p>The other key, <code class=\"language-text\">included</code> will have the record information from the references related to the entries in <code class=\"language-text\">data</code>. By default, <code class=\"language-text\">included</code> is empty on Mirage.</p>\n<h3 id=\"relationships-reference-objects\" style=\"position:relative;\"><a href=\"#relationships-reference-objects\" aria-label=\"relationships reference objects permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Relationships: reference objects</h3>\n<p>Each record on <code class=\"language-text\">data</code> can have a <code class=\"language-text\">relationships</code> key. In our example, the blog post <code class=\"language-text\">belongsTo</code> an author and <code class=\"language-text\">hasMany</code> blog post comments. By default, the relationships are not included in Mirage by the JSON API serializer.</p>\n<p>To include the relationships, we can <a href=\"https://miragejs.com/api/classes/serializer/#include\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">include</code> option</a> in the JSON API serializer. In that case, we'll get a <code class=\"language-text\">relationships</code> key inside each record from <code class=\"language-text\">data</code> with references to what those authors or comments are.</p>\n<p>Below you can see how a single record looks like when the relationship references are included:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"type\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"blog-posts\"</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"attributes\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"blog-post-title\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"A blog post\"</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"relationships\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"author\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"type\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"authors\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token property\">\"post-blog-comments\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"type\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"post-blog-comments\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Notice that in JSON API Serializer, types are all dasherized and pluralized, and that they are used in every record. For instance, a single author has a type <code class=\"language-text\">authors</code>.</p>\n<p>Additionally, all the records referenced in all the <code class=\"language-text\">relationships</code> key will be loaded in the <code class=\"language-text\">included</code> key of the response.</p>\n<p>Alternatively, we can <a href=\"https://miragejs.com/api/classes/jsonapi-serializer/#always-include-linkage-data\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">set the <code class=\"language-text\">alwaysIncludeLinkageData</code> option</a> to true on the JSON API serializer, with which we'd get the relationship information on the data records, but those related records will not be included in the <code class=\"language-text\">include</code> array.</p>\n<h3 id=\"more-details\" style=\"position:relative;\"><a href=\"#more-details\" aria-label=\"more details permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>More details</h3>\n<ul>\n<li><a href=\"https://miragejs.com/api/classes/jsonapi-serializer/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">JSON API Serializer documentation</a></li>\n<li><a href=\"https://github.com/miragejs/miragejs/blob/master/lib/serializers/json-api-serializer.js\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">JSON API Serializer</code> Source code</a></li>\n</ul>\n<h2 id=\"active-model-serializer\" style=\"position:relative;\"><a href=\"#active-model-serializer\" aria-label=\"active model serializer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Active Model Serializer</h2>\n<p><a href=\"https://guides.rubyonrails.org/active_model_basics.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Active Model</a> is the standard format for Rails APIs responses. Chances are if you don't already know whether or not you need to use this serializer, you most likely don't need this one. But let's still have a look at how this one handles its response. We'll be evoking the same fetch as the one we used for the JSON API Serializer. This is how the response with the Active Model Serializer would look:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"blog_posts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"0\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"blog_post_title\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"A blog post\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"author_id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"...\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"post_blog_comments_ids\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>...<span class=\"token punctuation\">]</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"field-names-format-under_scored\" style=\"position:relative;\"><a href=\"#field-names-format-under_scored\" aria-label=\"field names format under_scored permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Field names format: under_scored</h3>\n<p>The Active Model Serializer changes the caps to be under_scored no matter how the original model name was configured. As you can see in the example, the attribute names that were inconsistently cased are all under_scored now; for instance, <code class=\"language-text\">blogPost_title</code> becomes <code class=\"language-text\">blog_post_title</code>.</p>\n<h3 id=\"response-structure-top-level-types\" style=\"position:relative;\"><a href=\"#response-structure-top-level-types\" aria-label=\"response structure top level types permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Response structure: top level types</h3>\n<p>The Active Model Serializer presents the entity types on the top level arrays, where the type name is pluralized. Unlike the JSON API Serializer the data is not nested under <code class=\"language-text\">data</code> or <code class=\"language-text\">included</code> objects. You can think of the response having lists of records grouped by type on the top level. In this example, <code class=\"language-text\">blog_posts</code> is the type of the queried objects and it's at the top level.\nRelationships: <code class=\"language-text\">_id</code> suffixed reference (or embedded)</p>\n<p>By default, relationships are not resolved for this serializer. However, we can <a href=\"https://miragejs.com/api/classes/serializer/#include\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">include</code> option</a> for the Active Model Serializer and then we'll receive them as references but in a different format than JSON API.</p>\n<p>If <code class=\"language-text\">include</code> is configured, the records of the related fields in a relationship are also loaded at the top level of the JSON with their type names. In the target object, the reference to that related object is only an <code class=\"language-text\">id</code>.</p>\n<p>The field name of the relationship will be appended <code class=\"language-text\">_id</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span> \n  <span class=\"token property\">\"blog_posts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"author_id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"authors\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Mika\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Additionally, you can decide to <a href=\"https://miragejs.com/api/classes/serializer/#embed\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">embed</code> option</a> to nest the relationship object instead of having a reference:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span> \n  <span class=\"token property\">\"blog_posts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"author\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span><span class=\"token punctuation\">,</span>\n        <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Mika\"</span>\n      <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"more-details-1\" style=\"position:relative;\"><a href=\"#more-details-1\" aria-label=\"more details 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>More details</h3>\n<ul>\n<li><a href=\"https://github.com/miragejs/miragejs/blob/master/lib/serializers/active-model-serializer.js\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">Active Model Serializer</code> Source code</a></li>\n</ul>\n<h2 id=\"rest-serializer\" style=\"position:relative;\"><a href=\"#rest-serializer\" aria-label=\"rest serializer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Rest Serializer</h2>\n<p>The Rest Serializer is almost identical to the Active Model Serializer except that it transforms names to camelCase instead of under_score. Let's look at the example output for Rest Serializer:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"blogPosts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"0\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"blogPostTitle\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"A blog post\"</span>\n      <span class=\"token property\">\"author\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"...\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"postBlogComments\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>...<span class=\"token punctuation\">]</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"field-names-format-camelcase\" style=\"position:relative;\"><a href=\"#field-names-format-camelcase\" aria-label=\"field names format camelcase permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Field names format: camelCase</h3>\n<p>The Rest Serializer changes the caps to use camelCase no matter how the original attribute name was configured. As you can see in the example, the names that were inconsistently cased are all camelCased now; for instance, <code class=\"language-text\">blogPost_title</code> becomes <code class=\"language-text\">blogPostTitle</code>.</p>\n<h3 id=\"response-structure-top-level-types-1\" style=\"position:relative;\"><a href=\"#response-structure-top-level-types-1\" aria-label=\"response structure top level types 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Response structure: top level types</h3>\n<p>The Rest Serializer presents the entity types on the top level, in the same way Active Model Serializer does: the top level keys are pluralized camelCase types.\nRelationships: no-suffix id reference (or embedded)</p>\n<p>By default, relationships are not resolved for this serializer. However, we can <a href=\"https://miragejs.com/api/classes/serializer/#include\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">include</code> option</a> for the serializer and then we'll receive them as references.</p>\n<p>If <code class=\"language-text\">include</code> is configured, the records of the related fields in a relationship are also loaded at the top level of the JSON with their type names. In the target object, the reference to that related object is only an <code class=\"language-text\">id</code>. The field name of the relationship will not be suffixed with anything:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span> \n  <span class=\"token property\">\"blogPosts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"author\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"authors\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Mika\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Additionally, you can decide to <a href=\"https://miragejs.com/api/classes/serializer/#embed\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">embed</code> option</a> to nest the relationship object instead of having a reference:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span> \n  <span class=\"token property\">\"blogPosts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"author\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span><span class=\"token punctuation\">,</span>\n        <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Mika\"</span>\n      <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"more-details-2\" style=\"position:relative;\"><a href=\"#more-details-2\" aria-label=\"more details 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>More details</h3>\n<ul>\n<li><a href=\"https://github.com/miragejs/miragejs/blob/master/lib/serializers/rest-serializer.js\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">Rest Serializer</code> Source code</a></li>\n</ul>\n<h2 id=\"serializer\" style=\"position:relative;\"><a href=\"#serializer\" aria-label=\"serializer permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Serializer</h2>\n<p>Last but not least is the barebone <code class=\"language-text\">Serializer</code> which MirageJS describes as basic. Its behavior is quite similar to Rest Serializer, with the only difference lying in how the relationship references are suffixed.</p>\n<p>Let's check out the output for Serializer:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"blogPosts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"blogPostTitle\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"A blog post\"</span>\n      <span class=\"token property\">\"authorId\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"...\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"postBlogCommentsIds\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>...<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"field-names-format-camelcase-1\" style=\"position:relative;\"><a href=\"#field-names-format-camelcase-1\" aria-label=\"field names format camelcase 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Field names format: camelCase</h3>\n<p>This serializer changes the caps to use camelCase, no matter how the original attribute name was configured. As you can see in the example, the names that were inconsistently cased are all camelCased now; for instance, <code class=\"language-text\">blogPost_title</code> becomes <code class=\"language-text\">blogPostTitle</code>.</p>\n<h3 id=\"response-structure-top-level-types-2\" style=\"position:relative;\"><a href=\"#response-structure-top-level-types-2\" aria-label=\"response structure top level types 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Response structure: top level types</h3>\n<p>The Serializer presents the entity types on the top level, in the same way Active Model Serializer does: the top level keys are pluralized camelCase types.\nRelationships: <code class=\"language-text\">Id</code> suffix reference (or embedded)</p>\n<p>By default, relationships are not resolved for this serializer. However, we can <a href=\"https://miragejs.com/api/classes/serializer/#include\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">include</code> option</a> for this serializer and then we'll receive them as references.</p>\n<p>If <code class=\"language-text\">include</code> is configured, the records of the related fields in a relationship are also loaded at the top level of the JSON response with their type names. In the target object, the reference to that related object is only an <code class=\"language-text\">id</code>.</p>\n<p>The field name of the relationship will be appended <code class=\"language-text\">Id</code>:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span> \n  <span class=\"token property\">\"blogPosts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"authorId\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"authors\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Mika\"</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Additionally, you can decide to <a href=\"https://miragejs.com/api/classes/serializer/#embed\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">configure the <code class=\"language-text\">embed</code> option</a> to nest the relationship object instead of having a reference:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span> \n  <span class=\"token property\">\"blogPosts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"1\"</span><span class=\"token punctuation\">,</span>\n      <span class=\"token property\">\"author\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"5\"</span><span class=\"token punctuation\">,</span>\n        <span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Mika\"</span>\n      <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"more-details-3\" style=\"position:relative;\"><a href=\"#more-details-3\" aria-label=\"more details 3 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>More details</h3>\n<ul>\n<li><a href=\"https://github.com/miragejs/miragejs/blob/master/lib/serializer.js\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">Serializer</code> Source code</a></li>\n</ul>\n<h2 id=\"cheat-sheet\" style=\"position:relative;\"><a href=\"#cheat-sheet\" aria-label=\"cheat sheet permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Cheat sheet</h2>\n<table class=\"blog-cheat-sheet\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>JSON API Serializer</th>\n      <th>Active Model Serializer</th>\n      <th>Rest Serializer</th>\n      <th>Serializer</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td class=\"row-heading\">Response structure</td>\n      <td>\n        Top level <code class=\"language-text\">data</code> and <code class=\"language-text\">included</code> keys.<br/>\n        Every type-name is used in plural in each record.\n      </td>\n      <td>Top level keys by pluralized type_name.</td>\n      <td>Top level keys by pluralized typeName</td>\n      <td>Top level keys by pluralized typeName</td>\n    </tr>\n    <tr>\n      <td class=\"row-heading\">Casing transforms</td>\n      <td>dasherized</td>\n      <td>under_score</td>\n      <td>camelCase</td>\n      <td>camelCase</td>\n    </tr>\n    <tr>\n      <td class=\"row-heading\">Relationships included in response?</td>\n      <td>No, unless <code class=\"language-text\">include</code> is configured.   Option for <code class=\"language-text\">alwaysIncludeLInkageData</code></td>\n      <td>No, unless <code class=\"language-text\">include</code> is configured</td>\n      <td>No, unless <code class=\"language-text\">include</code> is configured</td>\n      <td>No, unless <code class=\"language-text\">include</code> is configured</td>\n    </tr>\n    <tr>\n      <td class=\"row-heading\">Relationship reference in record</td>\n      <td>\n        <code class=\"language-text\">relationships</code> key with reference object. <br/>  \n        ex.<pre class=\"language-text\"><code class=\"language-text\">\"relationships\": {\n  \"author\": {\n    \"type\": \"authors\",\n    \"id\": \"5\"\n  }\n}</code></pre>\n      </td>\n      <td>Name of attribute suffixed with <code class=\"language-text\">_id</code> <br>  ex: <code class=\"language-text\">\"author_id\": \"5\"</code></td>\n      <td>Name of attribute with no suffic  <br>  ex:  <code class=\"language-text\">\"author\": \"5\"</code></td>\n      <td>Name of attribute suffixed with <code class=\"language-text\">Id</code>  <br>  ex:  <code class=\"language-text\">\"authorId\": \"5\"</code></td>\n    </tr>\n  </tbody>\n</table>\n<aside class=\"posts-list-list\">\n  <h2>Related article:</h2>\n  <div class=\"posts-list-entry\">\n    <h3 class=\"posts-list-title\">\n      <a href=\"/blog/2020-07-29-decoupling-teams-through-simulation/\">\n        Beyond Mocking: Decoupling teams through Simulation\n      </a>\n    </h3>\n    <p>\n      Decoupling front-end and mobile teams from the back-end makes organizations more effective. In this article we explain why using simulation is an ideal strategy for this purpose, and present important considerations for adopting such practices.\n    </p>\n    <a href=\"/blog/2020-07-29-decoupling-teams-through-simulation/\" class=\"post-link\">\n      Continue reading\n      <span class=\"post-link--arrow\">→</span>\n    </a>\n  </div>\n</aside>","frontmatter":{"date":"August 31, 2020","description":"MirageJS ships with four out-of-the-box serializers. In this blog post, we overview the basics of serializers in Mirage, and examine in detail the specific format of each JSON API, Active Model, Rest and generic serializers.","tags":["simulation","mirage","dx"],"img":{"childImageSharp":{"fixed":{"src":"/static/d4e278e50eadb083bc7d0048fa835d4d/31987/2020-07-29-mirage-social.png"}}}}}}},"pageContext":{"id":"011e2f56-f71f-571d-9ccb-702f8d2add32"}},
    "staticQueryHashes": ["1241260443"]}