Skip to content

Deserialization excludes hash attribute contents #2219

Open
@coreypmurphy

Description

@coreypmurphy

Expected behavior vs actual behavior

Given a model with an attribute that is treated as a hash of options, I'm expecting AMS to deserialize the JSON payload so I can persist the "some-options" attribute (re-serialized as YAML by the ActiveRecord::Base "serialize" method) to the db as it did in [email protected].

But ActiveModelSerializers::Deserialization.jsonapi_parse() is excluding the contents of "some-options" during deserialization. Other attributes are deserialized as expected.

Steps to reproduce

POSTed JSON payload:

{
  "data": {
    "attributes": {
      "name": "John",
      "some-options": {
	"key1": "value 1",
	"key2": "value 2"
      }
    },
    "type": "courses"
  }
}

Model:

class Course < ApplicationRecord
  validates :name, presence: true
  serialize :some_options 
end

Controller:

class Api::CoursesController < ApplicationController
  def create
    course = Course.new(course_params)
    if course.save
      render json: course
    else
      render json: course.errors, status: :unprocessable_entity
    end
  end

  def course_params
    ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [
      :name, 
      some_options: option_keys
    ])
  end

  def option_keys
    params.dig('data','attributes').fetch('some-options', {}).keys
  end
end

config/initializers/json_api.rb

ActiveSupport.on_load(:action_controller) do
  require 'active_model_serializers/register_jsonapi_renderer'
end

ActiveModelSerializers.config.adapter = :json_api
ActiveModelSerializers.config.key_transform = :unaltered

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.6

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-darwin16]

OS Type & Version: OSX 10.12.4

Integrated application and version (e.g., Rails, Grape, etc): [email protected]

For reference this is a migration to [email protected] and JSONAPI from [email protected]. Our Ember frontend was using "ActiveModelAdapter", so we had Ember dealing with key transforms and serialization on requests coming to Rails and our strong params implementation wasn't being "deserialized" as they are in [email protected].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions