Class: GrapeOAS::Introspectors::DryIntrospectorSupport::InheritanceHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/introspectors/dry_introspector_support/inheritance_handler.rb

Overview

Handles contract inheritance detection and allOf schema building.

Instance Method Summary collapse

Constructor Details

#initialize(contract_resolver, stack:, registry:) ⇒ InheritanceHandler

Returns a new instance of InheritanceHandler.



8
9
10
11
12
# File 'lib/grape_oas/introspectors/dry_introspector_support/inheritance_handler.rb', line 8

def initialize(contract_resolver, stack:, registry:)
  @contract_resolver = contract_resolver
  @stack = stack
  @registry = registry
end

Instance Method Details

#build_inherited_schema(parent_contract, type_schema_builder) ⇒ ApiModel::Schema

Builds an inherited schema using allOf composition.

Parameters:

  • parent_contract (Class)

    the parent contract class

  • type_schema_builder (TypeSchemaBuilder)

    builder for type schemas

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/grape_oas/introspectors/dry_introspector_support/inheritance_handler.rb', line 39

def build_inherited_schema(parent_contract, type_schema_builder)
  # Build parent schema first
  parent_schema = DryIntrospector.new(parent_contract, stack: @stack, registry: @registry).build

  # Build child-only properties
  child_schema = build_child_only_schema(parent_contract, type_schema_builder)

  # Create allOf schema
  schema = ApiModel::Schema.new(
    canonical_name: @contract_resolver.contract_class.name,
    all_of: [parent_schema, child_schema],
  )

  @registry[@contract_resolver.contract_class] = schema
  schema
end

#find_parent_contractClass?

Finds parent contract class if this contract inherits from another.

Returns:

  • (Class, nil)

    the parent contract class or nil



17
18
19
20
21
22
23
24
25
# File 'lib/grape_oas/introspectors/dry_introspector_support/inheritance_handler.rb', line 17

def find_parent_contract
  return nil unless defined?(Dry::Validation::Contract)

  parent = @contract_resolver.contract_class.superclass
  return nil unless parent && parent < Dry::Validation::Contract && parent != Dry::Validation::Contract
  return nil unless parent.respond_to?(:schema)

  parent
end

#inherited?Boolean

Checks if the contract has a parent contract.

Returns:

  • (Boolean)

    true if inherited



30
31
32
# File 'lib/grape_oas/introspectors/dry_introspector_support/inheritance_handler.rb', line 30

def inherited?
  !find_parent_contract.nil?
end

#parent_contract_types(parent_contract) ⇒ Array<String>

Gets type keys from parent contract.

Parameters:

  • parent_contract (Class)

    the parent contract class

Returns:

  • (Array<String>)

    list of parent type keys



60
61
62
63
64
# File 'lib/grape_oas/introspectors/dry_introspector_support/inheritance_handler.rb', line 60

def parent_contract_types(parent_contract)
  return [] unless parent_contract.respond_to?(:schema)

  parent_contract.schema.types.keys.map(&:to_s)
end