Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Given the OpenAPI description document at https://www.bcb.gov.br/htms/dasfn/catalogo/1.0.10/openapi.json referencing the external schema file at https://www.bcb.gov.br/htms/dasfn/catalogo/1.0.10/schema.json, the following code

using Microsoft.OpenApi;
using Microsoft.OpenApi.Reader;

namespace Dasfn
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Uri baseUrl = new("https://www.bcb.gov.br/htms/dasfn/catalogo/1.0.10/");

            OpenApiReaderSettings settings = new()
            {
                BaseUrl = baseUrl,
                LoadExternalRefs = true,
            };

            ReadResult readResult = await OpenApiDocument.LoadAsync(new Uri(baseUrl, "openapi.json").AbsoluteUri, settings);

            if (readResult.Diagnostic?.Errors is IList<OpenApiError> errors)
            {
                foreach (OpenApiError error in errors)
                { 
                    Console.WriteLine(error); 
                }
            }

            OpenApiDocument document = readResult.Document!;

            OpenApiSchemaReference schema = (OpenApiSchemaReference) document
                .Paths["/catalogo"]
                .Operations![HttpMethod.Get]
                .Responses!["200"]
                .Content!["application/json"].Schema!
                ;

            Console.WriteLine($"Schema unresolved reference: {schema.UnresolvedReference}");
        }
    }
}

... reports failure to load schema.json apparently due to it not having a Version field. Furthermore, the schema is marked as an unsolved reference:

[File: schema.json] Version node not found.
Schema unresolved reference: True

It is my understanding that Version is only required on the OpenAPI description itself and not on referenced schemas.

I have no control over the definition of this OpenAPI description document so changing it is not an option.

Is there a proper way or a workaround for loading the external schema?

You must be logged in to vote

Replies: 1 comment

Comment options

I've got it working by patching the OpenAPI description document with a fragment containing the schema

using Microsoft.OpenApi;
using Microsoft.OpenApi.Reader;
using System.Text.Json.Nodes;

namespace Dasfn
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Uri baseUrl = new("https://www.bcb.gov.br/htms/dasfn/catalogo/1.0.10/");

            HttpClient httpClient = new()
            {
                BaseAddress = baseUrl,
            };

            OpenApiReaderSettings settings = new()
            {
                BaseUrl = baseUrl,
                HttpClient = httpClient,
                LoadExternalRefs = true,
            };

            (OpenApiDocument? document, OpenApiDiagnostic? diagnostic) = await OpenApiDocument.LoadAsync(new Uri(baseUrl, "openapi.json").AbsoluteUri, settings);

            if (diagnostic?.Errors is IList<OpenApiError> errors)
            {
                foreach (OpenApiError error in errors)
                { 
                    Console.WriteLine(error); 
                }
            }

            OpenApiMediaType mediaType = document!
                .Paths["/catalogo"]
                .Operations![HttpMethod.Get]
                .Responses!["200"]
                .Content!["application/json"]
                ;

            if (mediaType.Schema is OpenApiSchemaReference schemaReference && schemaReference.UnresolvedReference)
            {
                using MemoryStream stream = new(await httpClient.GetByteArrayAsync(schemaReference.Reference.ExternalResource, CancellationToken.None));

                mediaType.Schema = settings.Readers["json"]
                    .ReadFragment<OpenApiSchema>(stream, diagnostic!.SpecificationVersion, document, out OpenApiDiagnostic schemaDiagnostic);
            }
        }
    }
}

There's probably a better/proper way to do this, but at least for now this will suffice.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.