Skip to content

Schema reference not parsed as expected, results in circular reference #415

@ShaunLoganOracle

Description

@ShaunLoganOracle

It looks like there is an issue parsing schema references of a certain structure in an OA3 json document. The problem manifests when schema A is defined (only) as ""$ref":"#/components/schemas/B". in this case, the OpenApiDocument.Components.Schemas entry for A, has an UnresolvedReference, but the Reference.Id is #/components/schemas/A.

To demonstrate this using PetStore (OA 3, json), I have made two changes:

  1. change the response for the /pets path's Get operation to refer to AllPets:
    it was:
    "$ref": "#/components/schemas/Pets"
    change to:
    "$ref": "#/components/schemas/AllPets"
    and then,
  2. add the new AllPets schema definition:
    "AllPets": { "$ref":"#/components/schemas/Pets" },
    (yes, this seems unnecessary, but in my case I don't control the OA3 json generation, I'm just trying to parse it).

Per my reading of the OA3 spec, this syntax/structure should be valid: the schemas element is a map of string (schema id) to either a schema or a reference object.
From: OA3 spec.

When I attempt to ResolveSchema() on the AllPets schema object, it fails to resolve - I would expect it to resolve to the Pets schema object:
` string oaFilepath = GetTestFilepath ("petstore-3.0.json");

 OpenApiStreamReader reader = new OpenApiStreamReader ();
 using (Stream file = new FileStream (oaFilepath, FileMode.Open))
 {
    OpenApiDocument doc = reader.Read (file, out OpenApiDiagnostic diags);
    OpenApiSchema schema1 = doc.Components.Schemas["AllPets"];
    if (!schema1.UnresolvedReference)
       return;
    OpenApiSchema schema2 = (OpenApiSchema)doc.ResolveReference (schema1.Reference);
    if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
    {
       // detected a cycle - this code gets triggered
       Log ("This is a circular reference, WTF? {0}", schema1.Reference.Id);
    }
 }

`
I am fairly new to using OpenApi.NET so I might be misunderstanding what is expected. I tried to trace through the reader/parsing code and it looks like the SharpYaml representation has the expected reference from AllPets to Pets, but by the time the OpenApiDocument gets returned from the reader.Read() call, the Components.Schemas["AllPets"] entry is incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions