interagent / interagent/committee

Always getting invalid response error for hash with Rails and jbuilder

Open
#304 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
955
Forks
145
Avg merge
5d 4h
Merged PRs (30d)
6

Description

I am setting up the middleware like this in my config/application.rb;

if Rails.env.development? || Rails.env.test?
  config.middleware.use Committee::Middleware::RequestValidation, 
	schema_path: 'docs/schema-bundled.yml',
	params_key: 'action_dispatch.request.request_parameters'
  config.middleware.use Committee::Middleware::ResponseValidation, 
	schema_path: 'docs/schema-bundled.yml',
	raise: true
end

Now whenever I render anything from my controllers, like;

def doorkeeper_forbidden_render_options(*)
  { json: { error: { code: 'forbidden', message: 'This action is outside the authorized scopes' } } }
end

I always receive a Committee:InvalidResponse error;

Committee::InvalidResponse: #/components/responses/forbidden/content/application~1json/schema expected , but received Hash: {"error"=>{"code"=>"forbidden", "message"=>"This action is outside the authorized scopes"}}

Where the schema definitions looks like this;

openapi: 3.0.0

paths:
  '/api/v1/accounts/{account_id}':
    get:
      operationId: get_account
      summary: Retrieve an existing acocunt
      description: >
        To show information about an account, send a GET request to
        `/api/v1/accounts/$ACCOUNT_ID`.
      tags:
        - Accounts
      parameters:
        - $ref: '#/components/parameters/account_id'
      responses:
        '401':
          $ref: '#/components/responses/unauthorized'

components:
  parameters:
    account_id:
      in: path
      name: account_id
      description: A unique identifier for an account.
      required: true
      schema:
        type: string
        format: uuid
      example: acf19357-d974-4843-9525-e98426ad5346
  
  schemas:
    error:
      type: object
      properties:
        code:
          type: string
          description: >
            A short identifier corresponding to the HTTP status code returned.
            For example, the identifier for a response returning a 404 status code
            would be "not_found."
          example: not_found
        message:
          type: string
          description: >
            A message providing additional information about the error,
            including details to help resolve it when possible.
          example: The resource you were accessing could not be found.
        request_id:
          type: string
          format: uuid
          description: |
            Optionally, some endpoints may include a request ID that should be
            provided when reporting bugs or opening support tickets to help
            identify the issue.
          example: 4d9d8375-3c56-4925-a3e7-eb137fed17e9
      required:
        - code
        - message

  responses:
    forbidden:
      description: Forbidden.
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                $ref: '#/components/schemas/error'
          example:
            error:
              code: forbidden
              message: Forbidden to authenticate you.

Am I missing maybe some configuration value?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the middleware configuration in config/application.rb and the supplied OpenAPI schema, then trace Committee::Middleware::ResponseValidation for the rendered controller response. Reproduce the doorkeeper_forbidden_render_options case and compare the response body with the schema; done means the documented Rails and Jbuilder response validates without Committee::InvalidResponse.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.