Skip to content

Jbuilder ignore_nil, key_format, and deep_format_keys not working (Rails 7) #548

@shetty-tejas

Description

@shetty-tejas

File Structure:

config/initializers/jbuilder.rb

Jbuilder.ignore_nil
Jbuilder.key_format camelize: :lower
Jbuilder.deep_format_keys

config/routes.rb

  namespace 'api', defaults: { format: :json } do
    namespace 'v1' do
      resource 'post', only: [:create]
    end
  end

app/views/layouts/application.json.jbuilder

json.success @result.success?
json.merge! yield

app/views/api/v1/posts/create.json.jbuilder

json.data @model

Expected Result:

{
  success: true,
  data: {
    id: 1,
    subject: 'Test',
    content: 'Testing Content',
    createdAt: 'some timestamp'
  }
}

Actual Result:

{
  data: {
    id: 1,
    subject: 'Test',
    content: 'Testing Content',
    created_at: 'some timestamp',
    updated_at: nil
  }
}

I don't think I've set up the initializer wrong as inside the create.json.jbuilder, I used a debugger and checked that calling json, I can see that ignore_nil and other specified options have been set correctly. Yet the actual behaviour is not according to the expectations.

P.S: It's also not loading the success attribute from application.json.jbuilder that I had mentioned.

Any help regarding this would be nice 😄

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