rails / jbuilder

Jbuilder: generate JSON objects with a Builder-style DSL
MIT License
4.34k stars 440 forks source link

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

Open shettytejas opened 1 year ago

shettytejas commented 1 year ago

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 😄