RicoSuter/NSwag

OpenApiYamlDocument.ToYaml() corrupts document when using $ref in parameters

Opened this issue · 0 comments

(Related to #4360 )
v14.1.0.0

Test below fails:

    [Test]
    public void RefInParametersCanBeParsed()
    {
        var yamlText = @"
openapi: 3.0.0
info:
  title: Title
  version: 1.0.0
paths:
  /items:
    get:
      operationId: items
      parameters:
        - $ref: '#/components/parameters/myHeader'
      responses:
        200:
          description: Get items
          content:
            application/json:
              schema:
                type: string
components:
  parameters:
    myHeader:
      name: myHeaderName
      in: header
      schema:
        type: string
";
        var oaDoc = OpenApiYamlDocument.FromYamlAsync(yamlText).Result!;
        OpenApiParameter? parameterFromFirstDoc = oaDoc.Paths.Values.Single().Values.Single().Parameters.Single();
        parameterFromFirstDoc.HasReference.Should().BeTrue();
        var reference = parameterFromFirstDoc.Reference!;
        reference.Should().BeOfType<OpenApiParameter>();
        var paramFromRef = (OpenApiParameter)reference;
        paramFromRef.Kind.Should().Be(OpenApiParameterKind.Header);
        paramFromRef.Name.Should().Be("myHeaderName");
        
        //Now let's convert this back to yaml
        var newYamlText = oaDoc.ToYaml();
        
        //New yaml vs old are different, "/schema" is appended to the new one.
        /*
           + 01 
             02 openapi: 3.0.0
             03 info:
             09       operationId: items
             10       parameters:
           -         - $ref: '#/components/parameters/myHeader/schema'
           + 11         - $ref: '#/components/parameters/myHeader'
             12       responses:
             13         200:
         
         */
        //newYamlText.zShould().NotDiffWith(yamlText);
        
        var newOaDoc = OpenApiYamlDocument.FromYamlAsync(newYamlText).Result!;
        OpenApiParameter? parameterFromNewDoc = newOaDoc.Paths.Values.Single().Values.Single().Parameters.Single();
        parameterFromNewDoc.HasReference.Should().BeTrue();
        var newDocReference = parameterFromNewDoc.Reference!;

        //This now fails ("Expected type to be NSwag.OpenApiParameter, but found NJsonSchema.JsonSchema.")
        newDocReference.Should().BeOfType<OpenApiParameter>();


    }