Sha256: 79270dec4f8f32da88448ce4d36b16cd4773e7cbc80df692679f7dd8072a1977

Contents?: true

Size: 1.81 KB

Versions: 21

Compression:

Stored size: 1.81 KB

Contents

# TODO: links

module OpenAPIParser::Schemas
  class Response < Base
    include OpenAPIParser::MediaTypeSelectable

    openapi_attr_values :description

    # @!attribute [r] content
    #   @return [Hash{String => MediaType}, nil] content_type to MediaType hash
    openapi_attr_hash_object :content, MediaType, reference: false

    # @!attribute [r] headers
    #   @return [Hash{String => Header}, nil] header string to Header
    openapi_attr_hash_object :headers, Header, reference: true

    # @param [OpenAPIParser::RequestOperation::ValidatableResponseBody] response_body
    # @param [OpenAPIParser::SchemaValidator::ResponseValidateOptions] response_validate_options
    def validate(response_body, response_validate_options)
      validate_header(response_body.headers) if response_validate_options.validate_header

      media_type = select_media_type(response_body.content_type)
      unless media_type
        raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference if response_validate_options.strict

        return nil
      end

      options = ::OpenAPIParser::SchemaValidator::Options.new # response validator not support any options
      media_type.validate_parameter(response_body.response_data, options)
    end

    # select media type by content_type (consider wild card definition)
    # @param [String] content_type
    # @return [OpenAPIParser::Schemas::MediaType, nil]
    def select_media_type(content_type)
      select_media_type_from_content(content_type, content)
    end

    private

      # @param [Hash] response_headers
      def validate_header(response_headers)
        return unless headers

        headers.each do |name, schema|
          next unless response_headers.key?(name)

          value = response_headers[name]
          schema.validate(value)
        end
      end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

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