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);
  }

  late Schema result;
  switch (this) {
    case SchemaObject(
      title: final title,
      description: final description,
      nullable: final nullable,
      defaultValue: final defaultValue,
    ):
      // Handle List and Map defined as typedefs
      if (sRef is SchemaArray || sRef is SchemaMap) {
        result = copyWith(
          title: title ?? sRef.title,
          description: description ?? sRef.description,
          nullable: nullable ?? sRef.nullable,
        );
        break;
      }

      // Enums represented as objects
      if (sRef is SchemaEnum) {
        result = sRef.copyWith(
          title: title ?? sRef.title,
          description: description ?? sRef.description,
          defaultValue: defaultValue ?? sRef.defaultValue,
          nullable: nullable ?? sRef.nullable,
          ref: ref,
        );
        break;
      }

      result = (sRef as SchemaObject).copyWith(
        ref: ref,
        title: title ?? sRef.title,
        description: description ?? sRef.description,
        defaultValue: defaultValue ?? sRef.defaultValue,
        nullable: nullable ?? sRef.nullable,
      );
    case SchemaBoolean():
      result = (sRef as SchemaBoolean).copyWith(ref: ref);
    case SchemaString():
      result = (sRef as SchemaString).copyWith(ref: ref);
    case SchemaInteger():
      result = (sRef as SchemaInteger).copyWith(ref: ref);
    case SchemaNumber():
      result = (sRef as SchemaNumber).copyWith(ref: ref);
    case SchemaEnum(
      title: final title,
      description: final description,
      example: final example,
      defaultValue: final defaultValue,
      nullable: final nullable,
    ):
      result = (sRef as SchemaEnum).copyWith(
        ref: ref,
        title: title ?? sRef.title,
        description: description ?? sRef.description,
        defaultValue: defaultValue ?? sRef.defaultValue,
        example: example ?? sRef.example,
        nullable: nullable ?? sRef.nullable,
      );
    case SchemaArray():
      result = (sRef as SchemaArray).copyWith(ref: ref);
    case SchemaMap():
      result = (sRef as SchemaMap).copyWith(ref: ref);
  }

  return result;
}