Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ['2.7', '3.0', '3.1']
include:
- ruby: '2.7'
- ruby: '3.0'
- ruby: '3.1'
- ruby: '3.2'
- ruby: '3.3'
- ruby: '3.4'

steps:
- uses: actions/checkout@v4
Expand Down
17 changes: 15 additions & 2 deletions lib/jsonapi/swagger/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@ def initialize(path = 'swagger/v1/swagger.json')
end

def parse_doc
@doc ||= JSON.parse(load) rescue Hash.new{ |h, k| h[k]= {} }
@doc ||= begin
parsed = JSON.parse(load)

doc = Hash.new { |h, k| h[k] = {} }
doc.merge!(parsed) if parsed.is_a?(Hash)

if doc.key?('paths') && !doc['paths'].is_a?(Hash)
doc['paths'] = {}
end

doc
rescue StandardError
Hash.new { |h, k| h[k] = {} }
end
end

def base_path
Expand All @@ -28,4 +41,4 @@ def load

end
end
end
end
38 changes: 30 additions & 8 deletions spec/integration/swagger_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class NamedBase
class << self
attr_accessor :_source_root, :_destination_root

def desc(*) = nil
def desc(*)
nil
end

def source_root(path = nil)
self._source_root = path if path
Expand Down Expand Up @@ -77,13 +79,33 @@ def template(src, dest)
relation.define_singleton_method(:belongs_to?) { true }

resource = Class.new(JSONAPI::Resource) do
def self._attributes = { id: nil, name: nil }
def self._relationships = { company: OpenStruct.new(class_name: 'Company', table_name: 'companies') }
def self.sortable_fields = [:name]
def self.creatable_fields = [:name]
def self.updatable_fields = [:name]
def self.filters = { name: {} }
def self.mutable? = true
def self._attributes
{ id: nil, name: nil }
end

def self._relationships
{ company: OpenStruct.new(class_name: 'Company', table_name: 'companies') }
end

def self.sortable_fields
[:name]
end

def self.creatable_fields
[:name]
end

def self.updatable_fields
[:name]
end

def self.filters
{ name: {} }
end

def self.mutable?
true
end
end

# Ensure relationship object has belongs_to? and table_name.
Expand Down
62 changes: 49 additions & 13 deletions spec/jsonapi/swagger/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,33 @@

it 'wraps JSONAPI::Resource subclasses' do
klass = Class.new(JSONAPI::Resource) do
def self._attributes = { id: {}, name: {} }
def self._relationships = {}
def self.sortable_fields = []
def self.creatable_fields = []
def self.updatable_fields = []
def self.filters = {}
def self.mutable? = false
def self._attributes
{ id: {}, name: {} }
end

def self._relationships
{}
end

def self.sortable_fields
[]
end

def self.creatable_fields
[]
end

def self.updatable_fields
[]
end

def self.filters
{}
end

def self.mutable?
false
end
end
stub_const('UserResource', klass)

Expand All @@ -31,10 +51,21 @@ def self.mutable? = false

it 'wraps JSONAPI::Serializable::Resource subclasses' do
klass = Class.new(JSONAPI::Serializable::Resource) do
def self.type_val = :users
def self.attribute_blocks = { id: nil, name: nil }
def self.relationship_blocks = { company: nil }
def self.link_blocks = {}
def self.type_val
:users
end

def self.attribute_blocks
{ id: nil, name: nil }
end

def self.relationship_blocks
{ company: nil }
end

def self.link_blocks
{}
end
end
stub_const('SerializableUser', klass)

Expand All @@ -46,8 +77,13 @@ def self.link_blocks = {}

it 'wraps FastJsonapi::ObjectSerializer subclasses' do
klass = Class.new(FastJsonapi::ObjectSerializer) do
def self.attributes_to_serialize = { id: {}, name: {} }
def self.relationships_to_serialize = {}
def self.attributes_to_serialize
{ id: {}, name: {} }
end

def self.relationships_to_serialize
{}
end
end
stub_const('UserSerializer', klass)

Expand Down
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'json'

require 'logger'
require 'active_support'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/object/blank'
Expand All @@ -14,6 +15,24 @@
require 'jsonapi/swagger'

RSpec.configure do |config|
config.around do |example|
swagger = Jsonapi::Swagger
ivars = %i[
@version
@info
@file_path
@base_path
@use_rswag
@attribute_default
]

saved = ivars.to_h { |ivar| [ivar, swagger.instance_variable_get(ivar)] }

example.run
ensure
saved.each { |ivar, value| swagger.instance_variable_set(ivar, value) }
end

config.disable_monkey_patching!
config.order = :random
Kernel.srand config.seed
Expand Down