manifestToOpenApi function

OpenApiSpec manifestToOpenApi(
  1. RouteManifest manifest, {
  2. OpenApiConfig config = const OpenApiConfig(),
})

Converts a RouteManifest into an OpenApiSpec.

Implementation

OpenApiSpec manifestToOpenApi(
  RouteManifest manifest, {
  OpenApiConfig config = const OpenApiConfig(),
}) {
  final paths = <String, OpenApiPathItem>{};
  final tagNames = <String>{};

  for (final route in manifest.routes) {
    // Skip fallback routes — they have no meaningful path.
    if (route.isFallback) continue;

    final schema = _schemaForRoute(route);

    // Skip hidden routes unless explicitly included.
    if (schema != null && schema.hidden && !config.includeHidden) continue;

    // Convert routed path params (:id) to OpenAPI path params ({id}).
    final openApiPath = _convertPath(route.path);
    final method = route.method.toUpperCase();

    // Build the operation for this route.
    final operation = _buildOperation(route, schema);

    // Collect tags.
    tagNames.addAll(operation.tags);

    // Merge into the path item (multiple methods can share a path).
    final existing = paths[openApiPath] ?? const OpenApiPathItem();
    paths[openApiPath] = existing.withOperation(method, operation);
  }

  // Build tag objects from collected names.
  final tags = tagNames.map((name) => OpenApiTag(name: name)).toList()
    ..sort((a, b) => a.name.compareTo(b.name));

  return OpenApiSpec(
    info: OpenApiInfo(
      title: config.title,
      version: config.version,
      description: config.description,
    ),
    servers: config.servers,
    paths: paths,
    tags: tags,
  );
}