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

SemanticErrorException when path key exceeds 1023 characters #95

Open
ShaunLoganOracle opened this issue Mar 8, 2022 · 3 comments
Open
Labels

Comments

@ShaunLoganOracle
Copy link

ShaunLoganOracle commented Mar 8, 2022

Using v1.8.0.0 (via OpenAPI.NET v1.2.3) to parse an OA3 json document, we are seeing a SemanticErrorException when one of the keys in the Paths collection exceeds 1023 characters. For example, take the PetStore reference OA3 json and change the /pets path key to be extremely long. Attempt to read/parse the file, and get this exception:

SharpYaml.SemanticErrorException: (Lin: 15, Col: 1030, Chr: 1276) - (Lin: 15, Col: 1031, Chr: 1277): While parsing a flow mapping,  did not find expected ',' or '}'.
   at SharpYaml.Parser`1.ParseFlowMappingKey(Boolean isFirst) in C:\SharpYaml 1.8.0\src\SharpYaml\Parser.cs:line 903
   at SharpYaml.Parser`1.StateMachine() in C:\SharpYaml 1.8.0\src\SharpYaml\Parser.cs:line 198
   at SharpYaml.Parser`1.MoveNext() in C:\SharpYaml 1.8.0\src\SharpYaml\Parser.cs:line 128
   at SharpYaml.EventReader.MoveNext() in C:\SharpYaml 1.8.0\src\SharpYaml\EventReader.cs:line 117
   at SharpYaml.EventReader.Allow[T]() in C:\SharpYaml 1.8.0\src\SharpYaml\EventReader.cs:line 146
   at SharpYaml.Model.YamlValue.Load(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlValue.cs:line 59
   at SharpYaml.Model.YamlNode.ReadElement(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlNode.cs:line 44
   at SharpYaml.Model.YamlMapping.Load(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlMapping.cs:line 132
   at SharpYaml.Model.YamlNode.ReadElement(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlNode.cs:line 38
   at SharpYaml.Model.YamlMapping.Load(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlMapping.cs:line 132
   at SharpYaml.Model.YamlNode.ReadElement(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlNode.cs:line 38
   at SharpYaml.Model.YamlDocument.Load(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlDocument.cs:line 49
   at SharpYaml.Model.YamlStream.Load(EventReader eventReader, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlStream.cs:line 68
   at SharpYaml.Model.YamlStream.Load(TextReader stream, YamlNodeTracker tracker) in C:\SharpYaml 1.8.0\src\SharpYaml\Model\YamlStream.cs:line 60

(Edit: updated exception stack trace to include line numbers)

Here is the sample code that demonstrates this:

         var filepath = @"c:\temp\petstore-3.0-with-a-really-long-path.json";
            using (Stream file = new FileStream (filepath, FileMode.Open))
            {
               YamlDocument yamlDocument;
               using (var streamReader = new StreamReader (file))
               {
                  var yamlStream = new YamlStream ();
                  yamlStream.Load (streamReader);  // <-- this line throws
                  yamlDocument = yamlStream.Documents.First ();
                  Assert.IsNotNull (yamlDocument);
               }
            }

Here is the sample (long) path entry in the json file:

/pets-with-a-really-long-path-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123

Our expectation is that keys of arbitrary lengths could be parsed. Is there a workaround for this apparent limitation?

@xoofx
Copy link
Owner

xoofx commented Mar 9, 2022

Our expectation is that keys of arbitrary lengths could be parsed. Is there a workaround for this apparent limitation?

Nope, according to YAML specs 1.2.2

To limit the amount of lookahead required, the “:” indicator must appear at most 1024 Unicode characters beyond the start of the key. In addition, the key is restricted to a single line.

@xoofx xoofx added the invalid label Mar 9, 2022
@ShaunLoganOracle
Copy link
Author

OK, thanks for that pointer. So it seems that even though JSON does not have a limitation in the spec on key size (that I could find), I did find a reference that parser implementations could set a limit.

To summarize my case:

  • our tool is consuming OpenApi 3 documents in JSON format
  • we do this via OpenAPI.NET which relies on SharpYaml for parsing the raw input text into a YamlDocument
  • we have real world customers generating OA3 that contains path keys (in the OA3 Paths Object) that exceed 1024 characters
  • as far as we can tell, there is no size limit for these keys in the OA3 Spec or in OpenAPI.NET
  • so we're bumping up against SharpYaml's limitation

Would it be possible to have this limitation be configurable so that the consumers of SharpYaml (like OpenAPI.NET) could pass in or otherwise set a higher limit?

@xoofx
Copy link
Owner

xoofx commented Mar 10, 2022

Would it be possible to have this limitation be configurable so that the consumers of SharpYaml (like OpenAPI.NET) could pass in or otherwise set a higher limit?

Yes. PR Welcome.

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

No branches or pull requests

2 participants