Skip to content

[Bug]: Valid YAML doc fails to parse when starting with a curly brace #2231

@RafeArnold

Description

@RafeArnold

Description

When a valid OpenAPI YAML document starts with an opening curly brace {, parsing returns a result with a null OpenAPI and error messages. This affects Swagger 2.0 and OpenAPI 3.0/3.1.

The error is caused by the parser checking to see if the first non-whitespace character of the document is a { and, if it is, assuming the rest of the document is valid JSON and using a JSON specific deserialiser.

Affected Version

2.1.33

Steps to Reproduce

The error can be reproduced by running the following Java code

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;

import java.util.List;

class Parse {
    public static void main(String[] args) {
        String doc = """
                {
                  openapi: "2.0.0",
                  "info": {
                    "title": "Test API spec",
                    "version": "1.0.0"
                  },
                  "paths": {}
                }
                """;
        var parser = new OpenAPIParser();
        SwaggerParseResult result = parser.readContents(doc, List.of(), new ParseOptions());
        assert result.getOpenAPI() != null;
        assert result.getMessages().isEmpty();
    }
}

Expected Behavior

The assertions in the provided code example succeed.

Actual Behavior

The assertions in the provided code example fail.

Environment

  • Java version: Corretto 17
  • Build tool: 8.14
  • OS: macOS Sequoia 15.6.1

Checklist

  • I have searched the existing issues and this is not a duplicate.
  • I have provided sufficient information for maintainers to reproduce the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions