Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty Slice/Array throwing an compilation error and UnmarshalJSON is checking for all the attribute should be present #203

Open
shysudo opened this issue May 5, 2023 · 1 comment

Comments

@shysudo
Copy link

shysudo commented May 5, 2023

We upgraded from version v7.3.1 to v10.2.1 facing following issues

  1. When fields is empty slice(array) Code generation causing an compilation issue.
    {
    "type": "record",
    "name": "EquipmentDiscoveryStarted",
    "namespace": "io.leitstand.actor.events.controllers",
    "fields": []
    }

    `func (r EquipmentDiscoveryStarted) MarshalJSON() ([]byte, error) {
    var err error // (unused variable error)
    output := make(map[string]json.RawMessage)
    return json.Marshal(output)
    }

    func (r *EquipmentDiscoveryStarted) UnmarshalJSON(data []byte) error {
    var fields map[string]json.RawMessage
    if err := json.Unmarshal(data, &fields); err != nil {
    return err
    }

    var val json.RawMessage // (unused variable error)
    return nil
    }`

  2. UnmashalJSON method is causing an issue for checking of all the attribute(struct) throwing error, if not provided value for it, all the attribute become mandatory.
    Example : `func (r *GfastPortReason) UnmarshalJSON(data []byte) error {
    var fields map[string]json.RawMessage
    if err := json.Unmarshal(data, &fields); err != nil {
    return err
    }

    var val json.RawMessage
    val = func() json.RawMessage {
    if v, ok := fields["reason"]; ok {
    return v
    }
    return nil
    }()

    if val != nil {
    if err := json.Unmarshal([]byte(val), &r.Reason); err != nil {
    return err
    }
    } else {
    return fmt.Errorf("no value specified for reason")
    }
    val = func() json.RawMessage {
    if v, ok := fields["description"]; ok {
    return v
    }
    return nil
    }()

    if val != nil {
    if err := json.Unmarshal([]byte(val), &r.Description); err != nil {
    return err
    }
    } else {
    return fmt.Errorf("no value specified for description")
    }
    return nil
    }`

@tanis2000
Copy link
Contributor

Hey Gireesh, issue number one has been fixed in this PR (which has not yet been merged): #202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants