FIX: Handle JSON markdown format exceptions #435
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR handles variations of markdown formatted JSON responses which were not previously handled, resulting in exceptions (see #413 for details).
I decided to create a few helper functions in order to maintain code clarity while providing a more robust and extensible solution. Should new or additional patterns need handled in the future, it's likely that modifying the regex to include the new pattern will be sufficient without necessitating logic modifications.
Technical Details
Originally, only one specific case was handled, so if the markdown syntax was not in this format ```json\n .... \n```, it would result in an exception. Now the handler can resolve any combination of the following start and end notations, as well as scenarios where only the start or end markdown was present.
Start Notations Handled: ```json\n, `json\n, ```\n, `\n, ```json, `json, ```, `, json, json\n
End Notations Handled: \n```, \n`, ```, `
Finally, after removing any markdown, I added validation for proper JSON format. If the format is still invalid, another attempt will be taken to extract any valid JSON object or array. This could be useful in many cases; for instance, if the starting markdown tag had a typo such as ```jsn\n, the helper function which removes the starting tag would only remove the ```, so it would still be improper JSON without this final check.
Tests and Documentation
I added unit tests for the new functions I created, as well as for the original exception handler function which was not previously tested. I added comments for each function to document the purpose and usage.
Code Coverage
Coverage Before
### Combined Coverage:### File Specific Coverage:
### Uncovered lines (related to this work item):
Coverage After
### Combined Coverage:### File Specific Coverage:
### New File:
### Previously Untested:
Validation
@romanlutz @nina-msft