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

Maintain original keys/fields ordering #2456

Open bangbaew opened 8 months ago

bangbaew commented 8 months ago

I'm trying to convert ISO20022 schemas to C#, Go and Rust code, which ordering of fields is important, but the code generator seems to generate the fields in alphabetical order, it should be in the original order.

This is a portion of my schema:

"GroupHeader32": {
      "type": "object",
      "properties": {
        "MsgId": {
          "$ref": "#/definitions/Max35Text"
        },
        "CreDtTm": {
          "$ref": "#/definitions/ISODateTime"
        },
        "Authstn": {
          "maxItems": 2,
          "type": "array",
          "items": {
            "$ref": "#/definitions/Authorisation1Choice"
          }
        },
        "NbOfTxs": {
          "$ref": "#/definitions/Max15NumericText"
        },
        "CtrlSum": {
          "$ref": "#/definitions/DecimalNumber"
        },
        "InitgPty": {
          "$ref": "#/definitions/PartyIdentification32"
        },
        "FwdgAgt": {
          "$ref": "#/definitions/BranchAndFinancialInstitutionIdentification4"
        }
      },
      "required": ["MsgId", "CreDtTm", "NbOfTxs", "InitgPty"],
      "additionalProperties": false
    },

This is the generated C# code:

public partial class GroupHeader32
    {
        public Authorisation1Choice[] Authstn { get; set; }
        public DateTimeOffset CreDtTm { get; set; }
        public double? CtrlSum { get; set; }
        public BranchAndFinancialInstitutionIdentification4 FwdgAgt { get; set; }
        public PartyIdentification32 InitgPty { get; set; }
        public string MsgId { get; set; }
        public string NbOfTxs { get; set; }
    }

This is the generated Go code:

type GroupHeader32 struct {
    Authstn  []Authorisation1Choice                        `json:"Authstn,omitempty"`
    CreDtTm  string                                        `json:"CreDtTm"`
    CtrlSum  *float64                                      `json:"CtrlSum,omitempty"`
    FwdgAgt  *BranchAndFinancialInstitutionIdentification4 `json:"FwdgAgt,omitempty"`
    InitgPty PartyIdentification32                         `json:"InitgPty"`
    MsgID    string                                        `json:"MsgId"`
    NbOfTxs  string                                        `json:"NbOfTxs"`
}

This is the generated Rust code:

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct GroupHeader32 {
    authstn: Option<Vec<Authorisation1Choice>>,
    cre_dt_tm: String,
    ctrl_sum: Option<f64>,
    fwdg_agt: Option<BranchAndFinancialInstitutionIdentification4>,
    initg_pty: PartyIdentification32,
    msg_id: String,
    nb_of_txs: String,
}

Is there any option to maintain the properties in original order?

sschuberth commented 5 months ago

Looks like this duplicates https://github.com/glideapps/quicktype/issues/898 / https://github.com/glideapps/quicktype/issues/336.