Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error rendering XML when render_nil is set to true and value is nil. #150

Open
konung opened this issue Jun 25, 2015 · 2 comments
Open

Error rendering XML when render_nil is set to true and value is nil. #150

konung opened this issue Jun 25, 2015 · 2 comments

Comments

@konung
Copy link

konung commented Jun 25, 2015

Code:

class ProductRepresenter < Roar::Decorator
  include Roar::JSON
  include Roar::XML
  self.representation_wrap= nil

  Product.attribute_names.each do |attr|
    property attr.to_sym,  render_nil: true 
  end

 # This works fine  - cause this is a required attribute
  property :manufacturer, class: Manufacturer, decorator: ManufacturerRepresenter, render_nil: true

  # For the product in Question - this  :distribution_restriction is nil ( can be set to NULL if there is no restriction) so it causes a problem when trying to render xml
  property :distribution_restriction, class: DistributionRestriction, decorator: RestrictionRepresenter, render_nil: true

end

here is the error

ArgumentError: Requires a Node, NodeSet or String argument, and cannot accept a NilClass.
(You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
from /home/vagrant/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/nokogiri-.6.6.2/lib/nokogiri/xml/node.rb:803:in `coerce'
@apotonick
Copy link
Member

Please try this with separate representers (one for XML, only) and give me a complete test setup so I have a chance to understand what you're doing (No test, just some pseudo code is ok).

BTW: You don't need to to_sym property names, strings are fine.
BTW2: Sorry to disappoint you but XML support in Representable is very very low on my very very full priority list, so don't get angry at me if things take a bit longer. ❤️

@apotonick apotonick reopened this Jun 25, 2015
@konung
Copy link
Author

konung commented Jun 26, 2015

let me simplify a bit - so I don't get into how I implemented restrictions - as this bug happens whenever an associated Representer doesn't have anything to render - in this case manufacturer_id is nil and therefore associated manufacture is also nil - and I say render it as blank
( I want to supply a full set of attributes regardless of the value they are holding or not holding :) on each api call)

class ProductRepresenter < Roar::Decorator
  include Roar::XML
  self.representation_wrap= nil

  Product.attribute_names.each do |attr|
    property attr,  render_nil: true
  end

  property :manufacturer, class: Manufacturer, decorator: ManufacturerRepresenter, render_nil: true
end

class ManufacturerRepresenter < Roar::Decorator
  include Roar::XML
  self.representation_wrap= nil

  property :id
  property :name
  property :code
end


class Product < ActiveRecord::Base
  belongs_to :manufacturer, inverse_of: :products
end


class Manufacturer < ActiveRecord::Base
  has_many :products, inverse_of: :manufacturer
end


class ProductsController < ApplicationController
  include Roar::Rails::ControllerAdditions
  %w(json xml).each do |response|
    represents response.to_sym, Product
  end
  respond_to :html, :json, :xml

  def show
    product = Product.include_all.find_by_id(params[:id])
    #include_all is a scope that eager loads all related associations 
    #( I don't want to call it using default scope - as that slows down
    # all other lookups that don't need this level of detail)
    respond_with product
  end

end

So when I call http://api.dev/products/1.xml and product doesn't have a manufacturer for instance - than I get that error above.

THis only happens in XML, not in JSON -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants