SharpYaml icon indicating copy to clipboard operation
SharpYaml copied to clipboard

SemanticErrorException when path key exceeds 1023 characters

Open ShaunLoganOracle opened this issue 3 years ago • 3 comments

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?

ShaunLoganOracle avatar Mar 08 '22 22:03 ShaunLoganOracle

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 avatar Mar 09 '22 08:03 xoofx

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?

ShaunLoganOracle avatar Mar 09 '22 21:03 ShaunLoganOracle

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.

xoofx avatar Mar 10 '22 05:03 xoofx