voltbras / go-ocpp

v1.5 and v1.6 OCPP implementation in Golang
GNU General Public License v3.0
46 stars 7 forks source link

.Send method does not omit empty values at is should #8

Open EstoyAburrido opened 3 years ago

EstoyAburrido commented 3 years ago

Hi. I'm trying to send a request as follows:

    rawResp, err := st.Send(&cpreq.StopTransaction{
        TransactionId:  ocppVars.TransactionId[id],
        MeterStop:      int(ocppVars.Connectors[id].Consumed),
        Timestamp:      time.Now(),
        Reason:         "Remote",
    })

And StopTransaction struct has 'omitempty' json parameters on each non-required field but when I send the request the server(which is not based on the same library) gets this:

"StopTransaction",{"idTag":"","meterStop":13740,"reason":"Remote","timestamp":"2020-12-03T20:41:33+03:00","transactionData":null,"transactionId":1}

It complains about transactionData and replying with the following error message:

Error: Invalid value "null" for field transactionData

As you can see all the other optional fields are also included in the query.

Any idea how do I send the request omitting optional values ?

andig commented 3 years ago

Try json serializing the response type to see if it contains the expected fields and search further from there.

EstoyAburrido commented 3 years ago

If I manually remove empty fields in UnmarshalRequest at https://github.com/voltbras/go-ocpp/blob/master/ws/message.go everything works as expected.

EstoyAburrido commented 3 years ago

removing the file go-ocpp/messages/v1x/cpreq/json_encode.go also fixes all the problems, I have no idea what was this file meant to do but all it does is adding empty values to the query instead of omitting them

eduhenke commented 3 years ago

The json_encode file was auto-generated based on the OCPP JSON Schema, it also validates required properties, timestamp formats, etc. I also see that it doesn't correctly handle empty fields(even though it is annotated). Howerver, that file is still useful because, as I said, when unmarshalling, it validates all those JSON Schema constraints defined per action, however it does seem that we could remove the MarshalJSON functions, as the default MarshalJSON implementation seems sufficient and correct. I just feel that we needed to have this tested a bit more, before I can say that this change wouldn't break anything.

Also, please feel free to contribute to this library! Any help is appreciated!