-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenAPI POC #17
Closed
Closed
OpenAPI POC #17
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
588f382
fix(rack): ensure OPTIONS preflight requests are handled
paulsturgess 512e3ad
WIP
paulsturgess eb5b33c
partial responses
paulsturgess 6a8b33f
polymorphs and other bits
paulsturgess 0788a7e
convert_type_to_openapi_data_type v3.1.0
paulsturgess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ gem 'solargraph' | |
|
||
gem 'rake' | ||
gem 'rubocop' | ||
|
||
gem 'pry' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module CoreAPI | ||
module ArgumentSets | ||
class ObjectLookup < Apia::LookupArgumentSet | ||
|
||
name 'Object Lookup' | ||
description 'Provides for objects to be looked up' | ||
|
||
argument :id, type: :string | ||
argument :permalink, type: :string | ||
|
||
potential_error 'ObjectNotFound' do | ||
code :object_not_found | ||
description 'No object was found matching any of the criteria provided in the arguments' | ||
http_status 404 | ||
end | ||
|
||
resolver do |set, request, scope| | ||
objects = [{id: "123", permalink: "perma-123"}] | ||
object = objects.find { |o| o[:id] == set[:id] || o[:permalink] == set[:permalink] } | ||
raise_error 'ObjectNotFound' if object.nil? | ||
|
||
object | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'core_api/argument_sets/object_lookup' | ||
|
||
module CoreAPI | ||
module Endpoints | ||
class TestEndpoint < Apia::Endpoint | ||
|
||
description 'Returns the current time' | ||
argument :object, type: ArgumentSets::ObjectLookup, required: true | ||
field :time, type: Objects::Time, include: 'unix,day_of_week,year[as_string]', null: true do | ||
condition do |o| | ||
o[:time].year.to_s == '2023' | ||
end | ||
end | ||
field :object_id, type: :string do | ||
backend { |o| o[:object_id][:id] } | ||
end | ||
scope 'time' | ||
|
||
def call | ||
object = request.arguments[:object].resolve | ||
response.add_field :time, get_time_now | ||
response.add_field :object_id, object | ||
end | ||
|
||
private | ||
|
||
def get_time_now | ||
Time.now | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module CoreAPI | ||
module Objects | ||
class MonthLong < Apia::Object | ||
|
||
description 'Represents a month' | ||
|
||
field :month_long, type: :string do | ||
backend { |t| t.strftime('%B') } | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'core_api/objects/month_long' | ||
require 'core_api/objects/month_short' | ||
|
||
module CoreAPI | ||
module Objects | ||
class MonthPolymorph < Apia::Polymorph | ||
|
||
option 'MonthLong', type: CoreAPI::Objects::MonthLong, matcher: proc { |time| time.sec.even? } | ||
option 'MonthShort', type: CoreAPI::Objects::MonthShort, matcher: proc { |time| time.sec.odd? } | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module CoreAPI | ||
module Objects | ||
class MonthShort < Apia::Object | ||
|
||
description 'Represents a month' | ||
|
||
field :month_short, type: :string do | ||
backend { |t| t.strftime('%b') } | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'core_api/objects/day' | ||
require 'core_api/objects/year' | ||
require 'core_api/objects/month_polymorph' | ||
|
||
module CoreAPI | ||
module Objects | ||
class Time < Apia::Object | ||
|
||
description 'Represents a time' | ||
|
||
field :unix, type: :integer do | ||
backend(&:to_i) | ||
field :unix, type: :unix_time do | ||
backend { |t| t } | ||
end | ||
|
||
field :day_of_week, type: Objects::Day do | ||
backend { |t| t.strftime('%A') } | ||
end | ||
|
||
field :month, type: :string do | ||
backend { |t| t.strftime('%b') } | ||
end | ||
|
||
field :full, type: :string do | ||
backend { |t| t.to_s } | ||
end | ||
|
||
field :year, type: Objects::Year do | ||
backend { |t| t.year } | ||
end | ||
|
||
field :month, type: Objects::MonthPolymorph do | ||
backend { |t| t } | ||
end | ||
|
||
field :as_array, type: [:integer] do | ||
backend { |t| [t.year, t.month, t.day, t.hour, t.min, t.sec] } | ||
end | ||
|
||
field :as_array_of_objects, type: [Objects::Year] do | ||
backend { |t| [t.year] } | ||
end | ||
|
||
field :as_decimal, type: :decimal do | ||
backend { |t| t.to_f } | ||
end | ||
|
||
field :as_base64, type: :base64 do | ||
backend { |t| Base64.encode64(t.to_s) } | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module CoreAPI | ||
module Objects | ||
class TimeZone < Apia::Enum | ||
|
||
value 'Europe/London' | ||
value 'Europe/Madrid' | ||
value 'Asia/Singapore' | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'core_api/objects/day' | ||
|
||
module CoreAPI | ||
module Objects | ||
class Year < Apia::Object | ||
|
||
description 'Represents a year' | ||
|
||
field :as_integer, type: :integer do | ||
backend(&:to_i) | ||
end | ||
|
||
field :as_string, type: :string do | ||
backend(&:to_s) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'apia-openapi/rack' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'apia-openapi/schema' | ||
|
||
module Apia | ||
module OpenAPI | ||
class Rack | ||
|
||
def initialize(app, api, path, **options) | ||
@app = app | ||
@api = api | ||
@path = "/#{path.sub(/\A\/+/, '').sub(/\/+\z/, '')}" | ||
@options = options | ||
end | ||
|
||
def development? | ||
env_is_dev = ENV['RACK_ENV'] == 'development' | ||
return true if env_is_dev && @options[:development].nil? | ||
|
||
@options[:development] == true | ||
end | ||
|
||
def api | ||
return Object.const_get(@api) if @api.is_a?(String) && development? | ||
return @cached_api ||= Object.const_get(@api) if @api.is_a?(String) | ||
|
||
@api | ||
end | ||
|
||
def base_url | ||
@options[:base_url] || 'https://api.example.com/api/v1' | ||
end | ||
|
||
def call(env) | ||
# if @options[:hosts]&.none? { |host| host == env['HTTP_HOST'] } | ||
# return @app.call(env) | ||
# end | ||
|
||
unless env['PATH_INFO'] == @path | ||
return @app.call(env) | ||
end | ||
|
||
schema = Schema.new(api, base_url) | ||
body = schema.json | ||
|
||
[200, { 'Content-Type' => 'application/json', 'Content-Length' => body.bytesize.to_s }, [body]] | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data