Skip to content

fix(cli): surface YAML and AsyncAPI errors from docs serve#2874

Open
SarthakB11 wants to merge 1 commit into
ag2ai:mainfrom
SarthakB11:fix/issue-2709-diagnostics
Open

fix(cli): surface YAML and AsyncAPI errors from docs serve#2874
SarthakB11 wants to merge 1 commit into
ag2ai:mainfrom
SarthakB11:fix/issue-2709-diagnostics

Conversation

@SarthakB11

Copy link
Copy Markdown

Description

Refs #2709 (diagnostic portion).

Per @Sehat1137's recommendation on #2858 ("a smaller PR limited to that behavior, with deterministic parsing and regression tests"), this is the diagnostic-only redo: no new CLI options, no parser plugin, default yaml.safe_load preserved. The pluggable-parser proposal from #2709 is intentionally deferred per maintainer feedback.

faststream docs serve <asyncapi.yaml> previously collapsed three different failure modes into one opaque SCHEMA_NOT_SUPPORTED message:

  • YAML parse errors were not surfaced. yaml.safe_load ran outside any try, so the user either got an uncaught traceback or, more often, a downstream pydantic failure on the garbage that partial parsing produced.
  • AsyncAPI validation errors were dropped by contextlib.suppress(ValidationError), so there was no indication of which schema version was tried or why it failed.
  • YAML scalar gotchas like protocolVersion: 3.2 (parsed as a float, rejected by AsyncAPI's string type) gave no hint that the value just needed quoting. This is the exact repro from Bug: PyYAML cannot correct parse asyncapi.yaml #2709.

Fix

Edits only _parse_and_serve in faststream/_internal/cli/docs.py:

  1. Wrap yaml.safe_load in try / except yaml.YAMLError, echo the parser exception, exit 1.
  2. Replace suppress(ValidationError) with an explicit collector that records (version_label, ValidationError). When neither v3.0 nor v2.6 validates, print SCHEMA_NOT_SUPPORTED followed by both per-version error blocks on stderr.
  3. For each block, if any sub-error has type == "string_type" and an int | float | bool input, append a one-line hint pointing at the location and suggesting YAML quoting (e.g. '3.2').

Default parser is unchanged. _parse_and_serve's public signature is untouched. dto.py is untouched. No new modules, no parser registry, no --yaml-parser flag.

Tests

Two regression tests in tests/cli/test_asyncapi_docs.py:

  • test_serve_asyncapi_yaml_unquoted_scalar_reports_hint mutates the existing yaml_asyncapi_doc fixture to protocolVersion: 3.2, asserts exit code 1 and the v3.0 / v2.6 error enumeration plus the quoting hint.
  • test_serve_asyncapi_reports_yaml_parse_error feeds malformed YAML (foo: [unclosed) and asserts the parser exception is surfaced rather than swallowed.

Both follow the existing faststream_cli + generate_template + wait_for_stderr pattern in the same file.

Type of change

  • Bug fix (a non-breaking change that resolves an issue)

Checklist

  • My code adheres to the style guidelines of this project (just lint shows no errors)
  • I have conducted a self-review of my own code
  • I have made the necessary changes to the documentation
  • My changes do not generate any new warnings
  • I have added tests to validate the effectiveness of my fix or the functionality of my new feature
  • Both new and existing unit tests pass successfully on my local environment by running just test-coverage
  • I have ensured that static analysis tests are passing by running just static-analysis
  • I have included code examples to illustrate the modifications

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant