dereference method

Schema dereference({
  1. required Map<String, Schema>? components,
})

Implementation

Schema dereference({
  required Map<String, Schema>? components,
}) {
  if (ref == null) {
    return this;
  }
  var sRef = components?[ref?.split('/').last];
  if (sRef == null) {
    throw Exception(
      "\n\n'$ref' is not a valid component schema body reference\n",
    );
  }

  final isMatch = _checkReferenceTypes(ref, sRef, this);

  if (!isMatch) {
    // This is an object with a custom type definition
    return Schema.object(ref: ref);
  }

  return map(
    object: (s) {
      // Handle List and Map defined as typedefs
      if (sRef is SchemaArray || sRef is SchemaMap) {
        return copyWith(
          title: s.title ?? sRef.title,
          description: s.description ?? sRef.description,
          nullable: s.nullable ?? sRef.nullable,
        );
      }

      return (sRef as SchemaObject).copyWith(
        ref: ref,
        title: s.title ?? sRef.title,
        description: s.description ?? sRef.description,
        defaultValue: s.defaultValue ?? sRef.defaultValue,
        nullable: s.nullable ?? sRef.nullable,
      );
    },
    boolean: (s) {
      return (sRef as SchemaBoolean).copyWith(ref: ref);
    },
    string: (s) {
      return (sRef as SchemaString).copyWith(ref: ref);
    },
    integer: (s) {
      return (sRef as SchemaInteger).copyWith(ref: ref);
    },
    number: (s) {
      return (sRef as SchemaNumber).copyWith(ref: ref);
    },
    enumeration: (s) {
      return (sRef as SchemaEnum).copyWith(
        ref: ref,
        title: s.title ?? sRef.title,
        description: s.description ?? sRef.description,
        defaultValue: s.defaultValue ?? sRef.defaultValue,
        example: s.example ?? sRef.example,
        nullable: s.nullable ?? sRef.nullable,
      );
    },
    array: (s) {
      return (sRef as SchemaArray).copyWith(ref: ref);
    },
    map: (s) {
      return (sRef as SchemaMap).copyWith(
        ref: ref,
      );
    },
  );
}