docs: def _merge(obj, app, creator, parser):
      """ resolve $ref, and inject/merge referenced object to self.
      This operation should be carried in a cascade manner.
      """
      result = creator(NullContext())
      result.merge(obj, parser)
  
      guard = CycleGuard()
      guard.update(obj)
  
      r = getattr(obj, '$ref')
      while r and len(r) > 0:
          ro = app.resolve(r, parser)
          if ro.__class__ != obj.__class__:
              raise TypeError('Referenced Type mismatch: {0}'.format(r))
          try:
              guard.update(ro)
          except CycleDetectionError:
              # avoid infinite loop,
              # cycle detection has a dedicated scanner.
              break
  
          result.merge(ro, parser)
          r = getattr(ro, '$ref')
      return result