[
  {
    "path": ".github/dependabot.yml",
    "content": "version: 2\nupdates:\n  - package-ecosystem: \"github-actions\"\n    directory: \"/\"\n    schedule:\n      interval: \"weekly\"\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "name: CI\n\non: [push, pull_request]\n\njobs:\n  tests:\n    runs-on: ubuntu-latest\n    strategy:\n      fail-fast: false\n      matrix:\n        ruby: [2.7, '3.0', 3.1, 3.2, 3.3, 3.4, head, truffleruby-head]\n\n    steps:\n    - uses: actions/checkout@v3\n\n    - name: Sets up the Ruby version\n      uses: ruby/setup-ruby@v1\n      with:\n        ruby-version: ${{ matrix.ruby }}\n\n    - name: Sets up the environment\n      run: |\n        sudo apt-get install libsqlite3-dev\n\n    - name: Install legacy bundler for Ruby 2.7\n      if: ${{ matrix.ruby == 2.7 }}\n      run: |\n        gem install -q bundler -v 2.4.22\n\n    - name: Install bundler 2.7+ for modern Rubies\n      if: ${{ matrix.ruby != 2.7 }}\n      run: |\n        gem install -q bundler\n\n    - name: Run bundle install\n      run: |\n        bundle install\n\n    - name: Runs code QA and tests\n      run: bundle exec rake\n\n    - name: Publish to Rubygems\n      continue-on-error: true\n      if: ${{ github.ref == 'refs/heads/master' }}\n      run: |\n        mkdir -p $HOME/.gem\n        touch $HOME/.gem/credentials\n        chmod 0600 $HOME/.gem/credentials\n        printf -- \"---\\n:rubygems_api_key: Bearer ${GEM_HOST_API_KEY}\\n\" > $HOME/.gem/credentials\n        gem build *.gemspec\n        gem push *.gem\n      env:\n        GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}\n"
  },
  {
    "path": ".gitignore",
    "content": "# rcov generated\ncoverage\ncoverage.data\n\n# rdoc generated\nrdoc\n\n# yard generated\ndoc\n.yardoc\n\n# bundler\n.bundle\n.byebug_history\n\n# For MacOS:\n.DS_Store\n\n# For MacOS:\n.DS_Store\n\n# For TextMate\n#*.tmproj\n#tmtags\n\n*.swp\n\n# For redcar:\n#.redcar\n\n# For rubinius:\n#*.rbc\n\n# For the gem\ntest.db\n\n# For those using rbenv\n.ruby-version\n\n# For those who install gems locally to a vendor dir\n/vendor\n\n# Don't checkin Gemfile.lock\n# See: https://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/\nGemfile.lock\n\n# Gem builds\n/*.gem\n"
  },
  {
    "path": ".rspec",
    "content": "--color\n"
  },
  {
    "path": ".rubocop.yml",
    "content": "plugins:\n  - rubocop-performance\n  - rubocop-rspec\n\nAllCops:\n  NewCops: enable\n  SuggestExtensions: false\n\nStyle/FrozenStringLiteralComment:\n  Enabled: false\n\nStyle/SymbolArray:\n  Enabled: false\n\nStyle/WordArray:\n  Enabled: false\n\nStyle/SymbolProc:\n  Exclude:\n    - 'spec/fixtures/*.rb'\n\nLint/DuplicateMethods:\n  Exclude:\n    - 'spec/fixtures/*.rb'\n\nRSpec/SpecFilePathFormat:\n  Enabled: false\n\nRSpec/SpecFilePathSuffix:\n  Enabled: false\n\nRSpec/DescribedClass:\n  Enabled: false\n\nRSpec/ExampleLength:\n  Enabled: false\n\nRSpec/MultipleExpectations:\n  Enabled: false\n\nRSpec/NestedGroups:\n  Enabled: false\n\nPerformance/TimesMap:\n  Exclude:\n    - 'spec/**/**.rb'\n\nGemspec/RequiredRubyVersion:\n  Enabled: false\n\n# TODO: Fix these...\nStyle/Documentation:\n  Enabled: false\n\nStyle/GuardClause:\n  Exclude:\n    - 'lib/**/**.rb'\n\nStyle/ConditionalAssignment:\n  Exclude:\n    - 'lib/**/**.rb'\n\nStyle/IfUnlessModifier:\n  Exclude:\n    - 'lib/**/**.rb'\n\nLint/AssignmentInCondition:\n  Exclude:\n    - 'lib/**/**.rb'\n\nMetrics:\n  Exclude:\n    - 'lib/**/**.rb'\n\nMetrics/BlockLength:\n  Enabled: false\n\nLayout/LineLength:\n  Exclude:\n    - 'lib/**/**.rb'\n\nNaming/PredicatePrefix:\n  Exclude:\n    - 'lib/**/**.rb'\n\nNaming/AccessorMethodName:\n  Exclude:\n    - 'lib/**/**.rb'\n\nStyle/CaseLikeIf:\n  Exclude:\n    - 'lib/fast_jsonapi/object_serializer.rb'\n\nStyle/OptionalBooleanParameter:\n  Exclude:\n    - 'lib/fast_jsonapi/serialization_core.rb'\n    - 'lib/fast_jsonapi/relationship.rb'\n\nLint/DuplicateBranch:\n  Exclude:\n    - 'lib/fast_jsonapi/relationship.rb'\n\nStyle/DocumentDynamicEvalDefinition:\n  Exclude:\n    - 'lib/extensions/has_one.rb'\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# Changelog\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n## [Unreleased]\n- ...\n\n## [2.2.0] - 2021-03-11\n\n### Added\n- Proper error is raised on unsupported includes (#125)\n\n### Changed\n- Documentation updates (#137 #139 #143 #146)\n\n### Fixed\n- Empty relationships are no longer added to serialized doc (#116)\n- Ruby v3 compatibility (#160)\n\n## [2.1.0] - 2020-08-30\n\n### Added\n- Optional meta field to relationships (#99 #100)\n- Support for `params` on cache keys (#117)\n\n### Changed\n- Performance instrumentation (#110 #39)\n- Improved collection detection (#112)\n\n### Fixed\n- Ensure caching correctly incorporates fieldset information into the cache key to prevent incorrect fieldset caching (#90)\n- Performance optimizations for nested includes (#103)\n\n## [2.0.0] - 2020-06-22\n\nThe project was renamed to `jsonapi-serializer`! (#94)\n\n### Changed\n- Remove `ObjectSerializer#serialized_json` (#91)\n\n## [1.7.2] - 2020-05-18\n### Fixed\n- Relationship#record_type_for does not assign static record type for polymorphic relationships (#83)\n\n## [1.7.1] - 2020-05-01\n### Fixed\n- ObjectSerializer#serialized_json accepts arguments for to_json (#80)\n\n## [1.7.0] - 2020-04-29\n### Added\n- Serializer option support for procs (#32)\n- JSON serialization API method is now implementable (#44)\n\n### Changed\n- Support for polymorphic `id_method_name` (#17)\n- Relationships support for `&:proc` syntax (#58)\n- Conditional support for procs (#59)\n- Attribute support for procs (#67)\n- Refactor caching support (#52)\n- `is_collection?` is safer for objects (#18)\n\n### Removed\n- `serialized_json` is now deprecated (#44)\n\n## [1.6.0] - 2019-11-04\n### Added\n- Allow relationship links to be delcared as a method ([#2](https://github.com/fast-jsonapi/fast_jsonapi/pull/2))\n- Test against Ruby 2.6 ([#1](https://github.com/fast-jsonapi/fast_jsonapi/pull/1))\n- Include `data` key when lazy-loaded relationships are included  ([#10](https://github.com/fast-jsonapi/fast_jsonapi/pull/10))\n- Conditional links [#15](https://github.com/fast-jsonapi/fast_jsonapi/pull/15)\n- Include params on set_id block [#16](https://github.com/fast-jsonapi/fast_jsonapi/pull/16)\n### Changed\n- Optimize SerializationCore.get_included_records calculates remaining_items only once ([#4](https://github.com/fast-jsonapi/fast_jsonapi/pull/4))\n- Optimize SerializtionCore.parse_include_item by mapping in place ([#5](https://github.com/fast-jsonapi/fast_jsonapi/pull/5))\n- Define ObjectSerializer.set_key_transform mapping as a constant ([#7](https://github.com/fast-jsonapi/fast_jsonapi/pull/7))\n- Optimize SerializtionCore.remaining_items by taking from original array ([#9](https://github.com/fast-jsonapi/fast_jsonapi/pull/9))\n- Optimize ObjectSerializer.deep_symbolize by using each_with_object instead of Hash[map] ([#6](https://github.com/fast-jsonapi/fast_jsonapi/pull/6))\n"
  },
  {
    "path": "Gemfile",
    "content": "source 'https://rubygems.org'\n\n# Specify your gem's dependencies in fast_jsonapi.gemspec\ngemspec\n"
  },
  {
    "path": "LICENSE.txt",
    "content": "Apache License\nVersion 2.0, January 2004\nhttps://www.apache.org/licenses/\n\nTERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n1. Definitions.\n\n\"License\" shall mean the terms and conditions for use, reproduction,\nand distribution as defined by Sections 1 through 9 of this document.\n\n\"Licensor\" shall mean the copyright owner or entity authorized by\nthe copyright owner that is granting the License.\n\n\"Legal Entity\" shall mean the union of the acting entity and all\nother entities that control, are controlled by, or are under common\ncontrol with that entity. For the purposes of this definition,\n\"control\" means (i) the power, direct or indirect, to cause the\ndirection or management of such entity, whether by contract or\notherwise, or (ii) ownership of fifty percent (50%) or more of the\noutstanding shares, or (iii) beneficial ownership of such entity.\n\n\"You\" (or \"Your\") shall mean an individual or Legal Entity\nexercising permissions granted by this License.\n\n\"Source\" form shall mean the preferred form for making modifications,\nincluding but not limited to software source code, documentation\nsource, and configuration files.\n\n\"Object\" form shall mean any form resulting from mechanical\ntransformation or translation of a Source form, including but\nnot limited to compiled object code, generated documentation,\nand conversions to other media types.\n\n\"Work\" shall mean the work of authorship, whether in Source or\nObject form, made available under the License, as indicated by a\ncopyright notice that is included in or attached to the work\n(an example is provided in the Appendix below).\n\n\"Derivative Works\" shall mean any work, whether in Source or Object\nform, that is based on (or derived from) the Work and for which the\neditorial revisions, annotations, elaborations, or other modifications\nrepresent, as a whole, an original work of authorship. For the purposes\nof this License, Derivative Works shall not include works that remain\nseparable from, or merely link (or bind by name) to the interfaces of,\nthe Work and Derivative Works thereof.\n\n\"Contribution\" shall mean any work of authorship, including\nthe original version of the Work and any modifications or additions\nto that Work or Derivative Works thereof, that is intentionally\nsubmitted to Licensor for inclusion in the Work by the copyright owner\nor by an individual or Legal Entity authorized to submit on behalf of\nthe copyright owner. For the purposes of this definition, \"submitted\"\nmeans any form of electronic, verbal, or written communication sent\nto the Licensor or its representatives, including but not limited to\ncommunication on electronic mailing lists, source code control systems,\nand issue tracking systems that are managed by, or on behalf of, the\nLicensor for the purpose of discussing and improving the Work, but\nexcluding communication that is conspicuously marked or otherwise\ndesignated in writing by the copyright owner as \"Not a Contribution.\"\n\n\"Contributor\" shall mean Licensor and any individual or Legal Entity\non behalf of whom a Contribution has been received by Licensor and\nsubsequently incorporated within the Work.\n\n2. Grant of Copyright License. Subject to the terms and conditions of\nthis License, each Contributor hereby grants to You a perpetual,\nworldwide, non-exclusive, no-charge, royalty-free, irrevocable\ncopyright license to reproduce, prepare Derivative Works of,\npublicly display, publicly perform, sublicense, and distribute the\nWork and such Derivative Works in Source or Object form.\n\n3. Grant of Patent License. Subject to the terms and conditions of\nthis License, each Contributor hereby grants to You a perpetual,\nworldwide, non-exclusive, no-charge, royalty-free, irrevocable\n(except as stated in this section) patent license to make, have made,\nuse, offer to sell, sell, import, and otherwise transfer the Work,\nwhere such license applies only to those patent claims licensable\nby such Contributor that are necessarily infringed by their\nContribution(s) alone or by combination of their Contribution(s)\nwith the Work to which such Contribution(s) was submitted. If You\ninstitute patent litigation against any entity (including a\ncross-claim or counterclaim in a lawsuit) alleging that the Work\nor a Contribution incorporated within the Work constitutes direct\nor contributory patent infringement, then any patent licenses\ngranted to You under this License for that Work shall terminate\nas of the date such litigation is filed.\n\n4. Redistribution. You may reproduce and distribute copies of the\nWork or Derivative Works thereof in any medium, with or without\nmodifications, and in Source or Object form, provided that You\nmeet the following conditions:\n\n(a) You must give any other recipients of the Work or\nDerivative Works a copy of this License; and\n\n(b) You must cause any modified files to carry prominent notices\nstating that You changed the files; and\n\n(c) You must retain, in the Source form of any Derivative Works\nthat You distribute, all copyright, patent, trademark, and\nattribution notices from the Source form of the Work,\nexcluding those notices that do not pertain to any part of\nthe Derivative Works; and\n\n(d) If the Work includes a \"NOTICE\" text file as part of its\ndistribution, then any Derivative Works that You distribute must\ninclude a readable copy of the attribution notices contained\nwithin such NOTICE file, excluding those notices that do not\npertain to any part of the Derivative Works, in at least one\nof the following places: within a NOTICE text file distributed\nas part of the Derivative Works; within the Source form or\ndocumentation, if provided along with the Derivative Works; or,\nwithin a display generated by the Derivative Works, if and\nwherever such third-party notices normally appear. The contents\nof the NOTICE file are for informational purposes only and\ndo not modify the License. You may add Your own attribution\nnotices within Derivative Works that You distribute, alongside\nor as an addendum to the NOTICE text from the Work, provided\nthat such additional attribution notices cannot be construed\nas modifying the License.\n\nYou may add Your own copyright statement to Your modifications and\nmay provide additional or different license terms and conditions\nfor use, reproduction, or distribution of Your modifications, or\nfor any such Derivative Works as a whole, provided Your use,\nreproduction, and distribution of the Work otherwise complies with\nthe conditions stated in this License.\n\n5. Submission of Contributions. Unless You explicitly state otherwise,\nany Contribution intentionally submitted for inclusion in the Work\nby You to the Licensor shall be under the terms and conditions of\nthis License, without any additional terms or conditions.\nNotwithstanding the above, nothing herein shall supersede or modify\nthe terms of any separate license agreement you may have executed\nwith Licensor regarding such Contributions.\n\n6. Trademarks. This License does not grant permission to use the trade\nnames, trademarks, service marks, or product names of the Licensor,\nexcept as required for reasonable and customary use in describing the\norigin of the Work and reproducing the content of the NOTICE file.\n\n7. Disclaimer of Warranty. Unless required by applicable law or\nagreed to in writing, Licensor provides the Work (and each\nContributor provides its Contributions) on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\nimplied, including, without limitation, any warranties or conditions\nof TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\nPARTICULAR PURPOSE. You are solely responsible for determining the\nappropriateness of using or redistributing the Work and assume any\nrisks associated with Your exercise of permissions under this License.\n\n8. Limitation of Liability. In no event and under no legal theory,\nwhether in tort (including negligence), contract, or otherwise,\nunless required by applicable law (such as deliberate and grossly\nnegligent acts) or agreed to in writing, shall any Contributor be\nliable to You for damages, including any direct, indirect, special,\nincidental, or consequential damages of any character arising as a\nresult of this License or out of the use or inability to use the\nWork (including but not limited to damages for loss of goodwill,\nwork stoppage, computer failure or malfunction, or any and all\nother commercial damages or losses), even if such Contributor\nhas been advised of the possibility of such damages.\n\n9. Accepting Warranty or Additional Liability. While redistributing\nthe Work or Derivative Works thereof, You may choose to offer,\nand charge a fee for, acceptance of support, warranty, indemnity,\nor other liability obligations and/or rights consistent with this\nLicense. However, in accepting such obligations, You may act only\non Your own behalf and on Your sole responsibility, not on behalf\nof any other Contributor, and only if You agree to indemnify,\ndefend, and hold each Contributor harmless for any liability\nincurred by, or claims asserted against, such Contributor by reason\nof your accepting any such warranty or additional liability.\n\nEND OF TERMS AND CONDITIONS\n\nAPPENDIX: How to apply the Apache License to your work.\n\nTo apply the Apache License to your work, attach the following\nboilerplate notice, with the fields enclosed by brackets \"{}\"\nreplaced with your own identifying information. (Don't include\nthe brackets!)  The text should be enclosed in the appropriate\ncomment syntax for the file format. We also recommend that a\nfile or class name and description of purpose be included on the\nsame \"printed page\" as the copyright notice for easier\nidentification within third-party archives.\n\nCopyright {yyyy} {name of copyright owner}\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\nhttps://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n"
  },
  {
    "path": "README.md",
    "content": "# JSON:API Serialization Library\n\n## :warning: :construction: v2 (the `master` branch) is in maintenance mode! :construction: :warning:\n\nWe'll gladly accept bugfixes and security-related fixes for v2 (the `master` branch), but at this stage, contributions for new features/improvements are welcome only for v3. Please feel free to leave comments in the [v3 Pull Request](https://github.com/jsonapi-serializer/jsonapi-serializer/pull/141). \n\n---\n\nA fast [JSON:API](https://jsonapi.org/) serializer for Ruby Objects.\n\nPreviously this project was called **fast_jsonapi**, we forked the project\nand renamed it to **jsonapi/serializer** in order to keep it alive.\n\nWe would like to thank the Netflix team for the initial work and to all our\ncontributors and users for the continuous support!\n\n# Performance Comparison\n\nWe compare serialization times with `ActiveModelSerializer` and alternative\nimplementations as part of performance tests available at\n[jsonapi-serializer/comparisons](https://github.com/jsonapi-serializer/comparisons).\n\nWe want to ensure that with every\nchange on this library, serialization time stays significantly faster than\nthe performance provided by the alternatives. Please read the performance\narticle in the `docs` folder for any questions related to methodology.\n\n# Table of Contents\n\n* [Features](#features)\n* [Installation](#installation)\n* [Usage](#usage)\n  * [Rails Generator](#rails-generator)\n  * [Model Definition](#model-definition)\n  * [Serializer Definition](#serializer-definition)\n  * [Object Serialization](#object-serialization)\n  * [Compound Document](#compound-document)\n  * [Key Transforms](#key-transforms)\n  * [Collection Serialization](#collection-serialization)\n  * [Caching](#caching)\n  * [Params](#params)\n  * [Conditional Attributes](#conditional-attributes)\n  * [Conditional Relationships](#conditional-relationships)\n  * [Specifying a Relationship Serializer](#specifying-a-relationship-serializer)\n  * [Ordering `has_many` Relationship](#ordering-has_many-relationship)\n  * [Sparse Fieldsets](#sparse-fieldsets)\n  * [Using helper methods](#using-helper-methods)\n* [Performance Instrumentation](#performance-instrumentation)\n* [Deserialization](#deserialization)\n* [Migrating from Netflix/fast_jsonapi](#migrating-from-netflixfast_jsonapi)\n* [Contributing](#contributing)\n\n\n## Features\n\n* Declaration syntax similar to Active Model Serializer\n* Support for `belongs_to`, `has_many` and `has_one`\n* Support for compound documents (included)\n* Optimized serialization of compound documents\n* Caching\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'jsonapi-serializer'\n```\n\nExecute:\n\n```bash\n$ bundle install\n```\n\n## Usage\n\n### Rails Generator\nYou can use the bundled generator if you are using the library inside of\na Rails project:\n\n    rails g serializer Movie name year\n\nThis will create a new serializer in `app/serializers/movie_serializer.rb`\n\n### Model Definition\n\n```ruby\nclass Movie\n  attr_accessor :id, :name, :year, :actor_ids, :owner_id, :movie_type_id\nend\n```\n\n### Serializer Definition\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  set_type :movie  # optional\n  set_id :owner_id # optional\n  attributes :name, :year\n  has_many :actors\n  belongs_to :owner, record_type: :user\n  belongs_to :movie_type\nend\n```\n\n### Sample Object\n\n```ruby\nmovie = Movie.new\nmovie.id = 232\nmovie.name = 'test movie'\nmovie.actor_ids = [1, 2, 3]\nmovie.owner_id = 3\nmovie.movie_type_id = 1\nmovie\n\nmovies =\n  2.times.map do |i|\n    m = Movie.new\n    m.id = i + 1\n    m.name = \"test movie #{i}\"\n    m.actor_ids = [1, 2, 3]\n    m.owner_id = 3\n    m.movie_type_id = 1\n    m\n  end\n```\n\n### Object Serialization\n\n#### Return a hash\n```ruby\nhash = MovieSerializer.new(movie).serializable_hash\n```\n\n#### Return Serialized JSON\n```ruby\njson_string = MovieSerializer.new(movie).serializable_hash.to_json\n```\n\n#### Serialized Output\n\n```json\n{\n  \"data\": {\n    \"id\": \"3\",\n    \"type\": \"movie\",\n    \"attributes\": {\n      \"name\": \"test movie\",\n      \"year\": null\n    },\n    \"relationships\": {\n      \"actors\": {\n        \"data\": [\n          {\n            \"id\": \"1\",\n            \"type\": \"actor\"\n          },\n          {\n            \"id\": \"2\",\n            \"type\": \"actor\"\n          }\n        ]\n      },\n      \"owner\": {\n        \"data\": {\n          \"id\": \"3\",\n          \"type\": \"user\"\n        }\n      }\n    }\n  }\n}\n\n```\n\n#### The Optionality of `set_type`\nBy default fast_jsonapi will try to figure the type based on the name of the serializer class. For example `class MovieSerializer` will automatically have a type of `:movie`. If your serializer class name does not follow this format, you have to manually state the `set_type` at the serializer.\n\n### Key Transforms\nBy default fast_jsonapi underscores the key names. It supports the same key transforms that are supported by AMS. Here is the syntax of specifying a key transform\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  # Available options :camel, :camel_lower, :dash, :underscore(default)\n  set_key_transform :camel\nend\n```\nHere are examples of how these options transform the keys\n\n```ruby\nset_key_transform :camel # \"some_key\" => \"SomeKey\"\nset_key_transform :camel_lower # \"some_key\" => \"someKey\"\nset_key_transform :dash # \"some_key\" => \"some-key\"\nset_key_transform :underscore # \"some_key\" => \"some_key\"\n```\n\n### Attributes\nAttributes are defined using the `attributes` method.  This method is also aliased as `attribute`, which is useful when defining a single attribute.\n\nBy default, attributes are read directly from the model property of the same name.  In this example, `name` is expected to be a property of the object being serialized:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  attribute :name\nend\n```\n\nCustom attributes that must be serialized but do not exist on the model can be declared using Ruby block syntax:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  attributes :name, :year\n\n  attribute :name_with_year do |object|\n    \"#{object.name} (#{object.year})\"\n  end\nend\n```\n\nThe block syntax can also be used to override the property on the object:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  attribute :name do |object|\n    \"#{object.name} Part 2\"\n  end\nend\n```\n\nAttributes can also use a different name by passing the original method or accessor with a proc shortcut:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  attributes :name\n\n  attribute :released_in_year, &:year\nend\n```\n\n### Links Per Object\nLinks are defined using the `link` method. By default, links are read directly from the model property of the same name. In this example, `public_url` is expected to be a property of the object being serialized.\n\nYou can configure the method to use on the object for example a link with key `self` will get set to the value returned by a method called `url` on the movie object.\n\nYou can also use a block to define a url as shown in `custom_url`. You can access params in these blocks as well as shown in `personalized_url`\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  link :public_url\n\n  link :self, :url\n\n  link :custom_url do |object|\n    \"https://movies.com/#{object.name}-(#{object.year})\"\n  end\n\n  link :personalized_url do |object, params|\n    \"https://movies.com/#{object.name}-#{params[:user].reference_code}\"\n  end\nend\n```\n\n#### Links on a Relationship\n\nYou can specify [relationship links](https://jsonapi.org/format/#document-resource-object-relationships) by using the `links:` option on the serializer. Relationship links in JSON API are useful if you want to load a parent document and then load associated documents later due to size constraints (see [related resource links](https://jsonapi.org/format/#document-resource-object-related-resource-links))\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  has_many :actors, links: {\n    self: :url,\n    related: -> (object) {\n      \"https://movies.com/#{object.id}/actors\"\n    }\n  }\nend\n```\n\nRelationship links can also be configured to be defined as a method on the object.\n\n```ruby\n  has_many :actors, links: :actor_relationship_links\n```\n\nThis will create a `self` reference for the relationship, and a `related` link for loading the actors relationship later. NB: This will not automatically disable loading the data in the relationship, you'll need to do that using the `lazy_load_data` option:\n\n```ruby\n  has_many :actors, lazy_load_data: true, links: {\n    self: :url,\n    related: -> (object) {\n      \"https://movies.com/#{object.id}/actors\"\n    }\n  }\n```\n\n### Meta Per Resource\n\nFor every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.\n\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  meta do |movie|\n    {\n      years_since_release: Date.current.year - movie.year\n    }\n  end\nend\n```\n\n#### Meta on a Relationship\n\nYou can specify [relationship meta](https://jsonapi.org/format/#document-resource-object-relationships) by using the `meta:` option on the serializer. Relationship meta in JSON API is useful if you wish to provide non-standard meta-information about the relationship.\n\nMeta can be defined either by passing a static hash or by using Proc to the `meta` key. In the latter case, the record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.\n\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  has_many :actors, meta: Proc.new do |movie_record, params|\n    { count: movie_record.actors.length }\n  end\nend\n```\n\n### Compound Document\n\nSupport for top-level and nested included associations through `options[:include]`.\n\n```ruby\noptions = {}\noptions[:meta] = { total: 2 }\noptions[:links] = {\n  self: '...',\n  next: '...',\n  prev: '...'\n}\noptions[:include] = [:actors, :'actors.agency', :'actors.agency.state']\nMovieSerializer.new(movies, options).serializable_hash.to_json\n```\n\n### Collection Serialization\n\n```ruby\noptions[:meta] = { total: 2 }\noptions[:links] = {\n  self: '...',\n  next: '...',\n  prev: '...'\n}\nhash = MovieSerializer.new(movies, options).serializable_hash\njson_string = MovieSerializer.new(movies, options).serializable_hash.to_json\n```\n\n#### Control Over Collection Serialization\n\nYou can use `is_collection` option to have better control over collection serialization.\n\nIf this option is not provided or `nil` autodetect logic is used to try understand\nif provided resource is a single object or collection.\n\nAutodetect logic is compatible with most DB toolkits (ActiveRecord, Sequel, etc.) but\n**cannot** guarantee that single vs collection will be always detected properly.\n\n```ruby\noptions[:is_collection]\n```\n\nwas introduced to be able to have precise control this behavior\n\n- `nil` or not provided: will try to autodetect single vs collection (please, see notes above)\n- `true` will always treat input resource as *collection*\n- `false` will always treat input resource as *single object*\n\n### Caching\n\nTo enable caching, use `cache_options store: <cache_store>`:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  # use rails cache with a separate namespace and fixed expiry\n  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour\nend\n```\n\n`store` is required can be anything that implements a\n`#fetch(record, **options, &block)` method:\n\n- `record` is the record that is currently serialized\n- `options` is everything that was passed to `cache_options` except `store`, so it can be everything the cache store supports\n- `&block` should be executed to fetch new data if cache is empty\n\nSo for the example above it will call the cache instance like this:\n\n```ruby\nRails.cache.fetch(record, namespace: 'jsonapi-serializer', expires_in: 1.hour) { ... }\n```\n\n#### Caching and Sparse Fieldsets\n\nIf caching is enabled and fields are provided to the serializer, the fieldset will be appended to the cache key's namespace.\n\nFor example, given the following serializer definition and instance:\n```ruby\nclass ActorSerializer\n  include JSONAPI::Serializer\n\n  attributes :first_name, :last_name\n\n  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour\nend\n\nserializer = ActorSerializer.new(actor, { fields: { actor: [:first_name] } })\n```\n\nThe following cache namespace will be generated: `'jsonapi-serializer-fieldset:first_name'`.\n\n### Params\n\nIn some cases, attribute values might require more information than what is\navailable on the record, for example, access privileges or other information\nrelated to a current authenticated user. The `options[:params]` value covers these\ncases by allowing you to pass in a hash of additional parameters necessary for\nyour use case.\n\nLeveraging the new params is easy, when you define a custom id, attribute or\nrelationship with a block you opt-in to using params by adding it as a block\nparameter.\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  set_id do |movie, params|\n    # in here, params is a hash containing the `:admin` key\n    params[:admin] ? movie.owner_id : \"movie-#{movie.id}\"\n  end\n\n  attributes :name, :year\n  attribute :can_view_early do |movie, params|\n    # in here, params is a hash containing the `:current_user` key\n    params[:current_user].is_employee? ? true : false\n  end\n\n  belongs_to :primary_agent do |movie, params|\n    # in here, params is a hash containing the `:current_user` key\n    params[:current_user]\n  end\nend\n\n# ...\ncurrent_user = User.find(cookies[:current_user_id])\nserializer = MovieSerializer.new(movie, {params: {current_user: current_user}})\nserializer.serializable_hash\n```\n\nCustom attributes and relationships that only receive the resource are still possible by defining\nthe block to only receive one argument.\n\n### Conditional Attributes\n\nConditional attributes can be defined by passing a Proc to the `if` key on the `attribute` method. Return `true` if the attribute should be serialized, and `false` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  attributes :name, :year\n  attribute :release_year, if: Proc.new { |record|\n    # Release year will only be serialized if it's greater than 1990\n    record.release_year > 1990\n  }\n\n  attribute :director, if: Proc.new { |record, params|\n    # The director will be serialized only if the :admin key of params is true\n    params && params[:admin] == true\n  }\n\n  # Custom attribute `name_year` will only be serialized if both `name` and `year` fields are present\n  attribute :name_year, if: Proc.new { |record|\n    record.name.present? && record.year.present?\n  } do |object|\n    \"#{object.name} - #{object.year}\"\n  end\nend\n\n# ...\ncurrent_user = User.find(cookies[:current_user_id])\nserializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }})\nserializer.serializable_hash\n```\n\n### Conditional Relationships\n\nConditional relationships can be defined by passing a Proc to the `if` key. Return `true` if the relationship should be serialized, and `false` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  # Actors will only be serialized if the record has any associated actors\n  has_many :actors, if: Proc.new { |record| record.actors.any? }\n\n  # Owner will only be serialized if the :admin key of params is true\n  belongs_to :owner, if: Proc.new { |record, params| params && params[:admin] == true }\nend\n\n# ...\ncurrent_user = User.find(cookies[:current_user_id])\nserializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }})\nserializer.serializable_hash\n```\n\n### Specifying a Relationship Serializer\n\nIn many cases, the relationship can automatically detect the serializer to use.\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  # resolves to StudioSerializer\n  belongs_to :studio\n  # resolves to ActorSerializer\n  has_many :actors\nend\n```\n\nAt other times, such as when a property name differs from the class name, you may need to explicitly state the serializer to use.  You can do so by specifying a different symbol or the serializer class itself (which is the recommended usage):\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  # resolves to MovieStudioSerializer\n  belongs_to :studio, serializer: :movie_studio\n  # resolves to PerformerSerializer\n  has_many :actors, serializer: PerformerSerializer\nend\n```\n\nFor more advanced cases, such as polymorphic relationships and Single Table Inheritance, you may need even greater control to select the serializer based on the specific object or some specified serialization parameters. You can do by defining the serializer as a `Proc`:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  has_many :actors, serializer: Proc.new do |record, params|\n    if record.comedian?\n      ComedianSerializer\n    elsif params[:use_drama_serializer]\n      DramaSerializer\n    else\n      ActorSerializer\n    end\n  end\nend\n```\n\n### Ordering `has_many` Relationship\n\nYou can order the `has_many` relationship by providing a block:\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  has_many :actors do |movie|\n    movie.actors.order(position: :asc)\n  end\nend\n```\n\n### Sparse Fieldsets\n\nAttributes and relationships can be selectively returned per record type by using the `fields` option.\n\n```ruby\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  attributes :name, :year\nend\n\nserializer = MovieSerializer.new(movie, { fields: { movie: [:name] } })\nserializer.serializable_hash\n```\n\n### Using helper methods\n\nYou can mix-in code from another ruby module into your serializer class to reuse functions across your app.\n\nSince a serializer is evaluated in a the context of a `class` rather than an `instance` of a class, you need to make sure that your methods act as `class` methods when mixed in.\n\n\n##### Using ActiveSupport::Concern\n\n``` ruby\n\nmodule AvatarHelper\n  extend ActiveSupport::Concern\n\n  class_methods do\n    def avatar_url(user)\n      user.image.url\n    end\n  end\nend\n\nclass UserSerializer\n  include JSONAPI::Serializer\n\n  include AvatarHelper # mixes in your helper method as class method\n\n  set_type :user\n\n  attributes :name, :email\n\n  attribute :avatar do |user|\n    avatar_url(user)\n  end\nend\n\n```\n\n##### Using Plain Old Ruby\n\n``` ruby\nmodule AvatarHelper\n  def avatar_url(user)\n    user.image.url\n  end\nend\n\nclass UserSerializer\n  include JSONAPI::Serializer\n\n  extend AvatarHelper # mixes in your helper method as class method\n\n  set_type :user\n\n  attributes :name, :email\n\n  attribute :avatar do |user|\n    avatar_url(user)\n  end\nend\n\n```\n\n### Customizable Options\n\nOption | Purpose | Example\n------------ | ------------- | -------------\nset_type | Type name of Object | `set_type :movie`\nkey | Key of Object | `belongs_to :owner, key: :user`\nset_id | ID of Object | `set_id :owner_id` or `set_id { \\|record, params\\| params[:admin] ? record.id : \"#{record.name.downcase}-#{record.id}\" }`\ncache_options | Hash with store to enable caching and optional further cache options | `cache_options store: ActiveSupport::Cache::MemoryStore.new, expires_in: 5.minutes`\nid_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | `has_many :locations, id_method_name: :place_ids`\nobject_method_name | Set custom method name to get related objects | `has_many :locations, object_method_name: :places`\nrecord_type | Set custom Object Type for a relationship | `belongs_to :owner, record_type: :user`\nserializer | Set custom Serializer for a relationship | `has_many :actors, serializer: :custom_actor`, `has_many :actors, serializer: MyApp::Api::V1::ActorSerializer`, or `has_many :actors, serializer -> (object, params) { (return a serializer class) }`\npolymorphic | Allows different record types for a polymorphic association | `has_many :targets, polymorphic: true`\npolymorphic | Sets custom record types for each object class in a polymorphic association | `has_many :targets, polymorphic: { Person => :person, Group => :group }`\n\n### Performance Instrumentation\n\nPerformance instrumentation is available by using the\n`active_support/notifications`.\n\nTo enable it, include the module in your serializer class:\n\n```ruby\nrequire 'jsonapi/serializer'\nrequire 'jsonapi/serializer/instrumentation'\n\nclass MovieSerializer\n  include JSONAPI::Serializer\n  include JSONAPI::Serializer::Instrumentation\n\n  # ...\nend\n```\n\n[Skylight](https://www.skylight.io/) integration is also available and\nsupported by us, follow the Skylight documentation to enable it.\n\n### Running Tests\nThe project has and requires unit tests, functional tests and performance\ntests. To run tests use the following command:\n\n```bash\nrspec\n```\n\n## Deserialization\nWe currently do not support deserialization, but we recommend to use any of the next gems:\n\n### [JSONAPI.rb](https://github.com/stas/jsonapi.rb)\n\nThis gem provides the next features alongside deserialization:\n- Collection meta\n- Error handling\n- Includes and sparse fields\n- Filtering and sorting\n- Pagination\n\n## Migrating from Netflix/fast_jsonapi\n\nIf you come from [Netflix/fast_jsonapi](https://github.com/Netflix/fast_jsonapi), here is the instructions to switch.\n\n### Modify your Gemfile\n\n```diff\n- gem 'fast_jsonapi'\n+ gem 'jsonapi-serializer'\n```\n\n### Replace all constant references\n\n```diff\nclass MovieSerializer\n- include FastJsonapi::ObjectSerializer\n+ include JSONAPI::Serializer\nend\n```\n\n### Replace removed methods\n\n```diff\n- json_string = MovieSerializer.new(movie).serialized_json\n+ json_string = MovieSerializer.new(movie).serializable_hash.to_json\n```\n\n### Replace require references\n\n```diff\n- require 'fast_jsonapi'\n+ require 'jsonapi/serializer'\n```\n\n### Update your cache options\n\nSee [docs](https://github.com/jsonapi-serializer/jsonapi-serializer#caching).\n\n```diff\n- cache_options enabled: true, cache_length: 12.hours\n+ cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour\n```\n\n## Contributing\n\nPlease follow the instructions we provide as part of the issue and\npull request creation processes.\n\nThis project is intended to be a safe, welcoming space for collaboration, and\ncontributors are expected to adhere to the\n[Contributor Covenant](https://contributor-covenant.org) code of conduct.\n"
  },
  {
    "path": "Rakefile",
    "content": "require 'bundler/gem_tasks'\nrequire 'rspec/core/rake_task'\nrequire 'rubocop/rake_task'\n\ndesc('Codestyle check and linter')\nRuboCop::RakeTask.new('rubocop') do |task|\n  task.fail_on_error = true\n  task.patterns = [\n    'lib/**/*.rb',\n    'spec/**/*.rb'\n  ]\nend\n\nRSpec::Core::RakeTask.new(:spec)\ntask(default: [:rubocop, :spec])\n"
  },
  {
    "path": "docs/ISSUE_TEMPLATE.md",
    "content": "## Expected Behavior\n\n\n## Actual Behavior\n\n\n## Steps to Reproduce the Problem\n\n  1.\n  2.\n  3.\n\n## Specifications\n\n  - Version:\n  - Ruby version:\n"
  },
  {
    "path": "docs/PULL_REQUEST_TEMPLATE.md",
    "content": "## What is the current behavior?\n\n<!-- Please describe the current behavior that you are modifying, or link to a\n  relevant issue. -->\n\n## What is the new behavior?\n\n<!-- Please describe the behavior or changes that are being added here. -->\n\n## Checklist\n\nPlease make sure the following requirements are complete:\n\n- [ ] Tests for the changes have been added (for bug fixes / features)\n- [ ] Docs have been reviewed and added / updated if needed (for bug fixes /\n  features)\n- [ ] All automated checks pass (CI/CD)\n"
  },
  {
    "path": "docs/json_serialization.md",
    "content": "# JSON Serialization Support\n\nSupport for JSON serialization is no longer provided as part of the API of\n`fast_jsonapi`. This decision (see #12) is based on the idea that developers\nknow better what library for JSON serialization works best for their project.\n\nTo bring back the old functionality, define the `to_json` or `serialized_json`\nmethods with the relevant JSON library call. Here's an example on how to get\nit working with the popular `oj` gem:\n\n```ruby\nrequire 'oj'\nrequire 'fast_jsonapi'\n\nclass BaseSerializer\n  include JSONAPI::Serializer\n\n  def to_json\n    Oj.dump(serializable_hash)\n  end\n  alias_method :serialized_json, :to_json\nend\n\nclass MovieSerializer < BaseSerializer\n  # ...\nend\n```\n"
  },
  {
    "path": "docs/performance_methodology.md",
    "content": "# Performance using Fast JSON API\n\nWe have been getting a few questions about Fast JSON API's performance\nstatistics and the methodology used to measure the performance. This article is\nan attempt at addressing this aspect of the gem.\n\n## Prologue\n\nWith use cases like infinite scroll on complex models and bulk update on index\npages, we started observing performance degradation on our Rails APIs. Our\nfirst step was to enable instrumentation and then tune for performance. We\nrealized that, on average, more than 50% of the time was being spent on AMS\nserialization. At the same time, we had a couple of APIs that were simply\nproxying requests on top of a non-Rails, non-JSON API endpoint. Guess what? The\nnon-Rails endpoints were giving us serialized JSON back in a fraction of the\ntime spent by AMS.\n\nThis led us to explore AMS documentation in depth in an effort to try a variety\nof techniques such as caching, using OJ for JSON string generation etc. It\ndidn't yield the consistent results we were hoping to get. We loved the\ndeveloper experience of using AMS, but wanted better performance for our use\ncases.\n\nWe came up with patterns that we can rely upon such as:\n\n* We always use [JSON:API](https://jsonapi.org/) for our APIs\n* We almost always serialize a homogenous list of objects (Example: An array of\n  movies)\n\nOn the other hand:\n\n* AMS is designed to serialize JSON in several different formats, not just\n  JSON:API\n* AMS can also handle lists that are not homogenous\n\nThis led us to build our own object serialization library that would be faster\nbecause it would be tailored to our requirements. The usage of `fast_jsonapi`\ninternally on production environments resulted in significant performance\ngains.\n\n## Benchmark Setup\n\nThe benchmark setup is simple with classes for `Movie, Actor, MovieType, User`\non `movie_context.rb` for `fast_jsonapi` serializers and on `ams_context.rb`\nfor AMS serializers. We benchmark the serializers with 1, 25, 250, 1000 movies,\nthen we output the result.\n\nWe also ensure that JSON string output is equivalent to ensure neither library\nis doing excess work compared to the other. Please checkout\n`spec/object_serializer_performance_spec.rb`\n\n## Benchmark Results\n\nWe benchmarked results for creating a Ruby Hash. This approach removes the\neffect of chosen JSON string generation engines like OJ, Yajl etc. Benchmarks\nindicate that `fast_jsonapi` consistently performs around 25 times faster\nthan AMS in generating a ruby hash.\n\nWe applied a similar benchmark on the operation to serialize the objects to a\nJSON string. This approach helps with ensuring some important criterias, such\nas:\n\n* OJ is used as the JSON engine for benchmarking both AMS and `fast_jsonapi`\n* The benchmark is easy to understand\n* The benchmark helps to improve performance\n* The benchmark influences design decisions for the gem\n\nThis gem is currently used in several APIs at Netflix and has reduced the\nresponse times by more than half on many of these APIs. We truly appreciate the\nRuby and Rails communities and wanted to contribute in an effort to help\nimprove the performance of your APIs too.\n\n## Epilogue\n\n`fast_jsonapi` is not a replacement for AMS. AMS is a great gem, and it does\nmany things and is very flexible. We still use it for non JSON:API\nserialization and deserialization. What started off as an internal performance\nexercise evolved into `fast_jsonapi` and created an opportunity to give\nsomething back to the awesome **Ruby and Rails communities**.\n\nWe are excited to share it with all of you since we believe that there will be\n**no** end to this need for speed on APIs. :)\n"
  },
  {
    "path": "jsonapi-serializer.gemspec",
    "content": "lib = File.expand_path('lib', __dir__)\n$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)\n\nrequire 'jsonapi/serializer/version'\n\nGem::Specification.new do |gem|\n  gem.name = 'jsonapi-serializer'\n  gem.version = JSONAPI::Serializer::VERSION\n\n  gem.authors = ['JSON:API Serializer Community']\n  gem.email = ''\n\n  gem.summary = 'Fast JSON:API serialization library'\n  gem.description = 'Fast, simple and easy to use ' \\\n                    'JSON:API serialization library (also known as fast_jsonapi).'\n  gem.homepage = 'https://github.com/jsonapi-serializer/jsonapi-serializer'\n  gem.licenses = ['Apache-2.0']\n  gem.files = Dir['lib/**/*']\n  gem.require_paths = ['lib']\n  gem.extra_rdoc_files = ['LICENSE.txt', 'README.md']\n\n  gem.add_dependency('activesupport', '>= 4.2')\n\n  gem.add_development_dependency('activerecord')\n  gem.add_development_dependency('bundler')\n  gem.add_development_dependency('byebug')\n  gem.add_development_dependency('ffaker')\n  gem.add_development_dependency('jsonapi-rspec', '>= 0.0.5')\n  gem.add_development_dependency('rake')\n  gem.add_development_dependency('rspec')\n  gem.add_development_dependency('rubocop')\n  gem.add_development_dependency('rubocop-performance')\n  gem.add_development_dependency('rubocop-rspec')\n  gem.add_development_dependency('simplecov')\n  gem.add_development_dependency('sqlite3')\n  gem.metadata['rubygems_mfa_required'] = 'true'\nend\n"
  },
  {
    "path": "lib/extensions/has_one.rb",
    "content": "# frozen_string_literal: true\n\nActiveRecord::Associations::Builder::HasOne.class_eval do\n  # Based on\n  # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/collection_association.rb#L50\n  # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/singular_association.rb#L11\n  def self.define_accessors(mixin, reflection)\n    super\n    name = reflection.name\n    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1\n      def #{name}_id\n        # if an attribute is already defined with this methods name we should just use it\n        return read_attribute(__method__) if has_attribute?(__method__)\n        association(:#{name}).reader.try(:id)\n      end\n    CODE\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/attribute.rb",
    "content": "require 'fast_jsonapi/scalar'\n\nmodule FastJsonapi\n  class Attribute < Scalar; end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/helpers.rb",
    "content": "module FastJsonapi\n  class << self\n    # Calls either a Proc or a Lambda, making sure to never pass more parameters to it than it can receive\n    #\n    # @param [Proc] proc the Proc or Lambda to call\n    # @param [Array<Object>] *params any number of parameters to be passed to the Proc\n    # @return [Object] the result of the Proc call with the supplied parameters\n    def call_proc(proc, *params)\n      # The parameters array for a lambda created from a symbol (&:foo) differs\n      # from explictly defined procs/lambdas, so we can't deduce the number of\n      # parameters from the array length (and differs between Ruby 2.x and 3).\n      # In the case of negative arity -- unlimited/unknown argument count --\n      # just send the object to act as the method receiver.\n      if proc.arity.negative?\n        proc.call(params.first)\n      else\n        proc.call(*params.take(proc.parameters.length))\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/instrumentation/skylight.rb",
    "content": "require 'skylight'\n\nwarn('DEPRECATION: Skylight support was moved into the `skylight` gem.')\n"
  },
  {
    "path": "lib/fast_jsonapi/instrumentation.rb",
    "content": "require 'jsonapi/serializer/instrumentation'\n\nwarn(\n  'DEPRECATION: Performance instrumentation is no longer automatic. See: ' \\\n  'https://github.com/jsonapi-serializer/jsonapi-serializer' \\\n  '#performance-instrumentation'\n)\n"
  },
  {
    "path": "lib/fast_jsonapi/link.rb",
    "content": "require 'fast_jsonapi/scalar'\n\nmodule FastJsonapi\n  class Link < Scalar; end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/object_serializer.rb",
    "content": "# frozen_string_literal: true\n\nrequire 'active_support'\nrequire 'active_support/time'\nrequire 'active_support/concern'\nrequire 'active_support/inflector'\nrequire 'active_support/core_ext/numeric/time'\nrequire 'fast_jsonapi/helpers'\nrequire 'fast_jsonapi/attribute'\nrequire 'fast_jsonapi/relationship'\nrequire 'fast_jsonapi/link'\nrequire 'fast_jsonapi/serialization_core'\n\nmodule FastJsonapi\n  module ObjectSerializer\n    extend ActiveSupport::Concern\n    include SerializationCore\n\n    TRANSFORMS_MAPPING = {\n      camel: :camelize,\n      camel_lower: [:camelize, :lower],\n      dash: :dasherize,\n      underscore: :underscore\n    }.freeze\n\n    included do\n      # Set record_type based on the name of the serializer class\n      set_type(reflected_record_type) if reflected_record_type\n    end\n\n    def initialize(resource, options = {})\n      process_options(options)\n\n      @resource = resource\n    end\n\n    def serializable_hash\n      if self.class.is_collection?(@resource, @is_collection)\n        return hash_for_collection\n      end\n\n      hash_for_one_record\n    end\n    alias to_hash serializable_hash\n\n    def hash_for_one_record\n      serializable_hash = { data: nil }\n      serializable_hash[:meta] = @meta if @meta.present?\n      serializable_hash[:links] = @links if @links.present?\n\n      return serializable_hash unless @resource\n\n      serializable_hash[:data] = self.class.record_hash(@resource, @fieldsets[self.class.record_type.to_sym], @includes, @params)\n      serializable_hash[:included] = self.class.get_included_records(@resource, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?\n      serializable_hash\n    end\n\n    def hash_for_collection\n      serializable_hash = {}\n\n      data = []\n      included = []\n      fieldset = @fieldsets[self.class.record_type.to_sym]\n      @resource.each do |record|\n        data << self.class.record_hash(record, fieldset, @includes, @params)\n        included.concat self.class.get_included_records(record, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?\n      end\n\n      serializable_hash[:data] = data\n      serializable_hash[:included] = included if @includes.present?\n      serializable_hash[:meta] = @meta if @meta.present?\n      serializable_hash[:links] = @links if @links.present?\n      serializable_hash\n    end\n\n    private\n\n    def process_options(options)\n      @fieldsets = deep_symbolize(options[:fields].presence || {})\n      @params = {}\n\n      return if options.blank?\n\n      @known_included_objects = Set.new\n      @meta = options[:meta]\n      @links = options[:links]\n      @is_collection = options[:is_collection]\n      @params = options[:params] || {}\n      raise ArgumentError, '`params` option passed to serializer must be a hash' unless @params.is_a?(Hash)\n\n      if options[:include].present?\n        @includes = options[:include].reject(&:blank?).map(&:to_sym)\n        self.class.validate_includes!(@includes)\n      end\n    end\n\n    def deep_symbolize(collection)\n      if collection.is_a? Hash\n        collection.each_with_object({}) do |(k, v), hsh|\n          hsh[k.to_sym] = deep_symbolize(v)\n        end\n      elsif collection.is_a? Array\n        collection.map { |i| deep_symbolize(i) }\n      else\n        collection.to_sym\n      end\n    end\n\n    class_methods do\n      # Detects a collection/enumerable\n      #\n      # @return [TrueClass] on a successful detection\n      def is_collection?(resource, force_is_collection = nil)\n        return force_is_collection unless force_is_collection.nil?\n\n        resource.is_a?(Enumerable) && !resource.respond_to?(:each_pair)\n      end\n\n      def inherited(subclass)\n        super\n        subclass.attributes_to_serialize = attributes_to_serialize.dup if attributes_to_serialize.present?\n        subclass.relationships_to_serialize = relationships_to_serialize.dup if relationships_to_serialize.present?\n        subclass.cachable_relationships_to_serialize = cachable_relationships_to_serialize.dup if cachable_relationships_to_serialize.present?\n        subclass.uncachable_relationships_to_serialize = uncachable_relationships_to_serialize.dup if uncachable_relationships_to_serialize.present?\n        subclass.transform_method = transform_method\n        subclass.data_links = data_links.dup if data_links.present?\n        subclass.cache_store_instance = cache_store_instance\n        subclass.cache_store_options = cache_store_options\n        subclass.set_type(subclass.reflected_record_type) if subclass.reflected_record_type\n        subclass.meta_to_serialize = meta_to_serialize\n        subclass.record_id = record_id\n      end\n\n      def reflected_record_type\n        return @reflected_record_type if defined?(@reflected_record_type)\n\n        @reflected_record_type ||= (name.split('::').last.chomp('Serializer').underscore.to_sym if name&.end_with?('Serializer'))\n      end\n\n      def set_key_transform(transform_name)\n        self.transform_method = TRANSFORMS_MAPPING[transform_name.to_sym]\n\n        # ensure that the record type is correctly transformed\n        if record_type\n          set_type(record_type)\n        # TODO: Remove dead code\n        elsif reflected_record_type\n          set_type(reflected_record_type)\n        end\n      end\n\n      def run_key_transform(input)\n        if transform_method.present?\n          input.to_s.send(*@transform_method).to_sym\n        else\n          input.to_sym\n        end\n      end\n\n      def use_hyphen\n        warn('DEPRECATION WARNING: use_hyphen is deprecated and will be removed from fast_jsonapi 2.0 use (set_key_transform :dash) instead')\n        set_key_transform :dash\n      end\n\n      def set_type(type_name)\n        self.record_type = run_key_transform(type_name)\n      end\n\n      def set_id(id_name = nil, &block)\n        self.record_id = block || id_name\n      end\n\n      def cache_options(cache_options)\n        # FIXME: remove this if block once deprecated cache_options are not supported anymore\n        unless cache_options.key?(:store)\n          # fall back to old, deprecated behaviour because no store was passed.\n          # we assume the user explicitly wants new behaviour if he passed a\n          # store because this is the new syntax.\n          deprecated_cache_options(cache_options)\n          return\n        end\n\n        self.cache_store_instance = cache_options[:store]\n        self.cache_store_options = cache_options.except(:store)\n      end\n\n      # FIXME: remove this method once deprecated cache_options are not supported anymore\n      def deprecated_cache_options(cache_options)\n        warn('DEPRECATION WARNING: `store:` is a required cache option, we will default to `Rails.cache` for now. See https://github.com/fast-jsonapi/fast_jsonapi#caching for more information.')\n\n        %i[enabled cache_length].select { |key| cache_options.key?(key) }.each do |key|\n          warn(\"DEPRECATION WARNING: `#{key}` is a deprecated cache option and will have no effect soon. See https://github.com/fast-jsonapi/fast_jsonapi#caching for more information.\")\n        end\n\n        self.cache_store_instance = cache_options[:enabled] ? Rails.cache : nil\n        self.cache_store_options = {\n          expires_in: cache_options[:cache_length] || 5.minutes,\n          race_condition_ttl: cache_options[:race_condition_ttl] || 5.seconds\n        }\n      end\n\n      def attributes(*attributes_list, &block)\n        attributes_list = attributes_list.first if attributes_list.first.class.is_a?(Array)\n        options = attributes_list.last.is_a?(Hash) ? attributes_list.pop : {}\n        self.attributes_to_serialize = {} if attributes_to_serialize.nil?\n\n        # to support calling `attribute` with a lambda, e.g `attribute :key, ->(object) { ... }`\n        block = attributes_list.pop if attributes_list.last.is_a?(Proc)\n\n        attributes_list.each do |attr_name|\n          method_name = attr_name\n          key = run_key_transform(method_name)\n          attributes_to_serialize[key] = Attribute.new(\n            key: key,\n            method: block || method_name,\n            options: options\n          )\n        end\n      end\n\n      alias_method :attribute, :attributes\n\n      def add_relationship(relationship)\n        self.relationships_to_serialize = {} if relationships_to_serialize.nil?\n        self.cachable_relationships_to_serialize = {} if cachable_relationships_to_serialize.nil?\n        self.uncachable_relationships_to_serialize = {} if uncachable_relationships_to_serialize.nil?\n\n        # TODO: Remove this undocumented option.\n        #   Delegate the caching to the serializer exclusively.\n        if relationship.cached\n          cachable_relationships_to_serialize[relationship.name] = relationship\n        else\n          uncachable_relationships_to_serialize[relationship.name] = relationship\n        end\n        relationships_to_serialize[relationship.name] = relationship\n      end\n\n      def has_many(relationship_name, options = {}, &block)\n        relationship = create_relationship(relationship_name, :has_many, options, block)\n        add_relationship(relationship)\n      end\n\n      def has_one(relationship_name, options = {}, &block)\n        relationship = create_relationship(relationship_name, :has_one, options, block)\n        add_relationship(relationship)\n      end\n\n      def belongs_to(relationship_name, options = {}, &block)\n        relationship = create_relationship(relationship_name, :belongs_to, options, block)\n        add_relationship(relationship)\n      end\n\n      def meta(meta_name = nil, &block)\n        self.meta_to_serialize = block || meta_name\n      end\n\n      def create_relationship(base_key, relationship_type, options, block)\n        name = base_key.to_sym\n        if relationship_type == :has_many\n          base_serialization_key = base_key.to_s.singularize\n          id_postfix = '_ids'\n        else\n          base_serialization_key = base_key\n          id_postfix = '_id'\n        end\n        polymorphic = fetch_polymorphic_option(options)\n\n        Relationship.new(\n          owner: self,\n          key: options[:key] || run_key_transform(base_key),\n          name: name,\n          id_method_name: compute_id_method_name(\n            options[:id_method_name],\n            :\"#{base_serialization_key}#{id_postfix}\",\n            polymorphic,\n            options[:serializer],\n            block\n          ),\n          record_type: options[:record_type],\n          object_method_name: options[:object_method_name] || name,\n          object_block: block,\n          serializer: options[:serializer],\n          relationship_type: relationship_type,\n          cached: options[:cached],\n          polymorphic: polymorphic,\n          conditional_proc: options[:if],\n          transform_method: @transform_method,\n          meta: options[:meta],\n          links: options[:links],\n          lazy_load_data: options[:lazy_load_data]\n        )\n      end\n\n      def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, polymorphic, serializer, block)\n        if block.present? || serializer.is_a?(Proc) || polymorphic\n          custom_id_method_name || :id\n        else\n          custom_id_method_name || id_method_name_from_relationship\n        end\n      end\n\n      def serializer_for(name)\n        namespace = self.name.gsub(/()?\\w+Serializer$/, '')\n        serializer_name = \"#{name.to_s.demodulize.classify}Serializer\"\n        serializer_class_name = namespace + serializer_name\n        begin\n          serializer_class_name.constantize\n        rescue NameError\n          raise NameError, \"#{self.name} cannot resolve a serializer class for '#{name}'.  \" \\\n                           \"Attempted to find '#{serializer_class_name}'. \" \\\n                           'Consider specifying the serializer directly through options[:serializer].'\n        end\n      end\n\n      def fetch_polymorphic_option(options)\n        option = options[:polymorphic]\n        return false unless option.present?\n        return option if option.respond_to? :keys\n\n        {}\n      end\n\n      # def link(link_name, link_method_name = nil, &block)\n      def link(*params, &block)\n        self.data_links = {} if data_links.nil?\n\n        options = params.last.is_a?(Hash) ? params.pop : {}\n        link_name = params.first\n        link_method_name = params[-1]\n        key = run_key_transform(link_name)\n\n        data_links[key] = Link.new(\n          key: key,\n          method: block || link_method_name,\n          options: options\n        )\n      end\n\n      def validate_includes!(includes)\n        return if includes.blank?\n\n        parse_includes_list(includes).each_key do |include_item|\n          relationship_to_include = relationships_to_serialize[include_item]\n          raise(JSONAPI::Serializer::UnsupportedIncludeError.new(include_item, name)) unless relationship_to_include\n\n          relationship_to_include.static_serializer # called for a side-effect to check for a known serializer class.\n        end\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/railtie.rb",
    "content": "# frozen_string_literal: true\n\nrequire 'rails/railtie'\n\nclass Railtie < Rails::Railtie\n  initializer 'fast_jsonapi.active_record' do\n    ActiveSupport.on_load :active_record do\n      require 'extensions/has_one'\n    end\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/relationship.rb",
    "content": "module FastJsonapi\n  class Relationship\n    attr_reader :owner, :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method, :links, :meta, :lazy_load_data\n\n    def initialize(\n      owner:,\n      key:,\n      name:,\n      id_method_name:,\n      record_type:,\n      object_method_name:,\n      object_block:,\n      serializer:,\n      relationship_type:,\n      polymorphic:,\n      conditional_proc:,\n      transform_method:,\n      links:,\n      meta:,\n      cached: false,\n      lazy_load_data: false\n    )\n      @owner = owner\n      @key = key\n      @name = name\n      @id_method_name = id_method_name\n      @record_type = record_type\n      @object_method_name = object_method_name\n      @object_block = object_block\n      @serializer = serializer\n      @relationship_type = relationship_type\n      @cached = cached\n      @polymorphic = polymorphic\n      @conditional_proc = conditional_proc\n      @transform_method = transform_method\n      @links = links || {}\n      @meta = meta || {}\n      @lazy_load_data = lazy_load_data\n      @record_types_for = {}\n      @serializers_for_name = {}\n    end\n\n    def serialize(record, included, serialization_params, output_hash)\n      if include_relationship?(record, serialization_params)\n        empty_case = relationship_type == :has_many ? [] : nil\n\n        output_hash[key] = {}\n        output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case unless lazy_load_data && !included\n\n        add_meta_hash(record, serialization_params, output_hash) if meta.present?\n        add_links_hash(record, serialization_params, output_hash) if links.present?\n      end\n    end\n\n    def fetch_associated_object(record, params)\n      return FastJsonapi.call_proc(object_block, record, params) unless object_block.nil?\n\n      record.send(object_method_name)\n    end\n\n    def include_relationship?(record, serialization_params)\n      if conditional_proc.present?\n        FastJsonapi.call_proc(conditional_proc, record, serialization_params)\n      else\n        true\n      end\n    end\n\n    def serializer_for(record, serialization_params)\n      # TODO: Remove this, dead code...\n      if @static_serializer\n        @static_serializer\n\n      elsif polymorphic\n        name = polymorphic[record.class] if polymorphic.is_a?(Hash)\n        name ||= record.class.name\n        serializer_for_name(name)\n\n      elsif serializer.is_a?(Proc)\n        FastJsonapi.call_proc(serializer, record, serialization_params)\n\n      elsif object_block\n        serializer_for_name(record.class.name)\n\n      else\n        # TODO: Remove this, dead code...\n        raise \"Unknown serializer for object #{record.inspect}\"\n      end\n    end\n\n    def static_serializer\n      initialize_static_serializer unless @initialized_static_serializer\n      @static_serializer\n    end\n\n    def static_record_type\n      initialize_static_serializer unless @initialized_static_serializer\n      @static_record_type\n    end\n\n    private\n\n    def ids_hash_from_record_and_relationship(record, params = {})\n      initialize_static_serializer unless @initialized_static_serializer\n\n      return ids_hash(fetch_id(record, params), @static_record_type) if @static_record_type\n\n      return unless associated_object = fetch_associated_object(record, params)\n\n      if associated_object.respond_to? :map\n        return associated_object.map do |object|\n          id_hash_from_record object, params\n        end\n      end\n\n      id_hash_from_record associated_object, params\n    end\n\n    def id_hash_from_record(record, params)\n      associated_record_type = record_type_for(record, params)\n      id_hash(record.public_send(id_method_name), associated_record_type)\n    end\n\n    def ids_hash(ids, record_type)\n      return ids.map { |id| id_hash(id, record_type) } if ids.respond_to? :map\n\n      id_hash(ids, record_type) # ids variable is just a single id here\n    end\n\n    def id_hash(id, record_type, default_return = false)\n      if id.present?\n        { id: id.to_s, type: record_type }\n      else\n        default_return ? { id: nil, type: record_type } : nil\n      end\n    end\n\n    def fetch_id(record, params)\n      if object_block.present?\n        object = FastJsonapi.call_proc(object_block, record, params)\n        return object.map { |item| item.public_send(id_method_name) } if object.respond_to? :map\n\n        return object.try(id_method_name)\n      end\n      record.public_send(id_method_name)\n    end\n\n    def add_links_hash(record, params, output_hash)\n      output_hash[key][:links] = if links.is_a?(Symbol)\n                                   record.public_send(links)\n                                 else\n                                   links.each_with_object({}) do |(key, method), hash|\n                                     Link.new(key: key, method: method).serialize(record, params, hash)\n                                   end\n                                 end\n    end\n\n    def add_meta_hash(record, params, output_hash)\n      output_hash[key][:meta] = if meta.is_a?(Proc)\n                                  FastJsonapi.call_proc(meta, record, params)\n                                else\n                                  meta\n                                end\n    end\n\n    def run_key_transform(input)\n      if transform_method.present?\n        input.to_s.send(*transform_method).to_sym\n      else\n        input.to_sym\n      end\n    end\n\n    def initialize_static_serializer\n      return if @initialized_static_serializer\n\n      @static_serializer = compute_static_serializer\n      @static_record_type = compute_static_record_type\n      @initialized_static_serializer = true\n    end\n\n    def compute_static_serializer\n      if polymorphic\n        # polymorphic without a specific serializer --\n        # the serializer is determined on a record-by-record basis\n        nil\n\n      elsif serializer.is_a?(Symbol) || serializer.is_a?(String)\n        # a serializer was explicitly specified by name -- determine the serializer class\n        serializer_for_name(serializer)\n\n      elsif serializer.is_a?(Proc)\n        # the serializer is a Proc to be executed per object -- not static\n        nil\n\n      elsif serializer\n        # something else was specified, e.g. a specific serializer class -- return it\n        serializer\n\n      elsif object_block\n        # an object block is specified without a specific serializer --\n        # assume the objects might be different and infer the serializer by their class\n        nil\n\n      else\n        # no serializer information was provided -- infer it from the relationship name\n        serializer_name = name.to_s\n        serializer_name = serializer_name.singularize if relationship_type.to_sym == :has_many\n        serializer_for_name(serializer_name)\n      end\n    end\n\n    def serializer_for_name(name)\n      @serializers_for_name[name] ||= owner.serializer_for(name)\n    end\n\n    def record_type_for(record, serialization_params)\n      # if the record type is static, return it\n      return @static_record_type if @static_record_type\n\n      # if not, use the record type of the serializer, and memoize the transformed version\n      serializer = serializer_for(record, serialization_params)\n      @record_types_for[serializer] ||= run_key_transform(serializer.record_type)\n    end\n\n    def compute_static_record_type\n      if polymorphic\n        nil\n      elsif record_type\n        run_key_transform(record_type)\n      elsif @static_serializer\n        run_key_transform(@static_serializer.record_type)\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/scalar.rb",
    "content": "module FastJsonapi\n  class Scalar\n    attr_reader :key, :method, :conditional_proc\n\n    def initialize(key:, method:, options: {})\n      @key = key\n      @method = method\n      @conditional_proc = options[:if]\n    end\n\n    def serialize(record, serialization_params, output_hash)\n      if conditionally_allowed?(record, serialization_params)\n        if method.is_a?(Proc)\n          output_hash[key] = FastJsonapi.call_proc(method, record, serialization_params)\n        else\n          output_hash[key] = record.public_send(method)\n        end\n      end\n    end\n\n    def conditionally_allowed?(record, serialization_params)\n      if conditional_proc.present?\n        FastJsonapi.call_proc(conditional_proc, record, serialization_params)\n      else\n        true\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/serialization_core.rb",
    "content": "# frozen_string_literal: true\n\nrequire 'active_support'\nrequire 'active_support/concern'\nrequire 'digest/sha1'\n\nmodule FastJsonapi\n  MandatoryField = Class.new(StandardError)\n\n  module SerializationCore\n    extend ActiveSupport::Concern\n\n    included do\n      class << self\n        attr_accessor :attributes_to_serialize,\n                      :relationships_to_serialize,\n                      :cachable_relationships_to_serialize,\n                      :uncachable_relationships_to_serialize,\n                      :transform_method,\n                      :record_type,\n                      :record_id,\n                      :cache_store_instance,\n                      :cache_store_options,\n                      :data_links,\n                      :meta_to_serialize\n      end\n    end\n\n    class_methods do\n      def id_hash(id, record_type, default_return = false)\n        if id.present?\n          { id: id.to_s, type: record_type }\n        else\n          default_return ? { id: nil, type: record_type } : nil\n        end\n      end\n\n      def links_hash(record, params = {})\n        data_links.each_with_object({}) do |(_k, link), hash|\n          link.serialize(record, params, hash)\n        end\n      end\n\n      def attributes_hash(record, fieldset = nil, params = {})\n        attributes = attributes_to_serialize\n        attributes = attributes.slice(*fieldset) if fieldset.present?\n        attributes = {} if fieldset == []\n\n        attributes.each_with_object({}) do |(_k, attribute), hash|\n          attribute.serialize(record, params, hash)\n        end\n      end\n\n      def relationships_hash(record, relationships = nil, fieldset = nil, includes_list = nil, params = {})\n        relationships = relationships_to_serialize if relationships.nil?\n        relationships = relationships.slice(*fieldset) if fieldset.present?\n        relationships = {} if fieldset == []\n\n        relationships.each_with_object({}) do |(key, relationship), hash|\n          included = includes_list.present? && includes_list.include?(key)\n          relationship.serialize(record, included, params, hash)\n        end\n      end\n\n      def meta_hash(record, params = {})\n        FastJsonapi.call_proc(meta_to_serialize, record, params)\n      end\n\n      def record_hash(record, fieldset, includes_list, params = {})\n        if cache_store_instance\n          cache_opts = record_cache_options(cache_store_options, fieldset, includes_list, params)\n          record_hash = cache_store_instance.fetch(record, **cache_opts) do\n            temp_hash = id_hash(id_from_record(record, params), record_type, true)\n            temp_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?\n            temp_hash[:relationships] = relationships_hash(record, cachable_relationships_to_serialize, fieldset, includes_list, params) if cachable_relationships_to_serialize.present?\n            temp_hash[:links] = links_hash(record, params) if data_links.present?\n            temp_hash\n          end\n          record_hash[:relationships] = (record_hash[:relationships] || {}).merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present?\n        else\n          record_hash = id_hash(id_from_record(record, params), record_type, true)\n          record_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?\n          record_hash[:relationships] = relationships_hash(record, nil, fieldset, includes_list, params) if relationships_to_serialize.present?\n          record_hash[:links] = links_hash(record, params) if data_links.present?\n        end\n\n        record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present?\n        record_hash\n      end\n\n      # Cache options helper. Use it to adapt cache keys/rules.\n      #\n      # If a fieldset is specified, it modifies the namespace to include the\n      # fields from the fieldset.\n      #\n      # @param options [Hash] default cache options\n      # @param fieldset [Array, nil] passed fieldset values\n      # @param includes_list [Array, nil] passed included values\n      # @param params [Hash] the serializer params\n      #\n      # @return [Hash] processed options hash\n      # rubocop:disable Lint/UnusedMethodArgument\n      def record_cache_options(options, fieldset, includes_list, params)\n        return options unless fieldset\n\n        options = options ? options.dup : {}\n        options[:namespace] ||= 'jsonapi-serializer'\n\n        fieldset_key = fieldset.join('_')\n\n        # Use a fixed-length fieldset key if the current length is more than\n        # the length of a SHA1 digest\n        if fieldset_key.length > 40\n          fieldset_key = Digest::SHA1.hexdigest(fieldset_key)\n        end\n\n        options[:namespace] = \"#{options[:namespace]}-fieldset:#{fieldset_key}\"\n        options\n      end\n      # rubocop:enable Lint/UnusedMethodArgument\n\n      def id_from_record(record, params)\n        return FastJsonapi.call_proc(record_id, record, params) if record_id.is_a?(Proc)\n        return record.send(record_id) if record_id\n        raise MandatoryField, 'id is a mandatory field in the jsonapi spec' unless record.respond_to?(:id)\n\n        record.id\n      end\n\n      # It chops out the root association (first part) from each include.\n      #\n      # It keeps an unique list and collects all of the rest of the include\n      # value to hand it off to the next related to include serializer.\n      #\n      # This method will turn that include array into a Hash that looks like:\n      #\n      #   {\n      #       authors: Set.new([\n      #         'books',\n      #         'books.genre',\n      #         'books.genre.books',\n      #         'books.genre.books.authors',\n      #         'books.genre.books.genre'\n      #       ]),\n      #       genre: Set.new(['books'])\n      #   }\n      #\n      # Because the serializer only cares about the root associations\n      # included, it only needs the first segment of each include\n      # (for books, it's the \"authors\" and \"genre\") and it doesn't need to\n      # waste cycles parsing the rest of the include value. That will be done\n      # by the next serializer in line.\n      #\n      # @param includes_list [List] to be parsed\n      # @return [Hash]\n      def parse_includes_list(includes_list)\n        includes_list.each_with_object({}) do |include_item, include_sets|\n          include_base, include_remainder = include_item.to_s.split('.', 2)\n          include_sets[include_base.to_sym] ||= Set.new\n          include_sets[include_base.to_sym] << include_remainder if include_remainder\n        end\n      end\n\n      # includes handler\n      def get_included_records(record, includes_list, known_included_objects, fieldsets, params = {})\n        return unless includes_list.present?\n        return [] unless relationships_to_serialize\n\n        includes_list = parse_includes_list(includes_list)\n\n        includes_list.each_with_object([]) do |include_item, included_records|\n          relationship_item = relationships_to_serialize[include_item.first]\n\n          next unless relationship_item&.include_relationship?(record, params)\n\n          included_objects = Array(relationship_item.fetch_associated_object(record, params))\n          next if included_objects.empty?\n\n          static_serializer = relationship_item.static_serializer\n          static_record_type = relationship_item.static_record_type\n\n          included_objects.each do |inc_obj|\n            serializer = static_serializer || relationship_item.serializer_for(inc_obj, params)\n            record_type = static_record_type || serializer.record_type\n\n            if include_item.last.any?\n              serializer_records = serializer.get_included_records(inc_obj, include_item.last, known_included_objects, fieldsets, params)\n              included_records.concat(serializer_records) unless serializer_records.empty?\n            end\n\n            code = \"#{record_type}_#{serializer.id_from_record(inc_obj, params)}\"\n            next if known_included_objects.include?(code)\n\n            known_included_objects << code\n\n            included_records << serializer.record_hash(inc_obj, fieldsets[record_type], includes_list, params)\n          end\n        end\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/fast_jsonapi/version.rb",
    "content": "module FastJsonapi\n  VERSION = JSONAPI::Serializer::VERSION\nend\n"
  },
  {
    "path": "lib/fast_jsonapi.rb",
    "content": "# frozen_string_literal: true\n\nrequire 'jsonapi/serializer/errors'\n\nmodule FastJsonapi\n  require 'fast_jsonapi/object_serializer'\n  if defined?(::Rails)\n    require 'fast_jsonapi/railtie'\n  elsif defined?(::ActiveRecord)\n    require 'extensions/has_one'\n  end\nend\n"
  },
  {
    "path": "lib/generators/serializer/USAGE",
    "content": "Description:\n    Generates a serializer for the given model.\n\nExample:\n    rails generate serializer Movie\n\n    This will create:\n        app/serializers/movie_serializer.rb\n"
  },
  {
    "path": "lib/generators/serializer/serializer_generator.rb",
    "content": "# frozen_string_literal: true\n\nrequire 'rails/generators/base'\n\nclass SerializerGenerator < Rails::Generators::NamedBase\n  source_root File.expand_path('templates', __dir__)\n\n  argument :attributes, type: :array, default: [], banner: 'field field'\n\n  def create_serializer_file\n    template 'serializer.rb.tt', File.join('app', 'serializers', class_path, \"#{file_name}_serializer.rb\")\n  end\n\n  private\n\n  def attributes_names\n    attributes.map { |a| a.name.to_sym.inspect }\n  end\nend\n"
  },
  {
    "path": "lib/generators/serializer/templates/serializer.rb.tt",
    "content": "<% module_namespacing do -%>\nclass <%= class_name %>Serializer\n  include JSONAPI::Serializer\n  attributes <%= attributes_names.join(\", \") %>\nend\n<% end -%>\n"
  },
  {
    "path": "lib/jsonapi/serializer/errors.rb",
    "content": "# frozen_string_literal: true\n\nmodule JSONAPI\n  module Serializer\n    class Error < StandardError; end\n\n    class UnsupportedIncludeError < Error\n      attr_reader :include_item, :klass\n\n      def initialize(include_item, klass)\n        super()\n        @include_item = include_item\n        @klass = klass\n      end\n\n      def message\n        \"#{include_item} is not specified as a relationship on #{klass}\"\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/jsonapi/serializer/instrumentation.rb",
    "content": "require 'active_support'\nrequire 'active_support/notifications'\n\nmodule JSONAPI\n  module Serializer\n    # Support for instrumentation\n    module Instrumentation\n      # Performance instrumentation namespace\n      NOTIFICATION_NAMESPACE = 'render.jsonapi-serializer.'.freeze\n\n      # Patch methods to use instrumentation...\n      %w[\n        serializable_hash\n        get_included_records\n        relationships_hash\n      ].each do |method_name|\n        define_method(method_name) do |*args|\n          ActiveSupport::Notifications.instrument(\n            NOTIFICATION_NAMESPACE + method_name,\n            { name: self.class.name, serializer: self.class }\n          ) do\n            super(*args)\n          end\n        end\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/jsonapi/serializer/version.rb",
    "content": "module JSONAPI\n  module Serializer\n    VERSION = '2.2.0'.freeze\n  end\nend\n"
  },
  {
    "path": "lib/jsonapi/serializer.rb",
    "content": "require 'fast_jsonapi'\n\nmodule JSONAPI\n  module Serializer\n    # TODO: Move and cleanup the old implementation...\n    def self.included(base)\n      base.class_eval do\n        include FastJsonapi::ObjectSerializer\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "spec/fixtures/_user.rb",
    "content": "require 'active_support'\nrequire 'active_support/cache'\n\nclass User\n  attr_accessor :uid, :first_name, :last_name, :email\n\n  def self.fake(id = nil)\n    faked = new\n    faked.uid = id || SecureRandom.uuid\n    faked.first_name = FFaker::Name.first_name\n    faked.last_name = FFaker::Name.last_name\n    faked.email = FFaker::Internet.email\n    faked\n  end\nend\n\nclass NoSerializerUser < User\nend\n\nclass UserSerializer\n  include JSONAPI::Serializer\n\n  set_id :uid\n  attributes :first_name, :last_name, :email\n\n  meta do |obj|\n    {\n      email_length: obj.email.size\n    }\n  end\nend\n\nmodule Cached\n  class UserSerializer < ::UserSerializer\n    cache_options(\n      store: ActiveSupport::Cache::MemoryStore.new,\n      namespace: 'test'\n    )\n  end\nend\n"
  },
  {
    "path": "spec/fixtures/actor.rb",
    "content": "require 'active_support'\nrequire 'active_support/cache'\nrequire 'jsonapi/serializer/instrumentation'\n\nclass Actor < User\n  attr_accessor :movies, :movie_ids\n\n  def self.fake(id = nil)\n    faked = super\n    faked.movies = []\n    faked.movie_ids = []\n    faked\n  end\n\n  def movie_urls\n    {\n      movie_url: movies[0]&.url\n    }\n  end\nend\n\nclass ActorSerializer < UserSerializer\n  set_type :actor\n\n  attribute :email, if: ->(_object, params) { params[:conditionals_off].nil? }\n\n  has_many(\n    :played_movies,\n    serializer: :movie,\n    links: :movie_urls,\n    if: ->(_object, params) { params[:conditionals_off].nil? }\n  ) do |object|\n    object.movies\n  end\nend\n\nclass CamelCaseActorSerializer\n  include JSONAPI::Serializer\n\n  set_key_transform :camel\n\n  set_id :uid\n  set_type :user_actor\n  attributes :first_name\n\n  link :movie_url do |obj|\n    obj.movie_urls.values[0]\n  end\n\n  has_many(\n    :played_movies,\n    serializer: :movie\n  ) do |object|\n    object.movies\n  end\nend\n\nclass BadMovieSerializerActorSerializer < ActorSerializer\n  has_many :played_movies, serializer: :bad, object_method_name: :movies\nend\n\nmodule Cached\n  class ActorSerializer < ::ActorSerializer\n    # TODO: Fix this, the serializer gets cached on inherited classes...\n    has_many :played_movies, serializer: :movie do |object|\n      object.movies\n    end\n\n    cache_options(\n      store: ActiveSupport::Cache::MemoryStore.new,\n      namespace: 'test'\n    )\n  end\nend\n\nmodule Instrumented\n  class ActorSerializer < ::ActorSerializer\n    include ::JSONAPI::Serializer::Instrumentation\n  end\nend\n"
  },
  {
    "path": "spec/fixtures/movie.rb",
    "content": "class Movie\n  attr_accessor(\n    :id,\n    :name,\n    :year,\n    :actor_or_user,\n    :actors,\n    :actor_ids,\n    :polymorphics,\n    :owner,\n    :owner_id\n  )\n\n  def self.fake(id = nil)\n    faked = new\n    faked.id = id || SecureRandom.uuid\n    faked.name = FFaker::Movie.title\n    faked.year = FFaker::Vehicle.year\n    faked.actors = []\n    faked.actor_ids = []\n    faked.polymorphics = []\n    faked\n  end\n\n  def url(obj = nil)\n    @url ||= FFaker::Internet.http_url\n    return @url if obj.nil?\n\n    \"#{@url}?#{obj.hash}\"\n  end\n\n  def owner=(ownr)\n    @owner = ownr\n    @owner_id = ownr.uid\n  end\n\n  def actors=(acts)\n    @actors = acts\n    @actor_ids = actors.map do |actor|\n      actor.movies << self\n      actor.uid\n    end\n  end\nend\n\nclass MovieSerializer\n  include JSONAPI::Serializer\n\n  set_type :movie\n\n  attribute :released_in_year, &:year\n  attributes :name\n  attribute :release_year do |object, _params|\n    object.year\n  end\n\n  link :self, :url\n\n  belongs_to :owner, serializer: UserSerializer\n\n  belongs_to :actor_or_user,\n             id_method_name: :uid,\n             polymorphic: {\n               Actor => :actor,\n               User => :user\n             }\n\n  has_many(\n    :actors,\n    meta: proc { |record, _| { count: record.actors.length } },\n    links: {\n      actors_self: :url,\n      related: ->(obj) { obj.url(obj) }\n    }\n  )\n  has_one(\n    :creator,\n    object_method_name: :owner,\n    id_method_name: :uid,\n    serializer: ->(object, _params) { UserSerializer if object.is_a?(User) }\n  )\n  has_many(\n    :actors_and_users,\n    id_method_name: :uid,\n    polymorphic: {\n      Actor => :actor,\n      User => :user\n    }\n  ) do |obj|\n    obj.polymorphics\n  end\n\n  has_many(\n    :dynamic_actors_and_users,\n    id_method_name: :uid,\n    polymorphic: true\n  ) do |obj|\n    obj.polymorphics\n  end\n\n  has_many(\n    :auto_detected_actors_and_users,\n    id_method_name: :uid\n  ) do |obj|\n    obj.polymorphics\n  end\nend\n\nmodule Cached\n  class MovieSerializer < ::MovieSerializer\n    cache_options(\n      store: ActorSerializer.cache_store_instance,\n      namespace: 'test'\n    )\n\n    has_one(\n      :creator,\n      id_method_name: :uid,\n      serializer: :actor,\n      # TODO: Remove this undocumented option.\n      #   Delegate the caching to the serializer exclusively.\n      cached: false\n    ) do |obj|\n      obj.owner\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/attributes_fields_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:actor) do\n    act = Actor.fake\n    act.movies = [Movie.fake]\n    act\n  end\n  let(:params) { {} }\n  let(:serialized) do\n    ActorSerializer.new(actor, params).serializable_hash.as_json\n  end\n\n  describe 'attributes' do\n    it do\n      expect(serialized['data']).to have_id(actor.uid)\n      expect(serialized['data']).to have_type('actor')\n\n      expect(serialized['data'])\n        .to have_jsonapi_attributes('first_name', 'last_name', 'email').exactly\n      expect(serialized['data']).to have_attribute('first_name')\n        .with_value(actor.first_name)\n      expect(serialized['data']).to have_attribute('last_name')\n        .with_value(actor.last_name)\n      expect(serialized['data']).to have_attribute('email')\n        .with_value(actor.email)\n    end\n\n    context 'with nil identifier' do\n      before { actor.uid = nil }\n\n      it { expect(serialized['data']).to have_id(nil) }\n    end\n\n    context 'with `if` conditions' do\n      let(:params) { { params: { conditionals_off: 'yes' } } }\n\n      it do\n        expect(serialized['data']).not_to have_attribute('email')\n      end\n    end\n\n    context 'with include and fields' do\n      let(:params) do\n        {\n          include: [:played_movies],\n          fields: { movie: [:release_year], actor: [:first_name] }\n        }\n      end\n\n      it do\n        expect(serialized['data'])\n          .to have_jsonapi_attributes(:first_name).exactly\n\n        expect(serialized['included']).to include(\n          have_type('movie')\n          .and(have_id(actor.movies[0].id))\n          .and(have_jsonapi_attributes('release_year').exactly)\n        )\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/caching_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:actor) do\n    faked = Actor.fake\n    movie = Movie.fake\n    movie.owner = User.fake\n    movie.actors = [faked]\n    faked.movies = [movie]\n    faked\n  end\n  let(:cache_store) { Cached::ActorSerializer.cache_store_instance }\n\n  describe 'with caching' do\n    it do\n      expect(cache_store.delete(actor, namespace: 'test')).to be(false)\n\n      Cached::ActorSerializer.new(\n        [actor, actor], include: ['played_movies', 'played_movies.owner']\n      ).serializable_hash\n\n      expect(cache_store.delete(actor, namespace: 'test')).to be(true)\n      expect(cache_store.delete(actor.movies[0], namespace: 'test')).to be(true)\n      expect(\n        cache_store.delete(actor.movies[0].owner, namespace: 'test')\n      ).to be(false)\n    end\n\n    context 'without relationships' do\n      let(:user) { User.fake }\n\n      let(:serialized) { Cached::UserSerializer.new(user).serializable_hash.as_json }\n\n      it do\n        expect(serialized['data']).not_to have_key('relationships')\n      end\n    end\n  end\n\n  describe 'with caching and different fieldsets' do\n    context 'when fieldset is provided' do\n      it 'includes the fieldset in the namespace' do\n        expect(cache_store.delete(actor, namespace: 'test')).to be(false)\n\n        Cached::ActorSerializer.new(\n          [actor], fields: { actor: %i[first_name] }\n        ).serializable_hash\n\n        # Expect cached keys to match the passed fieldset\n        expect(cache_store.read(actor, namespace: 'test-fieldset:first_name')[:attributes].keys).to eq(%i[first_name])\n\n        Cached::ActorSerializer.new(\n          [actor]\n        ).serializable_hash\n\n        # Expect cached keys to match all valid actor fields (no fieldset)\n        expect(cache_store.read(actor, namespace: 'test')[:attributes].keys).to eq(%i[first_name last_name email])\n        expect(cache_store.delete(actor, namespace: 'test')).to be(true)\n        expect(cache_store.delete(actor, namespace: 'test-fieldset:first_name')).to be(true)\n      end\n    end\n\n    context 'when long fieldset is provided' do\n      let(:actor_keys) { %i[first_name last_name more_fields yet_more_fields so_very_many_fields] }\n      let(:digest_key) { Digest::SHA1.hexdigest(actor_keys.join('_')) }\n\n      it 'includes the hashed fieldset in the namespace' do\n        Cached::ActorSerializer.new(\n          [actor], fields: { actor: actor_keys }\n        ).serializable_hash\n\n        expect(cache_store.read(actor, namespace: \"test-fieldset:#{digest_key}\")[:attributes].keys).to eq(\n          %i[first_name last_name]\n        )\n\n        expect(cache_store.delete(actor, namespace: \"test-fieldset:#{digest_key}\")).to be(true)\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/errors_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:actor) { Actor.fake }\n  let(:params) { {} }\n\n  describe 'with errors' do\n    it do\n      expect do\n        BadMovieSerializerActorSerializer.new(\n          actor, include: ['played_movies']\n        )\n      end.to raise_error(\n        NameError, /cannot resolve a serializer class for 'bad'/\n      )\n    end\n\n    it do\n      expect { ActorSerializer.new(actor, include: ['bad_include']) }\n        .to raise_error(\n          JSONAPI::Serializer::UnsupportedIncludeError, /bad_include is not specified as a relationship/\n        )\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/instrumentation_spec.rb",
    "content": "require 'spec_helper'\n\n# Needed to subscribe to `active_support/notifications`\nrequire 'concurrent'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:serializer) do\n    Instrumented::ActorSerializer.new(Actor.fake)\n  end\n\n  it do\n    payload = event_name = nil\n    notification_name =\n      \"#{JSONAPI::Serializer::Instrumentation::NOTIFICATION_NAMESPACE}serializable_hash\"\n\n    ActiveSupport::Notifications.subscribe(\n      notification_name\n    ) do |ev_name, _s, _f, _i, ev_payload|\n      event_name = ev_name\n      payload = ev_payload\n    end\n\n    expect(serializer.serializable_hash).not_to be_nil\n\n    expect(event_name).to eq('render.jsonapi-serializer.serializable_hash')\n    expect(payload[:name]).to eq(serializer.class.name)\n    expect(payload[:serializer]).to eq(serializer.class)\n  end\nend\n"
  },
  {
    "path": "spec/integration/key_transform_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:actor) { Actor.fake }\n  let(:params) { {} }\n  let(:serialized) do\n    CamelCaseActorSerializer.new(actor, params).serializable_hash.as_json\n  end\n\n  describe 'camel case key tranformation' do\n    it do\n      expect(serialized['data']).to have_id(actor.uid)\n      expect(serialized['data']).to have_type('UserActor')\n      expect(serialized['data']).to have_attribute('FirstName')\n      expect(serialized['data']).to have_relationship('PlayedMovies')\n      expect(serialized['data']).to have_link('MovieUrl').with_value(nil)\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/links_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:movie) do\n    faked = Movie.fake\n    faked.actors = [Actor.fake]\n    faked\n  end\n  let(:params) { {} }\n  let(:serialized) do\n    MovieSerializer.new(movie, params).serializable_hash.as_json\n  end\n\n  describe 'links' do\n    it do\n      expect(serialized['data']).to have_link('self').with_value(movie.url)\n      expect(serialized['data']['relationships']['actors'])\n        .to have_link('actors_self').with_value(movie.url)\n      expect(serialized['data']['relationships']['actors'])\n        .to have_link('related').with_value(movie.url(movie))\n    end\n\n    context 'with included records' do\n      let(:serialized) do\n        ActorSerializer.new(movie.actors[0]).serializable_hash.as_json\n      end\n\n      it do\n        expect(serialized['data']['relationships']['played_movies'])\n          .to have_link('movie_url').with_value(movie.url)\n      end\n    end\n\n    context 'with root link' do\n      let(:params) do\n        {\n          links: { 'root_link' => FFaker::Internet.http_url }\n        }\n      end\n\n      it do\n        expect(serialized)\n          .to have_link('root_link').with_value(params[:links]['root_link'])\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/meta_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:user) { User.fake }\n  let(:params) { {} }\n  let(:serialized) do\n    UserSerializer.new(user, params).serializable_hash.as_json\n  end\n\n  it do\n    expect(serialized['data']).to have_meta('email_length' => user.email.size)\n  end\n\n  context 'with root meta' do\n    let(:params) do\n      {\n        meta: { 'code' => FFaker::Internet.password }\n      }\n    end\n\n    it do\n      expect(serialized).to have_meta(params[:meta])\n    end\n  end\nend\n"
  },
  {
    "path": "spec/integration/relationships_spec.rb",
    "content": "require 'spec_helper'\n\nRSpec.describe JSONAPI::Serializer do\n  let(:movie) do\n    mov = Movie.fake\n    mov.actors = rand(2..5).times.map { Actor.fake }\n    mov.owner = User.fake\n    poly_act = Actor.fake\n    poly_act.movies = [Movie.fake]\n    mov.polymorphics = [User.fake, poly_act]\n    mov.actor_or_user = Actor.fake\n    mov\n  end\n  let(:params) { {} }\n  let(:serialized) do\n    MovieSerializer.new(movie, params).serializable_hash.as_json\n  end\n\n  describe 'relationships' do\n    it do\n      actors_rel = movie.actors.map { |a| { 'id' => a.uid, 'type' => 'actor' } }\n\n      expect(serialized['data'])\n        .to have_relationship('actors').with_data(actors_rel)\n\n      expect(serialized['data'])\n        .to have_relationship('owner')\n        .with_data('id' => movie.owner.uid, 'type' => 'user')\n\n      expect(serialized['data'])\n        .to have_relationship('creator')\n        .with_data('id' => movie.owner.uid, 'type' => 'user')\n\n      expect(serialized['data'])\n        .to have_relationship('actors_and_users')\n        .with_data(\n          [\n            { 'id' => movie.polymorphics[0].uid, 'type' => 'user' },\n            { 'id' => movie.polymorphics[1].uid, 'type' => 'actor' }\n          ]\n        )\n\n      expect(serialized['data'])\n        .to have_relationship('dynamic_actors_and_users')\n        .with_data(\n          [\n            { 'id' => movie.polymorphics[0].uid, 'type' => 'user' },\n            { 'id' => movie.polymorphics[1].uid, 'type' => 'actor' }\n          ]\n        )\n\n      expect(serialized['data'])\n        .to have_relationship('auto_detected_actors_and_users')\n        .with_data(\n          [\n            { 'id' => movie.polymorphics[0].uid, 'type' => 'user' },\n            { 'id' => movie.polymorphics[1].uid, 'type' => 'actor' }\n          ]\n        )\n    end\n\n    describe 'has relationship meta' do\n      it do\n        expect(serialized['data']['relationships']['actors'])\n          .to have_meta('count' => movie.actors.length)\n      end\n    end\n\n    context 'with include' do\n      let(:params) do\n        { include: [:actors] }\n      end\n\n      it do\n        movie.actors.each do |actor|\n          expect(serialized['included']).to include(\n            have_type('actor')\n            .and(have_id(actor.uid))\n            .and(have_relationship('played_movies')\n            .with_data([{ 'id' => actor.movies[0].id, 'type' => 'movie' }]))\n          )\n        end\n      end\n\n      context 'with `if` conditions' do\n        let(:params) do\n          {\n            include: ['actors'],\n            params: { conditionals_off: 'yes' }\n          }\n        end\n\n        it do\n          movie.actors.each do |actor|\n            expect(serialized['included']).not_to include(\n              have_type('actor')\n              .and(have_id(actor.uid))\n              .and(have_relationship('played_movies'))\n            )\n          end\n        end\n      end\n\n      context 'with has_many polymorphic' do\n        let(:params) do\n          { include: ['actors_and_users.played_movies'] }\n        end\n\n        it do\n          expect(serialized['included']).to include(\n            have_type('user').and(have_id(movie.polymorphics[0].uid))\n          )\n\n          expect(serialized['included']).to include(\n            have_type('movie').and(have_id(movie.polymorphics[1].movies[0].id))\n          )\n\n          expect(serialized['included']).to include(\n            have_type('actor')\n            .and(have_id(movie.polymorphics[1].uid))\n            .and(\n              have_relationship('played_movies').with_data(\n                [{\n                  'id' => movie.polymorphics[1].movies[0].id,\n                  'type' => 'movie'\n                }]\n              )\n            )\n          )\n        end\n      end\n\n      context 'with belongs_to polymorphic' do\n        let(:params) do\n          { include: ['actor_or_user'] }\n        end\n\n        it do\n          expect(serialized['included']).to include(\n            have_type('actor').and(have_id(movie.actor_or_user.uid))\n          )\n        end\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "spec/spec_helper.rb",
    "content": "unless RUBY_ENGINE == 'truffleruby'\n  require 'simplecov'\n  SimpleCov.start do\n    add_group 'Lib', 'lib'\n    add_group 'Tests', 'spec'\n  end\n  SimpleCov.minimum_coverage 90\nend\n\nrequire 'active_support'\nrequire 'active_support/core_ext/object'\nrequire 'active_support/core_ext/object/json'\nrequire 'jsonapi/serializer'\nrequire 'ffaker'\nrequire 'rspec'\nrequire 'jsonapi/rspec'\nrequire 'byebug'\nrequire 'securerandom'\n\nDir[File.expand_path('spec/fixtures/*.rb')].sort.each { |f| require f }\n\nRSpec.configure do |config|\n  config.include JSONAPI::RSpec\n\n  config.mock_with :rspec\n  config.filter_run_when_matching :focus\n  config.disable_monkey_patching!\n\n  config.expect_with :rspec do |c|\n    c.syntax = :expect\n  end\nend\n"
  }
]