Skip to content

Read and Write results in different JSON #683

@janRucka

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.

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