Skip to content

Bug: PyYAML cannot correct parse asyncapi.yaml #2709

@chirizxc

Description

@chirizxc

Description

Starting with v0.6.0 faststream supports AsyncAPI 3.0.

The AsyncAPI 3.0 documentation states:

In order to preserve the ability to round-trip between YAML and JSON formats, YAML version 1.2 is recommended along with some additional constraints:

  • Tags MUST be limited to those allowed by the JSON Schema ruleset
  • Keys used in YAML maps MUST be limited to a scalar string, as defined by the YAML Failsafe schema ruleset

Note: AsyncAPI use js-yaml as parser, *js-yaml playground

According to spec, yaml of the following type:

protocolVersion: ! "3.2"

should be converted to {protocolVersion: "3.2"}, i.e. to a string, but PyYAML converts it to an float:

import yaml

y = "protocolVersion: ! '3.2'"

print(yaml.safe_load(y))  # {'protocolVersion': 3.2}
print(type(yaml.safe_load(y)["protocolVersion"]))  # <class 'float'>

def _get_yaml_config(file: Path) -> dict[str, Any] | Any:
"""Parse yaml config file to dict."""
try:
import yaml
except ImportError as e:
typer.echo(INSTALL_YAML, err=True)
raise typer.Exit(1) from e
with file.open("r", encoding="utf-8") as config_file:
return yaml.safe_load(config_file)

(Also, PyYAML does not fully support YAML v1.2.* and does not pass the official yaml-test-suite).

Valid AsyncAPI config: https://studio.asyncapi.com/?share=851921b7-91cd-414c-9438-e94876953c0d

but faststream cannot load it

❯ faststream docs serve asyncapi.yaml                                                                                                                 
`asyncapi.yaml` not supported. Make sure that your schema is valid and schema version supported by FastStream

Proposal: make it plug-able so that the user can use any parser through the interface.

(Also we need improve error message; right now it's unclear. It needs to clearly show whether it's a parsing error or a pydantic validation error.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions