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

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

Open
EstoyAburrido opened this issue Dec 3, 2020 · 4 comments
Open

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

EstoyAburrido opened this issue Dec 3, 2020 · 4 comments

Comments

@EstoyAburrido
Copy link

EstoyAburrido commented Dec 3, 2020

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
Copy link

andig commented Dec 3, 2020

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

@EstoyAburrido
Copy link
Author

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
Copy link
Author

EstoyAburrido commented Dec 5, 2020

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
Copy link
Member

eduhenke commented Dec 5, 2020

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!

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

3 participants