Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/jsi.multischema' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Jul 2, 2020
2 parents 93957b6 + 11610cb commit 9c060ce
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ gemspec
gem 'byebug'
gem 'coveralls_reborn', '~> 0.11'
gem 'wwtd'
gem 'ur', github: 'notEthan/ur', branch: 'jsi.multischema' # ur v0.2.0 awaiting release
gem 'jsi', github: 'notEthan/jsi', branch: 'multischema' # jsi v0.4.0 awaiting release
3 changes: 2 additions & 1 deletion lib/scorpio/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ def faraday_connection(yield_ur = nil)
faraday_builder.call(faraday_connection)
if yield_ur
::Ur::Faraday # autoload trigger
faraday_connection.response(:yield_ur, ur_class: Scorpio::Ur, logger: self.logger, &yield_ur)

faraday_connection.response(:yield_ur, schemas: Set[Scorpio::Ur.schema], logger: self.logger, &yield_ur)
end
faraday_connection.adapter(*faraday_adapter)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/scorpio/resource_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ def request_schema_fail(object, schema)

def response_object_to_instances(object, initialize_options = {})
if object.is_a?(JSI::Base)
model = models_by_schema[object.schema]
models = object.jsi_schemas.map { |schema| models_by_schema[schema] }
if models.size == 0
model = nil
elsif models.size == 1
model = models.first
else
raise(Scorpio::OpenAPI::Error, "multiple models indicated by response JSI. models: #{models.inspect}; jsi: #{jsi.pretty_inspect.chomp}")
end
end

if object.respond_to?(:to_hash)
Expand Down
4 changes: 3 additions & 1 deletion lib/scorpio/response.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Scorpio
class Response < ::Ur::Response
Response = Scorpio::Ur.properties['response']

module Response
# @return [::JSI::Schema] the schema for this response according to its OpenAPI doc
def response_schema
ur.scorpio_request.operation.response_schema(status: status, media_type: media_type)
Expand Down
27 changes: 12 additions & 15 deletions lib/scorpio/ur.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
module Scorpio
class Ur < ::Ur
# Scorpio::Ur is a JSI Schema module with which scorpio extends the ::Ur schema module
Ur = JSI::Schema.new({
'$id' => 'https://schemas.jsi.unth.net/ur',
'properties' => {
'request' => {},
'response' => {},
}
}).jsi_schema_module

-> { Scorpio::Response }.() # invoke autoload

module Ur
attr_accessor :scorpio_request

# raises a subclass of Scorpio::HTTPError if the response has an error status.
Expand Down Expand Up @@ -28,19 +39,5 @@ def raise_on_http_error
end
nil
end

private
# overrides JSI::Base#class_for_schema to use Scorpio::Response instead of ::Ur::Response.
# maybe a Scorpio::Ur::Request in the future if I need to extend that ... or Scorpio::Request
# if I decide to make that subclass ::Ur::Request. not sure if that's a good idea or a terrible
# idea.
def class_for_schema(schema)
jsi_class_for_schema = super
if jsi_class_for_schema == ::Ur::Response
Scorpio::Response
else
jsi_class_for_schema
end
end
end
end

0 comments on commit 9c060ce

Please sign in to comment.