lib/cocina/generator/schema_base.rb in cocina-models-0.58.1 vs lib/cocina/generator/schema_base.rb in cocina-models-0.58.2
- old
+ new
@@ -2,16 +2,17 @@
module Cocina
module Generator
# Base class for generating from openapi
class SchemaBase
- attr_reader :schema_doc, :key, :required, :parent
+ attr_reader :schema_doc, :key, :required, :nullable, :parent
- def initialize(schema_doc, key: nil, required: false, parent: nil)
+ def initialize(schema_doc, key: nil, required: false, nullable: false, parent: nil)
@schema_doc = schema_doc
@key = key
@required = required
+ @nullable = nullable
@parent = parent
end
def filename
"#{name.underscore}.rb"
@@ -27,15 +28,13 @@
return '' if required
'.meta(omittable: true)'
end
- # Allows non-required values to be set to nil. This is useful when doing
+ # Allows nillable values to be set to nil. This is useful when doing
# an update and you want to clear out a value.
def optional
- return '' if required
-
- '.optional'
+ nullable ? '.optional' : ''
end
def quote(item)
return item unless schema_doc.type == 'string'