Skip to content

Support relative URI resolution in $dynamicRef #2928

Description

Problem

$dynamicRef values with relative URIs (e.g., ./external.yaml#node) are not resolved. FindDocumentByBaseUri compares the raw string against document base URIs, so a relative reference never matches an absolute document URI.

Example:

# Document A (baseUri: https://example.com/main.yaml)
Tree:
  properties:
    next:
      $dynamicRef: './external.yaml#node'

# Document B (baseUri: https://example.com/external.yaml)
ExternalNode:
  $dynamicAnchor: node
  type: object

$dynamicRef: './external.yaml#node' should resolve to https://example.com/external.yaml (Document B), but FindDocumentByBaseUri('./external.yaml') cannot match https://example.com/external.yaml.

Proposed fix

Pass the host document's BaseUri to FindDocumentByBaseUri and resolve the relative reference via new Uri(baseUri, documentUri):

internal OpenApiDocument? FindDocumentByBaseUri(string documentUri, Uri? baseUri)
{
    var resolved = baseUri is not null 
        ? new Uri(baseUri, documentUri).ToString() 
        : documentUri;
    return _dynamicAnchorRegistryByDocument.Keys
        .Concat(_anchorRegistryByDocument.Keys)
        .Distinct()
        .FirstOrDefault(doc => doc.BaseUri?.ToString()
            .Equals(resolved, StringComparison.OrdinalIgnoreCase) == true);
}

Scope

  • OpenApiWorkspace.FindDocumentByBaseUri — accept optional base URI parameter
  • OpenApiSchemaReference.Target — pass Reference.HostDocument.BaseUri
  • Tests — cross-document resolution with relative URIs

Related

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions