Skip to content

Commit

Permalink
[CDF-23515] 🤔GraphQL Comment issue (#1289)
Browse files Browse the repository at this point in the history
* tests: added failing test

* tests: extend test case

* fix: graphql parser ignore end of line comments

* build: changelog

* fix: introduced bug
  • Loading branch information
doctrino authored Dec 11, 2024
1 parent c3d7ac0 commit 827acfd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.cdf-tk.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Changes are grouped as follows:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## TBD

### Fixed

- [alpha feature] Deploying `GraphQL` now correctly ignores end-of-line comments in the `.graphql` file.

## [0.3.20] - 2024-12-10

### Fixed
Expand Down
17 changes: 14 additions & 3 deletions cognite_toolkit/_cdf_tk/utils/graphql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,23 @@ def _parse(self) -> "list[_Entity]":
parentheses: list[str] = []
directive_tokens: _DirectiveTokens | None = None
is_directive_start = False
is_comment = False
is_multiline_comment = False
is_end_of_line_comment = False
is_in_double_quote = False
is_in_single_quote = False
tokens = self._token_pattern.findall(self.raw)
for no, token in enumerate(tokens):
if no >= 2 and (tokens[no - 2 : no + 1] == ['"'] * 3 or tokens[no - 2 : no + 1] == ["'"] * 3):
is_comment = not is_comment
if is_comment:
is_multiline_comment = not is_multiline_comment
elif token == '"':
is_in_double_quote = not is_in_double_quote
elif token == "'":
is_in_single_quote = not is_in_single_quote
elif token == "#" and not (is_in_double_quote or is_in_single_quote):
is_end_of_line_comment = True
if "\n" in token and is_end_of_line_comment:
is_end_of_line_comment = False
if is_multiline_comment or is_end_of_line_comment:
continue

token = self._multi_newline.sub("\n", token)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_unit/test_cdf_tk/test_utils/test_graphql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,31 @@
{ViewId("cdf_cdm", "CogniteCADModel", "v1")},
id="Setting custom filter on view",
),
pytest.param(
'''"""
@name Tag (Beta)
@code CTG
@Description Beta version only. Should not be used unless aligned with Celanese Data Governance Owner. Tag is an object designed for performing functional requirements and serving as a specification for equipment.
"""
type TagBeta @view (version: "7#") {
name: String
description: String
aliases: [String]
isActive: Boolean
tagTypes: [TagType]
tagClass: CfihosTagClass
functionalLocation: FunctionalLocation # --> To be deprecated. Use functionalLocations instead
functionalLocations: [FunctionalLocation]
equipment: Equipment # --> To be deprecated. Use equipments instead
equipments: [Equipment]
reportingUnit: ReportingUnit # --> To be deprecated. Use reportingUnits instead
reportingUnits: [ReportingUnit]
}''',
DATA_MODEL,
{ViewId(SPACE, "TagBeta", "7#")},
set(),
id="Type with comments",
),
]

DirectiveTestCases = [
Expand Down

0 comments on commit 827acfd

Please sign in to comment.