google / go-github

Go library for accessing the GitHub v3 API
https://pkg.go.dev/github.com/google/go-github/v66/github
BSD 3-Clause "New" or "Revised" License
10.44k stars 2.06k forks source link

Getting a User object with null fields fails #9

Closed Kissaki closed 11 years ago

Kissaki commented 11 years ago

The github API v3 declares that blank fields are included with a null as the value instead of being omitted.

For a user which did not specify an organisation, blog or bio the Get returns null as the field values. In go-github this leads to an unmarshalling failure as type string is defined in the struct type User. Unfortunately, null can not be unmarshalled/mapped to string.

A potential solution may be to change the type from string to pointers, allowing nil. But then this should probably be implemented as a general concept across the library, as the API docs state that generally empty fields are returned as null values - and does not seem to note which fields can be empty/null in the end.

willnorris commented 11 years ago

Are you actually seeing these marshaling errors in practice? I've tried writing a sample test that causes an error, but am unable. Could you post an example, either here or in a pull request?

Kissaki commented 11 years ago

A simple script that leads to the unmarshalling error:

package main

import (
    "log"
    githubapi "github.com/google/go-github/github"
)

func main() {
    github := githubapi.NewClient(nil)
    uname := "Kissaki"
    _, err := github.Users.Get(uname)
    if err != nil {
        log.Fatalf("Failed to find user %s. Error: %s\n", uname, err)
    } else {
        log.Printf("Found user %s\n", uname)
    }
}
willnorris commented 11 years ago

running that script, I get:

% go run main.go
2013/06/23 10:23:37 Found user Kissaki

What version of Go are you running? I've tried both 1.0.3 and 1.1, and neither throw an error for me.

Kissaki commented 11 years ago

I’m running a 1.0.3 on Windows 7 x64 here.

Kissaki commented 11 years ago

With Go 1.1.1 on Windows 7 x64 this is no longer an issue. I guess the behaviour changed from Go version 1.0 to 1.1.

So unless functionality for Go 1.0 is a goal here, I’m fine with closing this.

willnorris commented 11 years ago

ah, cool... thanks for following up on this. I'll close this out, but maybe we can start a "known issues" page somewhere, and mention this.