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

Add support for marshalling/unmarshalling JSON #6969

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sdk/azcore/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func (r *Response) HasStatusCode(statusCodes ...int) bool {
// UnmarshalAsJSON calls json.Unmarshal() to unmarshal the received payload into the value pointed to by v.
// If no payload was received a RequestError is returned. If json.Unmarshal fails a UnmarshalError is returned.
func (r *Response) UnmarshalAsJSON(v interface{}) error {
// TODO: verify early exit is correct
if len(r.payload()) == 0 {
return newRequestError("missing payload", r)
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question, to understand the reason behind returning nil, wouldn't it be better to return an error is the payload empty? Since clearly calling the function means that there was the expectation that something would be unmarshaled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my original assumption, however the byte_getNull test expects this to be valid. I actually think the swagger is malformed, i.e. the MIME type should be application/text and not application/json as none of the models are actually JSON. We also have this same behavior in track 1, but again it might be predicated on some bad assumptions (i.e. is there ever a valid success case where the swagger specifies a JSON model but the service is allowed to return null).

}
r.removeBOM()
err := json.Unmarshal(r.payload(), v)
Expand All @@ -78,8 +79,9 @@ func (r *Response) UnmarshalAsJSON(v interface{}) error {
// UnmarshalAsXML calls xml.Unmarshal() to unmarshal the received payload into the value pointed to by v.
// If no payload was received a RequestError is returned. If xml.Unmarshal fails a UnmarshalError is returned.
func (r *Response) UnmarshalAsXML(v interface{}) error {
// TODO: verify early exit is correct
if len(r.payload()) == 0 {
return newRequestError("missing payload", r)
return nil
}
r.removeBOM()
err := xml.Unmarshal(r.payload(), v)
Expand Down
34 changes: 34 additions & 0 deletions sdk/azcore/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,37 @@ func TestResponseUnmarshalJSON(t *testing.T) {
t.Fatal("unexpected value")
}
}

func TestResponseUnmarshalJSONNoBody(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.SetResponse(mock.WithBody([]byte{}))
pl := NewPipeline(srv, NewTelemetryPolicy(TelemetryOptions{}))
resp, err := pl.Do(context.Background(), NewRequest(http.MethodGet, srv.URL()))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := resp.CheckStatusCode(http.StatusOK); err != nil {
t.Fatalf("unexpected status code error: %v", err)
}
if err := resp.UnmarshalAsJSON(nil); err != nil {
t.Fatalf("unexpected error unmarshalling: %v", err)
}
}

func TestResponseUnmarshalXMLNoBody(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.SetResponse(mock.WithBody([]byte{}))
pl := NewPipeline(srv, NewTelemetryPolicy(TelemetryOptions{}))
resp, err := pl.Do(context.Background(), NewRequest(http.MethodGet, srv.URL()))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := resp.CheckStatusCode(http.StatusOK); err != nil {
t.Fatalf("unexpected status code error: %v", err)
}
if err := resp.UnmarshalAsXML(nil); err != nil {
t.Fatalf("unexpected error unmarshalling: %v", err)
}
}