resolveUri method Null safety

APIObject? resolveUri(
  1. Uri uri
)

Returns a component definition for uri.

Construct uri as a path, e.g. Uri(path: /components/schemas/name).

Implementation

APIObject? resolveUri(Uri uri) {
  final segments = uri.pathSegments;
  if (segments.length != 3) {
    throw ArgumentError(
      "Invalid reference URI. Must be a path URI of the form: '/components/<type>/<name>'",
    );
  }

  if (segments.first != "components") {
    throw ArgumentError(
      "Invalid reference URI: does not begin with /components/",
    );
  }

  Map<String, APIObject?>? namedMap;
  switch (segments[1]) {
    case "schemas":
      namedMap = schemas;
      break;
    case "responses":
      namedMap = responses;
      break;
    case "parameters":
      namedMap = parameters;
      break;
    case "requestBodies":
      namedMap = requestBodies;
      break;
    case "headers":
      namedMap = headers;
      break;
    case "securitySchemes":
      namedMap = securitySchemes;
      break;
    case "callbacks":
      namedMap = callbacks;
      break;
  }

  if (namedMap == null) {
    throw ArgumentError(
      "Invalid reference URI: component type '${segments[1]}' does not exist.",
    );
  }

  final result = namedMap[segments.last];

  return result;
}