Skip to content

Commit

Permalink
Fixed #80 - JSONSchemaParser List would allow opening comma before fi…
Browse files Browse the repository at this point in the history
…rst element if there was a whitespace before it
  • Loading branch information
noamgat committed Apr 20, 2024
1 parent f411dea commit 7173659
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lmformatenforcer/jsonschemaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def add_character(self, new_character: str) -> CharacterLevelParser:
updated_parser.object_stack[receiving_idx] = updated_parser.object_stack[receiving_idx].add_character(new_character)
if new_character in WHITESPACE_CHARACTERS:
updated_parser.num_consecutive_whitespaces += 1
updated_parser.last_non_whitespace_character = self.last_non_whitespace_character
else:
updated_parser.num_consecutive_whitespaces = 0
updated_parser.last_non_whitespace_character = new_character
Expand Down
17 changes: 17 additions & 0 deletions tests/test_jsonschemaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,20 @@ class FlightRoute(BaseModel):

_test_json_schema_parsing_with_string(output_ok, FlightRoute.model_json_schema(), True)
_test_json_schema_parsing_with_string(output_notok, FlightRoute.model_json_schema(), False)


def test_comma_cannot_start_list_2():
# This also stresses the whitespace handling + max consecutive whitespace concept,
# bug reported in https://github.com/noamgat/lm-format-enforcer/issues/80
output_notok = """
{
"airports": [
,"Hamad",
",Doha",
",Bahrain",
",Dammam"
]
}"""
class FlightRoute(BaseModel):
airports: List[str]
_test_json_schema_parsing_with_string(output_notok, FlightRoute.model_json_schema(), False)

0 comments on commit 7173659

Please sign in to comment.