Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: relative references in subdirectory documents are not loading #1674 #2243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dldl-cmd
Copy link
Contributor

@dldl-cmd dldl-cmd commented Mar 10, 2025

Use OpenApiDocuments BaseUri as location of the document. This allows to have during loading further documents a base Url for retrieval, which can be combined with a relative Uri to get an absolute.
Fixes #1674

…crosoft#1674

Use OpenApiDocuments BaseUri as location of the document. This allows to have during loading further documents a base Url for retrieval, which can be combined with a relative Uri to get an absolute.
@baywet
Copy link
Member

baywet commented Mar 12, 2025

Thanks for the contribution!

@MaggieKimani1 can you do an initial review here please?

@@ -259,11 +265,11 @@
private static async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, OpenApiReaderSettings settings, string format = null, CancellationToken token = default)
{
// Create workspace for all documents to live in.
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);
var openApiWorkSpace = new OpenApiWorkspace(baseUrl);
var baseUrl = document.BaseUri;

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
baseUrl
is useless, since its value is never read.
@MaggieKimani1
Copy link
Contributor

MaggieKimani1 commented Mar 20, 2025

@dldl-cmd sorry for the delay in response/review.
I have checked out the issue that this PR resolves and it seems that this is by design.
Have you considered creating a custom external refs loader, an implementation of our IStreamLoader interface as per Darrel's comment here?
Our default stream reader class is a bit limited as file/resource location might vary depending on use case, hence the exposed interface that you can use to resolve the relative references for documents contained in subdirectories, and pass the custom loader as part of the OpenApiReaderSettings as shown here


Once you do so, the files will automatically be registered with the source document's workspace.

Reference:

public class ResourceLoader : IStreamLoader
{
public Stream Load(Uri uri)
{
return null;
}
public Task<Stream> LoadAsync(Uri uri, CancellationToken cancellationToken = default)
{
var path = new Uri(new("http://example.org/V3Tests/Samples/OpenApiWorkspace/"), uri).AbsolutePath;
path = path[1..]; // remove leading slash
return Task.FromResult(Resources.GetStream(path));
}
}

@dldl-cmd
Copy link
Contributor Author

dldl-cmd commented Mar 20, 2025

@MaggieKimani1 implementing just an own stream loader cannot solve the problem. The reason is, that the stream loader has no idea, which document is the current parent and where this document is located. Therefore it cannot resolve the relative reference as it doesn't know to which location it is relative. An example:

  • root.yaml
....
$ref: ./DirA/DirB/DirC/second.yaml#/components/schema/Second
  • DirA/DirB/DirC/second.yaml
...
$ref: ../third.yaml#/components/schema/Third
  • DirA/DirB/third.yaml
...
$ref: ./DirC/Fourth.yaml#/components/schema/Fourth.yaml

When the reference $ref: ./DirC/Fourth.yaml#/components/schema/Fourth.yaml should be resolved. It is necessary to known that this reference is relative to the document at DirA/DirB/third.yaml.

This information is not only necessary at the time of loading the document but also when OpenApiDocument.ResolveReference tries to resolve a reference.

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.

Relative reference in subdirectory OpenApi document fails to load
3 participants