datamodel-code-generator
datamodel-code-generator copied to clipboard
Produces invalid Python syntax from some multi-line GraphQL comments
Describe the bug datamodel-code-generator creates invalid Python syntax if multi-line comments are present on some GraphQL items.
This was discovered on the Shopify API and a minimal reproduction is below.
Workaround: Disable the default black formatter (which discovers the syntax error and exits) using datamodel-codegen --formatters isort. This allows outputting the broken Python, so then you can fix 32000 lines of code by hand.
To Reproduce
Example schema:
"""
Test
What's up?
"""
type Type1 {
id: ID!
}
"""
Test
What's up?
"""
type Type2 {
id: ID!
}
"""
Test
What's going on?
"""
union Union1 = Type1 | Type2
Used commandline:
$ poetry run datamodel-codegen --input invalid.graphql --input-file-type graphql --output invalid.py
Expected behavior Should produce:
class Type1(BaseModel):
"""
Test
What's up?
"""
id: ID
typename__: Optional[Literal['Type1']] = Field('Type1', alias='__typename')
class Type2(BaseModel):
"""
Test
What's up?
"""
id: ID
typename__: Optional[Literal['Type2']] = Field('Type2', alias='__typename')
"""
Test
What's going on?
"""
Union1: TypeAlias = Union[
'Type1',
'Type2',
]
Actually produces:
class Type1(BaseModel):
"""
Test
What's up?
"""
id: ID
typename__: Optional[Literal['Type1']] = Field('Type1', alias='__typename')
class Type2(BaseModel):
"""
Test
What's up?
"""
id: ID
typename__: Optional[Literal['Type2']] = Field('Type2', alias='__typename')
# Test
What's going on?
Union1: TypeAlias = Union[
'Type1',
'Type2',
]
Version:
- OS: Arch Linux
- Python version: 3.13
- datamodel-code-generator version: 0.28.4