Skip to content

Reader changes external reference #986

@richardkaspar

Description

@richardkaspar

Describe the bug
Microsoft.OpenApi.Readers 1.3.2 changes external references, that contains the character #.
The behavior of Microsoft.OpenApi.Readers 1.2.3 is as expected. It does not change external references. I did not check other Versions.
Example:
Reference in the OpenAPI file: https://gigantic-server.com/schemas/Monster#/?id=foo
After reading: https://gigantic-server.com/schemas/Monster#/components/schemas//?id=foo

To Reproduce
Use the following code to read 'readFile' and write 'writeFile'.
Open the two files in a compare tool.
Code:

        using var stream = new FileStream(readFile, FileMode.Open);
        var doc =
            new OpenApiStreamReader(
                    new OpenApiReaderSettings
                    {
                        ReferenceResolution = ReferenceResolutionSetting.DoNotResolveReferences
                    })
               .Read(stream, out _);

        var sb = new StringBuilder();

        var writer = new OpenApiYamlWriter(new StringWriter(sb, new CultureInfo("en_us")));

        doc.SerializeAsV3(writer);

        File.WriteAllText(writeFile, sb.ToString());

readFile:

    openapi: 3.0.1
    info:
      title: anyOf-oneOf
      license:
        name: MIT
      version: 1.0.0
    paths: { }
    components:
      schemas:
        Nested:
          type: object
          properties:
            AnyOf:
              anyOf:
                - $ref: '#/components/schemas/OneOf'
                - $ref: file:///C:/MySchemas.json#/definitions/ArrayObject
                - $ref: https://gigantic-server.com/schemas/Monster#/?id=foo
                - $ref: '#/components/schemas/ExtRef'
            OneOf:
              oneOf:
                - $ref: ../foo/schemas.yaml#/components/schemas/Number
                - $ref: '#/components/schemas/ExtRef'
                - $ref: '#/components/schemas/AnyOf'
                - type: string
        OneOf:
          oneOf:
            - $ref: ../foo/schemas.yaml#/components/schemas/Number
            - $ref: file:///C:/MySchemas.json#/definitions/ArrayObject
            - $ref: https://gigantic-server.com/schemas/Monster#/?id=foo
            - $ref: '#/components/schemas/ExtRef'
            - $ref: '#/components/schemas/AnyOf'
            - type: string
        AnyOf:
          anyOf:
            - $ref: '#/components/schemas/OneOf'
            - $ref: ../foo/schemas.yaml#/components/schemas/Number
            - $ref: file:///C:/MySchemas.json#/definitions/ArrayObject
            - $ref: https://gigantic-server.com/schemas/Monster#/?id=foo
            - $ref: '#/components/schemas/ExtRef'
        ExtRef:
          $ref: https://www.mid.de/OpenAPI/Schema/foo#/

writeFile:

    openapi: 3.0.1
    info:
      title: anyOf-oneOf
      license:
        name: MIT
      version: 1.0.0
    paths: { }
    components:
      schemas:
        Nested:
          type: object
          properties:
            AnyOf:
              anyOf:
                - $ref: '#/components/schemas/OneOf'
                - $ref: file:///C:/MySchemas.json#/components/schemas//definitions/ArrayObject
                - $ref: https://gigantic-server.com/schemas/Monster#/components/schemas//?id=foo
                - $ref: '#/components/schemas/ExtRef'
            OneOf:
              oneOf:
                - $ref: ../foo/schemas.yaml#/components/schemas/Number
                - $ref: '#/components/schemas/ExtRef'
                - $ref: '#/components/schemas/AnyOf'
                - type: string
        OneOf:
          oneOf:
            - $ref: ../foo/schemas.yaml#/components/schemas/Number
            - $ref: file:///C:/MySchemas.json#/components/schemas//definitions/ArrayObject
            - $ref: https://gigantic-server.com/schemas/Monster#/components/schemas//?id=foo
            - $ref: '#/components/schemas/ExtRef'
            - $ref: '#/components/schemas/AnyOf'
            - type: string
        AnyOf:
          anyOf:
            - $ref: '#/components/schemas/OneOf'
            - $ref: ../foo/schemas.yaml#/components/schemas/Number
            - $ref: file:///C:/MySchemas.json#/components/schemas//definitions/ArrayObject
            - $ref: https://gigantic-server.com/schemas/Monster#/components/schemas//?id=foo
            - $ref: '#/components/schemas/ExtRef'
        ExtRef:
          $ref: https://www.mid.de/OpenAPI/Schema/foo#/components/schemas//

Expected behavior
The content of writeFile and readFile should be equal.

Screenshots/Code Snippets
grafik

Additional context

Metadata

Metadata

Assignees

Labels

type:bugA broken experience

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions