How to load an OpenAPI description document with an external schema #2481
Unanswered
alfredmyers
asked this question in
Q&A
Replies: 1 comment
-
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. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
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
... reports failure to load schema.json apparently due to it not having a
Version
field. Furthermore, the schema is marked as an unsolved reference: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?
Beta Was this translation helpful? Give feedback.
All reactions