Skip to content

Read and Write results in different JSON #683

Closed

Description

@janRucka

Simple Read and Write results in different documents. What is even more alarming is that after write the document is invalid (tested here and also openapi-diff crashes on it. Original is valid) Following simplified example demonstrates it:

        public static void Test()
        {
            var input =
@"{
  ""openapi"": ""3.0.1"",
  ""paths"": {
    ""/api/v2/test"": {
      ""post"": {
        ""operationId"": ""Test"",
        ""requestBody"": {
          ""content"": {
            ""application/x-www-form-urlencoded"": {
              ""schema"": {
                ""type"": ""object"",
                ""properties"": {
                  ""Username"": {
                    ""type"": ""string"",
                    ""nullable"": true
                  }
                }
              },
              ""encoding"": {
                ""Username"": {
                  ""style"": ""form""
                }
              }
            }
          }
        },
        ""responses"": {
          ""200"": {
            ""description"": ""Success"",
            ""content"": {
              ""application/json"": {}
            }
          }
        }
      }
    }
  }
}";
            var apiDocument = new OpenApiStringReader().Read(input, out var _);

            var stringBuilder = new StringBuilder();
            using var stringWriter = new StringWriter(stringBuilder);
            apiDocument.SerializeAsV3(new OpenApiJsonWriter(stringWriter));
            stringWriter.Flush();
            var output = stringBuilder.ToString();

            var actualOutput =
@"{
  ""openapi"": ""3.0.1"",
  ""info"": { },
  ""paths"": {
    ""/api/v2/test"": {
      ""post"": {
        ""operationId"": ""Test"",
        ""requestBody"": {
          ""content"": {
            ""application/x-www-form-urlencoded"": {
              ""schema"": {
                ""type"": ""object"",
                ""properties"": {
                  ""Username"": {
                    ""type"": ""string"",
                    ""nullable"": true
                  }
                }
              },
              ""encoding"": {
                ""Username"": { }
              }
            }
          }
        },
        ""responses"": {
          ""200"": {
            ""description"": ""Success"",
            ""content"": {
              ""application/json"": null
            }
          }
        }
      }
    }
  }
}";
        }

Notice "application/json": null (=this is why it is invalid) vs "application/json": {} and encoding.

Activity

  1. added this to the 1.3 milestone on Jan 23, 2022
  2. darrelmiller commented on Jan 23, 2022

    @darrelmiller
    Member

    Thank you for reporting this. Roundtripping empty media type objects should work. However, it is an interesting question to ask if null is functionally equivalent to an empty object in this case. I've raised this on the OpenAPI slack. In the meanwhile I have made the fix.

    I also have fixed the issue with the encoding style. There's not many people that use that feature so it had a bug that was simple to fix. This should go out in the next preview release of 1.3.0.

  3. janRucka commented on Jan 27, 2022

    @janRucka
    Author

    Thank you as well!

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions