Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into rename_schema_valid…
Browse files Browse the repository at this point in the history
…ators
  • Loading branch information
ota42y committed Jan 28, 2023
2 parents c6a71c8 + c9de79f commit 599d0f9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ target :lib do
check "lib/openapi_parser/schema_validator/options.rb"
check "lib/openapi_parser/schema_validator/base.rb"

library 'uri'
library 'uri', 'json', 'pathname'
end
8 changes: 6 additions & 2 deletions lib/openapi_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ def load(filepath, config = {})
def load_uri(uri, config:, schema_registry:)
# Open-uri doesn't open file scheme uri, so we try to open file path directly
# File scheme uri which points to a remote file is not supported.
uri_path = uri.path
raise "file not found" if uri_path.nil?

content = if uri.scheme == 'file'
open(uri.path)&.read
open(uri_path)&.read
elsif uri.is_a?(OpenURI::OpenRead)
uri.open()&.read
end

extension = Pathname.new(uri.path).extname
extension = Pathname.new(uri_path).extname
load_hash(parse_file(content, extension), config: config, uri: uri, schema_registry: schema_registry)
end

Expand Down Expand Up @@ -85,6 +88,7 @@ def parse_yaml(content)
end

def parse_json(content)
raise "json content is nil" unless content
JSON.parse(content)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/openapi_parser/schema_validator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def coerce_and_validate(_value, _schema, **_keyword_args)

def validate_discriminator_schema(discriminator, value, parent_discriminator_schemas: [])
property_name = discriminator.property_name
unless (property_name && value.key?(property_name))
if property_name.nil? || !value.key?(property_name)
return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
end
mapping_key = value[property_name]
Expand Down
2 changes: 2 additions & 0 deletions sig/openapi_parser/config.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
module OpenAPIParser
class Config
@config: untyped
@request_validator_options: OpenAPIParser::SchemaValidator::Options
@response_validate_options: OpenAPIParser::SchemaValidator::ResponseValidateOptions
alias request_body_options request_validator_options
alias path_params_options request_validator_options

Expand Down
22 changes: 22 additions & 0 deletions sig/openapi_parser/errors.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module OpenAPIParser
class OpenAPIError < StandardError
def initialize: (untyped reference) -> untyped
end

class ValidateError < OpenAPIError
def initialize: (untyped data, (String | nil) type, untyped reference) -> untyped
def message: -> String

def self.build_error_result: (Object value, OpenAPIParser::Schemas::Schema schema) -> [nil, OpenAPIParser::ValidateError]
end

class NotExistDiscriminatorMappedSchema < OpenAPIError
def initialize: (untyped mapped_schema_reference, untyped reference) -> untyped
def message: -> String
end

class NotExistDiscriminatorPropertyName < OpenAPIError
def initialize: (untyped mapped_schema_reference, untyped value, untyped reference) -> untyped
def message: -> String
end
end
3 changes: 3 additions & 0 deletions sig/openapi_parser/reference_expander.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class OpenAPIParser::ReferenceExpander
def self.expand: (OpenAPIParser::Schemas::OpenAPI openapi, untyped validate_references) -> nil
end
7 changes: 4 additions & 3 deletions sig/openapi_parser/schema_validators/base.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ module OpenAPIParser
class Base
@coerce_value: bool | nil

def initialize: (OpenAPIParser::SchemaValidator::Validatable validatable, (bool | nil) coerce_value) -> untyped
attr_reader validatable: OpenAPIParser::SchemaValidator::Validatable
def coerce_and_validate: (Object _value, OpenAPIParser::Schemas::Schema _schema, **untyped) -> bot

def initialize: (OpenAPIParser::SchemaValidator::Validatable validatable, (bool | nil) coerce_value) -> untyped
def coerce_and_validate: (Object _value, OpenAPIParser::Schemas::Schema _schema, **untyped) -> [untyped, (ValidateError | NotExistDiscriminatorMappedSchema | nil)]
def validate_discriminator_schema: (
OpenAPIParser::Schemas::Discriminator discriminator,
Hash[String, bot] value,
?parent_discriminator_schemas: Array[OpenAPIParser::Schemas::Schema]
) -> [Object | nil, OpenAPIParser::validate_error]
) -> [Object | nil, OpenAPIParser::OpenAPIError]
end
end
end
5 changes: 5 additions & 0 deletions sig/types.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ module OpenURI
end
end


module Psych
def self.safe_load: (untyped content, untyped parmitted_classes) -> Hash[bot, bot]
end

3 changes: 0 additions & 3 deletions sig/wip_types.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,3 @@ class OpenAPIParser::Schemas::Discriminator < OpenAPIParser::Schemas::Base
attr_reader property_name: (String | nil)
attr_reader mapping: Hash[String, String]
end

class OpenAPIParser::OpenAPIError
end

0 comments on commit 599d0f9

Please sign in to comment.