Skip to content

Commit

Permalink
Put file loading code inside FileLoader module
Browse files Browse the repository at this point in the history
  • Loading branch information
ahx committed Dec 6, 2024
1 parent 9c2aaaa commit c162c0a
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 148 deletions.
4 changes: 2 additions & 2 deletions lib/openapi_first.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'yaml'
require 'multi_json'
require_relative 'openapi_first/refs'
require_relative 'openapi_first/file_loader'
require_relative 'openapi_first/errors'
require_relative 'openapi_first/configuration'
require_relative 'openapi_first/definition'
Expand Down Expand Up @@ -53,7 +53,7 @@ def self.find_error_response(name)
def self.load(filepath, only: nil, &)
raise FileNotFoundError, "File not found: #{filepath}" unless File.exist?(filepath)

contents = Refs.load_file(filepath)
contents = FileLoader.load(filepath)
parse(contents, only:, filepath:, &)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/openapi_first/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.build_router(contents, filepath:, config:)

def initialize(contents, filepath:, config:)
ref_resolver = JSONSchemer::CachedResolver.new do |uri|
Refs.load_file(File.join(File.dirname(filepath), uri.path))
FileLoader.load(File.join(File.dirname(filepath), uri.path))
end
configuration = JSONSchemer::Configuration.new(
ref_resolver:,
Expand Down
20 changes: 20 additions & 0 deletions lib/openapi_first/file_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module OpenapiFirst
# Functions to handle $refs
# @!visibility private
module FileLoader
module_function

def load(file_path)
raise FileNotFoundError, "File not found #{file_path}" unless File.exist?(file_path)

body = File.read(file_path)
extname = File.extname(file_path)
return JSON.parse(body) if extname == '.json'
return YAML.unsafe_load(body) if ['.yaml', '.yml'].include?(extname)

body
end
end
end
61 changes: 0 additions & 61 deletions lib/openapi_first/refs.rb

This file was deleted.

12 changes: 7 additions & 5 deletions lib/openapi_first/resolved.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module OpenapiFirst
# This is here to give easy access to resolved $refs
# @visibility private
Expand All @@ -15,10 +17,8 @@ def [](key)
self.class.new(value, context:)
end

def each
resolved.each do |key, value|
yield key, value
end
def each(&)
resolved.each(&)
end

def resolved
Expand All @@ -27,9 +27,11 @@ def resolved
elsif value.is_a?(Array)
return value.map do |item|
break item.resolved if item.is_a?(Resolved)

item
end
end

value
end

Expand All @@ -38,10 +40,10 @@ def resolved
private attr_accessor :value
private attr_accessor :context


def resolve_ref(pointer)
value = Hana::Pointer.new(pointer[1..]).eval(context)
raise "Unknown reference #{pointer} in #{context}" unless value

value
end
end
Expand Down
94 changes: 94 additions & 0 deletions spec/data/petstore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
components:
schemas:
Pets:
type: array
title: Pets
items:
$ref: "#/components/schemas/Pet"
Pet:
$ref: "./components/schemas/pet.yaml#/Pet"
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
24 changes: 24 additions & 0 deletions spec/file_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

RSpec.describe OpenapiFirst::FileLoader do
describe '.load' do
it 'loads .json' do
contents = described_class.load('./spec/data/petstore.json')
expect(contents['openapi']).to eq('3.0.0')
end

it 'loads .yaml' do
contents = described_class.load('./spec/data/petstore.yaml')
expect(contents['openapi']).to eq('3.0.0')
end

it 'loads .yml' do
contents = described_class.load('./spec/data/petstore.yml')
expect(contents['openapi']).to eq('3.0.0')
end

it 'raises FileNotFoundError if file was not found' do
expect { described_class.load('./spec/data/unknown.yaml') }.to raise_error(OpenapiFirst::FileNotFoundError, 'File not found ./spec/data/unknown.yaml')
end
end
end
78 changes: 0 additions & 78 deletions spec/refs_spec.rb

This file was deleted.

3 changes: 2 additions & 1 deletion spec/resolved_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../lib/openapi_first/resolved'
# frozen_string_literal: true

require_relative '../lib/openapi_first/resolved'

RSpec.describe OpenapiFirst::Resolved do
let(:original_hash) do
Expand Down

0 comments on commit c162c0a

Please sign in to comment.