Skip to content

Commit

Permalink
Merge pull request #762 from sul-dlss/drop-openapi3-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Jan 8, 2025
2 parents fc8229a + adac040 commit 0a11330
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 23 deletions.
7 changes: 0 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
cocina-models (0.99.2)
activesupport
commonmarker (~> 2.0, != 2.0.2)
deprecation
dry-struct (~> 1.0)
dry-types (~> 1.1)
Expand All @@ -12,7 +11,6 @@ PATH
i18n
jsonpath
nokogiri
openapi3_parser
openapi_parser (~> 1.0)
super_diff
thor
Expand Down Expand Up @@ -44,8 +42,6 @@ GEM
json_schema (~> 0.14, >= 0.14.3)
openapi_parser (~> 1.0)
rack (>= 1.5)
commonmarker (2.0.1)
rb_sys (~> 0.9)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
deprecation (1.1.0)
Expand Down Expand Up @@ -94,8 +90,6 @@ GEM
nokogiri (1.18.1)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
openapi3_parser (0.10.0)
commonmarker (>= 1.0)
openapi_parser (1.0.0)
optimist (3.2.0)
parallel (1.26.3)
Expand All @@ -108,7 +102,6 @@ GEM
rack (3.1.8)
rainbow (3.1.1)
rake (13.2.1)
rb_sys (0.9.105)
regexp_parser (2.10.0)
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down
6 changes: 0 additions & 6 deletions cocina-models.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'activesupport'
if RUBY_VERSION >= '3.4' # rubocop:disable Gemspec/RubyVersionGlobalsUsage
spec.add_dependency 'commonmarker', '>= 2.0.2' # commonmarker <= 2.0.1 is incompatible with Ruby 3.4
else
spec.add_dependency 'commonmarker', '~> 2.0', '!= 2.0.2' # commonmarker 2.0.2 includes a breaking change in Rubies < 3.4
end
spec.add_dependency 'deprecation'
spec.add_dependency 'dry-struct', '~> 1.0'
spec.add_dependency 'dry-types', '~> 1.1'
Expand All @@ -38,7 +33,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'i18n' # for validating BCP 47 language tags, according to RFC 4646
spec.add_dependency 'jsonpath' # used for date/time validation
spec.add_dependency 'nokogiri'
spec.add_dependency 'openapi3_parser' # Parsing openapi doc
# Match these version requirements to what committee wants,
# so that our client (non-committee) users have the same dependencies.
spec.add_dependency 'openapi_parser', '~> 1.0'
Expand Down
4 changes: 3 additions & 1 deletion lib/cocina/generator/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def types_markdown_from(types)
end

def schemas
@schemas ||= Openapi3Parser.load_file(options[:openapi]).components.schemas
@schemas ||= OpenAPIParser.parse(YAML.load_file(options[:openapi]), strict_reference_validation: true)
.find_object('#/components')
.schemas
end

def schema_for(schema_name, lite: false)
Expand Down
8 changes: 4 additions & 4 deletions lib/cocina/generator/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def validate
end

def validatable?
!schema_doc.node_context.document.paths["/validate/#{schema_doc.name}"].nil? && !lite
!schema_doc.root.paths.path["/validate/#{schema_doc.name}"].nil? && !lite
end

def properties
Expand Down Expand Up @@ -118,13 +118,13 @@ def one_of_properties_for(doc)
end

def schema_properties_for(doc)
doc.properties.map do |key, properties_doc|
Array(doc.properties).map do |key, properties_doc|
clazz = property_class_for(properties_doc)
clazz.new(properties_doc,
key: key,
required: doc.requires?(key),
required: doc.required&.include?(key),
relaxed: lite && clazz != SchemaValue,
nullable: properties_doc.nullable?,
nullable: properties_doc.nullable,
parent: self,
schemas: schemas)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/cocina/generator/schema_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ module Cocina
module Generator
# Class for generating from an openapi array
class SchemaArray < SchemaBase
GENERIC_ITEMS_NAME = 'items'

def generate
"attribute :#{name.camelize(:lower)}, Types::Strict::Array.of(#{array_of_type}).default([].freeze)"
end

def array_of_type
schema_doc.items.name || dry_datatype(schema_doc.items)
items_name = schema_doc.items.name
return items_name unless items_name == GENERIC_ITEMS_NAME

dry_datatype(schema_doc.items)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cocina/generator/schema_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def description
end

def deprecation
return '' unless schema_doc.deprecated?
return '' unless schema_doc.deprecated

"# DEPRECATED\n"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cocina/generator/vocab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class #{namespace} < Vocabulary('#{URIS.fetch(namespace)}')
}.freeze

def vocabs
type_properties = schemas.values.map { |schema| schema.properties['type'] }.compact
type_properties = schemas.values.map { |schema| schema.properties&.[]('type') }.compact
type_properties.map(&:enum).flat_map(&:to_a)
.filter { |vocab| vocab.start_with?(BASE) }
.uniq
Expand Down
17 changes: 15 additions & 2 deletions lib/cocina/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
require 'json'
require 'yaml'
require 'openapi_parser'
require 'openapi3_parser'
require 'active_support'
require 'active_support/core_ext'
require 'thor'
Expand Down Expand Up @@ -40,6 +39,17 @@ def camelize(basename, _abspath)
loader.ignore("#{__dir__}/rspec")
loader.setup

module OpenAPIParser
module Schemas
# Patch OpenAPIParser::Schemas::Schema so it can tell us its name, the way Openapi3Parser schemas do.
class Schema
def name
object_reference.split('/').last
end
end
end
end

module Cocina
# Provides Ruby objects for the repository and serializing them to/from JSON.
module Models
Expand Down Expand Up @@ -191,7 +201,10 @@ def self.has_metadata?(dyn)

def self.druid_regex
@druid_regex ||= begin
str = Openapi3Parser.load_file('openapi.yml').components.schemas['Druid'].pattern
str = OpenAPIParser.parse(YAML.load_file('openapi.yml'), strict_reference_validation: true)
.find_object('#/components')
.schemas['Druid']
.pattern
Regexp.new(str)
end
end
Expand Down

0 comments on commit 0a11330

Please sign in to comment.