Skip to content

Generator can crash the consuming build on unrelated additional files #70

Description

@YVbakker

The AdditionalTextsProvider filter matches any .yaml/.yml/.json file marked as AdditionalFiles in the consuming project, not just OpenAPI specs:

var provider = context.AdditionalTextsProvider
    .Where(file => file.Path.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                   file.Path.EndsWith(".yml", StringComparison.OrdinalIgnoreCase) ||
                   file.Path.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
    .Collect();

Combined with OpenApiDocumentProcessor.ProcessDocument:

return (document.Components?.Schemas ?? throw new InvalidOperationException("Document has no schemas"))

Any unrelated JSON/YAML additional file (e.g. appsettings.json) picked up by a consumer will throw and surface as a generator-crash diagnostic (CS8785), breaking their build.

Suggested fix:

  • Require an explicit opt-in (filename convention, e.g. *.openapi.yaml, or MSBuild item metadata) instead of matching by extension alone.
  • Replace thrown exceptions with context.ReportDiagnostic(...) using a proper DiagnosticDescriptor so a single bad/unrelated file doesn't take down the whole build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions