Sha256: 60a77e69a945e5302222ceb297096f2c5b31562fab2971c563a97641ce51b7c8

Contents?: true

Size: 1.91 KB

Versions: 24

Compression:

Stored size: 1.91 KB

Contents

# TODO: support extended property

module OpenAPIParser::Schemas
  class Responses < Base
    # @!attribute [r] default
    #   @return [Response, Reference, nil] default response object
    openapi_attr_object :default, Response, reference: true

    # @!attribute [r] response
    #   @return [Hash{String => Response, Reference}, nil] response object indexed by status code. see: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#patterned-fields-1
    openapi_attr_hash_body_objects 'response', Response, reject_keys: [:default], reference: true, allow_data_type: false

    # validate params data by definition
    # find response object by status_code and content_type
    # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#patterned-fields-1
    # @param [OpenAPIParser::RequestOperation::ValidatableResponseBody] response_body
    # @param [OpenAPIParser::SchemaValidator::ResponseValidateOptions] response_validate_options
    def validate(response_body, response_validate_options)
      return nil unless response

      if (res = find_response_object(response_body.status_code))

        return res.validate(response_body, response_validate_options)
      end

      raise ::OpenAPIParser::NotExistStatusCodeDefinition, object_reference if response_validate_options.strict

      nil
    end

    private

      # @param [Integer] status_code
      # @return [Response]
      def find_response_object(status_code)
        if (res = response[status_code.to_s])
          return res
        end

        wild_card = status_code_to_wild_card(status_code)
        if (res = response[wild_card])
          return res
        end

        default
      end

      # parse 400 -> 4xx
      # OpenAPI3 allow 1xx, 2xx, 3xx... only, don't allow 41x
      # @param [Integer] status_code
      def status_code_to_wild_card(status_code)
        top = status_code / 100
        "#{top}XX"
      end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
openapi_parser-2.2.2 lib/openapi_parser/schemas/responses.rb
openapi_parser-2.2.1 lib/openapi_parser/schemas/responses.rb
openapi_parser-2.2.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-2.1.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-2.0.0 lib/openapi_parser/schemas/responses.rb
openapi_parser_firetail-1.0.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-1.0.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-1.0.0.beta1 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.15.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.14.1 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.14.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.13.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.12.1 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.12.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.11.2 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.11.1 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.11.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.10.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.9.0 lib/openapi_parser/schemas/responses.rb
openapi_parser-0.8.0 lib/openapi_parser/schemas/responses.rb