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

Subclasses keep the parent's .documentation? #121

Open
liclac opened this issue Apr 10, 2015 · 4 comments
Open

Subclasses keep the parent's .documentation? #121

liclac opened this issue Apr 10, 2015 · 4 comments
Labels

Comments

@liclac
Copy link

liclac commented Apr 10, 2015

I'm not sure if I'm using it wrong, or if it's bugged, but if I do this:

# api/entities/my_entity.rb
module API
  module Entities
    class MyEntity < Grape::Entity
      expose :name, documentation: { type: String, required: true }
      expose :message, documentation: { type: String }

      expose :created_at, documentation: { type: DateTime, required: true }
      expose :updated_at, documentation: { type: DateTime, required: true }

      expose :relation, using: API::Entities::Other, documentation: { is_array: true }

      def self.entity_name() "MyEntity" end

      class Update < API::Entities::MyEntity
        unexpose :created_at
        unexpose :updated_at
        unexpose :relation
      end
    end
  end
end

# api/entities/other.rb
module API
  module Entities
    class Other < Grape::Entity
      expose :created_at, documentation: { type: DateTime, required: true }
      expose :updated_at, documentation: { type: DateTime, required: true }
    end
  end
end

The results aren't quite what you'd expect in regards to .documentation:

$ rails console
Loading development environment (Rails 4.2.1)
irb(main):001:0> API::Entities::MyEntity.documentation
=> {:name=>{:type=>String, :required=>true}, :message=>{:type=>String}, :created_at=>{:type=>DateTime, :required=>true}, :updated_at=>{:type=>DateTime, :required=>true}, :relation=>{:is_array=>true}}
irb(main):002:0> API::Entities::MyEntity::Update.documentation
=> {:name=>{:type=>String, :required=>true}, :message=>{:type=>String}, :created_at=>{:type=>DateTime, :required=>true}, :updated_at=>{:type=>DateTime, :required=>true}, :relation=>{:is_array=>true}}

As a result, it currently appears to be impossible to use Entities for parameter reuse, without resorting to copypasting expose … lines into a separate class - making unexpose rather useless.

As a side note, it doesn't appear to be unexpose itself that's broken, as expose in a subclass similarly has no effect.

@dan-corneanu
Copy link
Contributor

Can you please describe what result are you expected from the above setup?

@liclac
Copy link
Author

liclac commented Apr 11, 2015

I would expect unexpose'd attributes in the subclass to be absent from the subclass' documentation, and vice versa for newly expose'd.

@dan-corneanu
Copy link
Contributor

Yes, this is what I would expect too. I think this is a bug, the problem is most probably in the implementation of Grape::Entity#documentation which calls #documentation for the superclass too and merges with the subclass' documentation.

Can someone more knowledgeable confirm that this is indeed a bug? I would be more than happy to add a failing test and a fix for it.

@dblock
Copy link
Member

dblock commented Apr 11, 2015

Yes, looks like a bug to me.

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

No branches or pull requests

3 participants