Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
greenmooseSE opened this issue Sep 7, 2024 · 0 comments

Comments

@greenmooseSE
Copy link

(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>();


    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant