Skip to content

Cross-file type references fail when referenced type's file sorts after referencing file #362

@mikaello

Description

@mikaello

Bug report

Description

When using multiple .avsc files where one schema references a named type (record/enum/fixed) defined in another file, avrodoc-plus throws a browser console error if the referenced file sorts alphabetically after the referencing file:

Uncaught Unknown type name "com.example.enums.LogLevel" at logLevel

Root cause

In public/js/avrodoc.js, schemas share a single shared_types registry and are processed in the order they are provided (reflecting alphabetical filename order from the CLI). In public/js/schema.js, type references are resolved eagerly during parsing. Since complex/references_external_types.avsc is parsed before enums/loglevel.avsc, the referenced type is not yet in shared_types when the logLevel field is resolved.

The shared_types registry is shared across all files, so the approach would work if schemas were processed in dependency order — types defined before types that reference them.

Reproduction

  1. Create two schemas in the same input directory:

enums/log_level.avsc

{
  "type": "enum",
  "name": "LogLevel",
  "namespace": "com.example",
  "symbols": ["DEBUG", "INFO", "WARN", "ERROR"]
}

complex/composite.avsc (processed before enums/ alphabetically)

{
  "type": "record",
  "name": "Composite",
  "namespace": "com.example",
  "fields": [
    { "name": "level", "type": "com.example.LogLevel" }
  ]
}
  1. Run: npx @mikaello/avrodoc-plus -i schemas -o out.html
  2. Open out.html — browser console shows: Uncaught Unknown type name "com.example.LogLevel" at level

Expected behaviour

Cross-file type references should resolve regardless of filename sort order. Possible fixes:

  • Two-pass resolution in the browser JS: first pass registers all named types from all schemas; second pass resolves field type references.
  • Topological sort in the CLI: before embedding schemas in the HTML, sort them so definitions always precede their references.

Environment

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