interagent / committee

A collection of Rack middleware to support JSON Schema.
MIT License
881 stars 135 forks source link

Add option to permit validation when schema and data are empty #417

Closed chibicco closed 5 months ago

chibicco commented 7 months ago

Background

In our project, there are scenarios where the API returns an empty response body with status code 200. However, the current OpenAPI 2 schema validation does not allow an empty schema for the response body, causing validation errors in these cases.

Specifically, in cases such as the schema and controller example below, I expect the Committee::Middleware::ResponseValidation check to pass successfully.

"/pets/cat": {
  "get": {
    "description": "Returns pets which are cats",
    "operationId": "find pets which are cats",
    "responses": {
      "200": {
        "description": "empty schema"
      }
    }
  }
}
class CatController
    # pets/cat
    get "/" do
        status 200
        present nil
    end
end

Changes

To address this issue, I have added an option to allow an empty schema for the response body in OpenAPI 2. This change enables the validation to pass when both the schema and response body are nil, accommodating the specific use case in our project.

  1. If allow_blank_structures is set to true, the validation will pass if the schema is empty and the data is nil.
  2. I have also modified the error message for the case where data is present and the schema does not exist.
chibicco commented 7 months ago

@brandur @ota42y

If you have a moment, I would appreciate it if you could review it. I apologize for my poor English skills, so please bear with me.

brandur commented 6 months ago

@chibicco Sorry for the delay. Do you still need this?

chibicco commented 6 months ago

@brandur Thank you for response :)

Yes, I need this pull requests. I have rebased on the latest master, If possible, please review.

chibicco commented 6 months ago

Greetings.

I have just noticed the following correction. https://github.com/interagent/committee/pull/420

If my PR is not appropriate, I would appreciate it if you could close it.

brandur commented 6 months ago

@chibicco Let's hold on that a bit longer. We have someone offering to help with maintenance so we may yet be able to rescue the project.

ydah commented 6 months ago

memo) I noticed in writing the following comment that 204 responses and so on expect to describe a response without a body. In other words, I think the next major update will change this parameters default value.

https://github.com/interagent/committee/pull/417#discussion_r1596566077

chibicco commented 5 months ago

@ydah

Thank you for your review! I have added the necessary commits for the corrections and replied to the comments. I kindly ask for your confirmation.

chibicco commented 5 months ago

@ydah I have updated the CHANGELOG.md :) We appreciate your kind support.

chibicco commented 5 months ago

@brandur Thank you for merging the PR. Could you kindly release the latest master at a time that is convenient for you?

brandur commented 5 months ago

@chibicco Yes! I believe @ydah is waiting for one more change on review feedback over on #396, after which he'll cut a release and get this shipped.

chibicco commented 5 months ago

I see, I understand !

ydah commented 5 months ago

@chibicco Thank you for waiting. Since v5.3.0 has been released, so your patch has been released! Thank you for your great work! https://rubygems.org/gems/committee/versions/5.3.0

chibicco commented 5 months ago

@ydah

I have confirmed the v5.3.0 release : ) Thank you!