glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
11.93k stars 1.05k forks source link

Generated Python Dataclasses from a JSON schema not valid (non-default argument follows default argument) #2448

Open bertcornelissen opened 9 months ago

bertcornelissen commented 9 months ago

When I generate a Python (>=3.7) dataclass from a json schema, the class members are are listed alphabetically. When an optional field (= 'not required') is used, this field gets a default value. No problem but these fields should placed at the end of the dataclass definition:

In next example 'approval_code' is optional and the dataclass that is being generated is as follows:

@dataclass class Ifsf1200: additional_data_private: str aiid: int amount_transaction: str approval_code: Optional[int] = None # THIS IS NOT CORRECT !! bitmap_primary: str bitmap_secondary: str card_acceptor_business_code: int card_acceptor_id_code: str

Looks like it only happens with :int 's !!

Hope I missed some option to avoid this. Used you software before with great success (Thanks)

Bert Cornelissen

bertcornelissen commented 9 months ago

I found out what caused it:

In the schema:
"approval_code": { "type": "string", "format": "integer" },

If I change it to :+1: "approval_code": { "type": "integer", },

it works.

(Schemas were generated from a json file into a Json schema with Quicktype.

Maybe no bug...

Bert Cornelissen