Class: Meta::JsonSchema::RefSchema

Inherits:
BaseSchema show all
Defined in:
lib/meta/json_schema/schemas/ref_schema.rb

Instance Attribute Summary collapse

Attributes inherited from BaseSchema

#options

Instance Method Summary collapse

Methods inherited from BaseSchema

#filter?, #find_schema, #if?, #scoped, #staged, #to_schema, #value?

Constructor Details

#initialize(object_schema, options = {}) ⇒ RefSchema

Returns a new instance of RefSchema.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 10

def initialize(object_schema, options = {})
  raise ArgumentError, 'object_schema 必须是一个 ObjectSchema' unless object_schema.is_a?(ObjectSchema)

  super(options)
  @object_schema = object_schema
end

Instance Attribute Details

#object_schemaObject (readonly)

Returns the value of attribute object_schema.



8
9
10
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 8

def object_schema
  @object_schema
end

Instance Method Details

#defined_scopes(stage:, defined_scopes_mapping:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 40

def defined_scopes(stage:, defined_scopes_mapping:)
  defined_scopes_mapping ||= {}

  if object_schema.properties.respond_to?(:schema_name)
    # 只有命名实体才会被缓存
    schema_name = object_schema.properties.schema_name(stage)
    return defined_scopes_mapping[schema_name] if defined_scopes_mapping.key?(schema_name)
  end

  defined_scopes_mapping[schema_name] = []
  # 求解 defined_scopes,最终结果去重 + 排序
  defined_scopes = object_schema.properties.each.map do |name, property|
    property.defined_scopes(stage: stage, defined_scopes_mapping: defined_scopes_mapping)
  end.flatten.uniq.sort_by(&:name)
  defined_scopes_mapping[schema_name] = defined_scopes
  defined_scopes
end

#filter(value, user_options = {}) ⇒ Object



17
18
19
20
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 17

def filter(value, user_options = {})
  value = super
  object_schema.filter(value, user_options)
end

#to_schema_doc(user_options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 22

def to_schema_doc(user_options)
  raise '引用的 ObjectSchema 没有包含命名逻辑,无法生成文档' unless object_schema.naming?

  # 首先,要求出 defined_scopes
  defined_scopes = self.defined_scopes(stage: user_options[:stage], defined_scopes_mapping: user_options[:defined_scopes_mapping])
  # 然后,求出 schema_name
  schema_name = object_schema.resolve_name(user_options[:stage], user_options[:scope], defined_scopes)
  # 接着将 Schema 写进 schemas 选项中去
  schema_components = user_options[:schema_docs_mapping] || {}
  unless schema_components.key?(schema_name)
    schema_components[schema_name] = nil # 首先设置 schemas 防止出现无限循环
    schema_components[schema_name] = object_schema.to_schema_doc(**user_options) # 原地修改 schemas,无妨
  end

  # 最后,返回这个 $ref 结构
  { '$ref': "#/components/schemas/#{schema_name}" }
end