Skip to content

Schema Missing #371

Closed
Closed

Description

@robbymeier

When a global consumes/produces is set after paths then the schema is not loaded when calling OpenApiStreamReader.Read. The code below replicates that issue. The original json has the consumes/produces at the end. It then produces openApiDocument3 with the missing schema. The code then moves the 'produces' before 'paths' and produces openApiDocument4. In this case the schema is not missing. The different results are shown in the attachment. Note that some tools produce json descriptions with global consumes/produces after paths so this is not just a matter of "why don't you just move it to the front".

string jsonDescription = "{"swagger":"2.0","info":{"description":"consumes/produces issue","version":"2.0.0","title":"Swagger Petstore"},"host":"petstore.swagger.io","basePath":"/v2","paths":{"/pet/{petId}":{"get":{"tags":["pet"],"summary":"Find pet by ID","operationId":"getPetById","parameters":[{"name":"petId","in":"path","description":"ID of pet to return","required":true,"type":"integer","format":"int64"}],"responses":{"200":{"description":"successful operation","schema":{"$ref":"#/definitions/Pet"}}}}}},"definitions":{"Pet":{"type":"object","required":["name","photoUrls"],"properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string","example":"doggie"}},"xml":{"name":"Pet"}}},"produces":["application/json"]}";
missingschema

byte[] byteArray3 = Encoding.ASCII.GetBytes(jsonDescription);
MemoryStream stream3 = new MemoryStream(byteArray3);
OpenApiDocument openApiDocument3 = new OpenApiStreamReader().Read(stream3, out var diagnostic3);
//Re-arrange produces.
Newtonsoft.Json.Linq.JObject jObject3 = Newtonsoft.Json.Linq.JObject.Parse(jsonDescription);
Newtonsoft.Json.Linq.JProperty producesProperty = jObject3.Property("produces");
Newtonsoft.Json.Linq.JProperty paths = jObject3.Property("paths");
jObject3.Property("produces").Remove();
paths.AddBeforeSelf(producesProperty);

jsonDescription = jObject3.ToString();
byteArray3 = Encoding.ASCII.GetBytes(jsonDescription);
stream3 = new MemoryStream(byteArray3);
OpenApiDocument openApiDocument4 = new OpenApiStreamReader().Read(stream3, out var diagnostic4);

Activity

  1. darrelmiller commented on Dec 23, 2018

    @darrelmiller
    Member

    I believe this PR #368 should fix your issue.

  2. jaredgermano commented on Jan 4, 2019

    @jaredgermano

    SchemaRepro.zip

    The title sounds like the problem I'm having though i'm not sure it's the exact same.

    Using Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer 3,1 on our API, the schema field appears to not get parsed correctly (Or the JSON is malformed). Either way I've attached a repro project with sample json that should demonstrate the issue. Kindly refer me elsewhere if you don't think it's a problem with the reader, however this worked in version 2.2.0 of the API explorer.

    EDIT: On further inspection it looks like the new Reader version 1.1.2 exhibits the problem but not 1.1.1.

  3. PerthCharern commented on Jan 8, 2019

    @PerthCharern
    Contributor

    jaredgermano

    I think #368 alone is not a fix to your problem but it should have solved a regression that was introduced earlier.

    That said, the latest version containing fix in #368 hasn't been released as a NuGet package yet. I can release it soon, and will notify this thread. After that, could you give it a try and let us know if you are still seeing the problem?

    If that still doesn't work, I'll investigate this more.

  4. tsieberg commented on Feb 7, 2019

    @tsieberg

    I'm having the same issue and have verified that #368 does resolve my issue. I would like to see this released as a NuGet package so I can consume the fix on our build server.

  5. PerthCharern commented on Apr 28, 2019

    @PerthCharern
    Contributor

    It looks like the issue should have been solved by #368 and the NuGet package has been published. I'll close this issue - please feel free to open if the bug is still not fixed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions