Skip to content

Add support for documentation fields #4

Closed

Description

@dblock

Grape exposures give you entity_name, exposures and documentation. We can add those for out-of-the-box support for https://github.com/tim-vandecasteele/grape-swagger.

module Grape
  module Roar
    module Representer
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def entity_name
          self.name.split('::').last.gsub(/Presenter$/, '')
        end

        def exposures
          {}
        end

        def documentation
          Hash[representable_attrs.map do |attribute|
            property_name = attribute[:as].evaluate nil, nil
            next if property_name == '_links'
            [ property_name, { desc: attribute[:desc], type: attribute[:type] }]
          end.compact]
        end
      end
    end
  end
end

Activity

  1. taybin commented on Jul 31, 2014

    @taybin
  2. dblock commented on Jul 31, 2014

    @dblock
    MemberAuthor

    @taybin Mostly, yes.

  3. danielkummer commented on Oct 10, 2014

    @danielkummer

    How can I use your code to document the swagger model - I can't really get it to work. The representer shows up in the grape documentation but both type and description are missing for the defined properties…

    Update: Got it! The documentation method has to look like this (the documentation values are stored under attribute[:documentation]):

    def documentation
        Hash[representable_attrs.map do |attribute|
            property_name = attribute[:as].evaluate nil, nil
            next if property_name == '_links'
            [property_name, {desc: attribute[:documentation][:desc], type: attribute[:documentation][:type]}]
        end.compact]
    end
    
  4. dblock commented on Oct 10, 2014

    @dblock
    MemberAuthor

    Would love it if someone turned this into production code :)

  5. sebweaver commented on Feb 11, 2016

    @sebweaver

    👍

  6. dblock commented on Oct 27, 2026

    @dblock
    MemberAuthor

    PR #47 documents the existing optional grape-swagger-representable parser rather than duplicating its integration. Use a Roar decorator as the response model and declare property metadata as documentation: { type: Integer, desc: "Product ID" }; require the parser before add_swagger_documentation.

    The exact README example was executed with Grape 4.0.1, grape-swagger 2.2.0, and grape-swagger-representable 0.2.2. The generated Swagger response reference, property types/descriptions, and actual rendered response match. The parser registers decorator classes, not representer modules; response metadata does not validate incoming parameters.

    Posted by Copilot, an AI, on behalf of @dblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions