Class: OpenAPIGenerateTypeScriptFetch::ObjectSchema
- Inherits:
-
Object
- Object
- OpenAPIGenerateTypeScriptFetch::ObjectSchema
- Defined in:
- lib/openapi_generate_typescript_fetch/schema.rb
Overview
Common info about schema, used in multiple places so gathered here to shorten templates.
Instance Attribute Summary collapse
-
#additional ⇒ Object
readonly
Returns the value of attribute additional.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#unknown_names ⇒ Object
readonly
Returns the value of attribute unknown_names.
Instance Method Summary collapse
-
#initialize(schema) ⇒ ObjectSchema
constructor
A new instance of ObjectSchema.
Constructor Details
#initialize(schema) ⇒ ObjectSchema
Returns a new instance of ObjectSchema.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/openapi_generate_typescript_fetch/schema.rb', line 47 def initialize(schema) raise ArgumentError, "#{schema['type']} not an object" unless schema['type'] == 'object' @schema = schema @props = [] props = schema['properties'] || {} pat_props = schema['patternProperties'] || {} add_props = schema['additionalProperties'] reqd = schema['required'] || [] props.each do |name, spec| @props.push(ObjectSchemaProperty.new( name: name, req: reqd.include?(name), type: (Gen.h.category_and_name(spec) || [ spec['type'] ]).last, spec: spec )) end pat_props.each do |pattern, spec| @props.push(ObjectSchemaProperty.new( name: pattern, type: (Gen.h.category_and_name(spec) || [ spec['type'] ]).last, pattern: true, spec: spec )) end if add_props.is_a?(Hash) @additional = true @props.push(ObjectSchemaProperty.new( type: (Gen.h.category_and_name(add_props) || [ add_props['type'] ]).last, additional: true, spec: add_props )) elsif add_props.nil? || add_props == true @additional = true @props.push(ObjectSchemaProperty.new( type: nil, additional: true, spec: add_props )) else @additional = false end @props.sort! return if @additional if !pat_props.empty? # All patterns must have same failure cases. tps = Gen.x.cfg.dig(*%w[tests patterns]) return if tps.nil? cands = nil @props.select(&:pattern).each do |p| pt = tps.index { |pc| pc['pattern'] == p.name } return if pt.nil? fails = tps[pt]['fail'] return if fails.nil? cands = cands.nil? ? Set.new(fails) : cands & Set.new(fails) return if cands.empty? end cands -= Set.new(@props.reject(&:pattern).map(&:name)) @unknown_names = cands.empty? ? nil : cands.to_a.sort! elsif !props.empty? fixed = @props.reject(&:pattern).map(&:name).map(&:size) @unknown_names = [ 'a' * (fixed.max + 1) ] @unknown_names.push('a') if fixed.min > 1 else # No properties, pattern properties, no additional properties. @unknown_names = [ 'a' ] end end |
Instance Attribute Details
#additional ⇒ Object (readonly)
Returns the value of attribute additional.
46 47 48 |
# File 'lib/openapi_generate_typescript_fetch/schema.rb', line 46 def additional @additional end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
46 47 48 |
# File 'lib/openapi_generate_typescript_fetch/schema.rb', line 46 def props @props end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
46 47 48 |
# File 'lib/openapi_generate_typescript_fetch/schema.rb', line 46 def schema @schema end |
#unknown_names ⇒ Object (readonly)
Returns the value of attribute unknown_names.
46 47 48 |
# File 'lib/openapi_generate_typescript_fetch/schema.rb', line 46 def unknown_names @unknown_names end |