Skip to content

Bug: Author fetch fails because Author.Type has the wrong type #1

Description

@SR-G

Encountered some bug and i struggled around it. Below is the summary from AI about reproducing / fixing the bug (which is about Authors / retrieval "ByIdentifier").


Dependency

github.com/raitucarp/openlibrary-go v0.2.1

Description

Open Library's author endpoint returns the type field as an object:

"type": {
  "key": "/type/author"
}

However, the SDK defines Author.Type as a string:

Type string `json:"type"`

As a result, fetching a valid author returns a JSON unmarshalling error:

json: cannot unmarshal object into Go struct field .type of type string

Despite the error, api.Data() is partially populated and contains valid author data, including the author's name.

Reproduction

package main

import (
	"fmt"

	openlibrary "github.com/raitucarp/openlibrary-go"
)

func main() {
	api := openlibrary.NewClient().
		Authors().
		ByIdentifier("/authors/OL23919A")

	err := api.Fetch()

	fmt.Printf("error=%v\n", err)
	fmt.Printf("data=%#v\n", api.Data())
}

The endpoint used by the SDK is:

https://openlibrary.org/authors/OL23919A.json

The endpoint returns a valid response similar to:

{
  "key": "/authors/OL23919A",
  "name": "J. K. Rowling",
  "type": {
    "key": "/type/author"
  }
}

Expected behavior

Fetch() should return nil and decode the author successfully.

The decoded author should be accessible through:

api.Data().Name

Expected value:

J. K. Rowling

Actual behavior

Fetch() returns the following error:

json: cannot unmarshal object into Go struct field .type of type string

The response is nevertheless partially decoded:

api.Data().Key  = "/authors/OL23919A"
api.Data().Name = "J. K. Rowling"

This means consumers cannot rely on err == nil to determine whether the author was successfully retrieved.

Root cause

The SDK currently defines the author type as a string:

type Author struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	Type string `json:"type"`
}

The Open Library API returns the type property as an object:

"type": {
  "key": "/type/author"
}

The JSON structure and the Go model are incompatible.

Suggested fix

Define a compatible type:

type AuthorType struct {
	Key string `json:"key"`
}

Then update the Author structure:

type Author struct {
	Key  string     `json:"key"`
	Name string     `json:"name"`
	Type AuthorType `json:"type"`
}

Alternatively, if the field is not required by the SDK, it could be defined as:

Type json.RawMessage `json:"type"`

or omitted from the model if appropriate.

Additional recommendation

Please check the other SDK models for fields named type. Open Library commonly represents type information as an object containing a key property rather than as a plain string.

For example:

"type": {
  "key": "/type/author"
}

The SDK should model these fields consistently with the current Open Library JSON API responses.

Environment

  • Go version: 1.27.0
  • SDK version: github.com/raitucarp/openlibrary-go v0.2.1
  • Endpoint: https://openlibrary.org/authors/OL23919A.json
  • Author key: /authors/OL23919A

Workaround

Until the SDK is fixed, consumers may still be able to use the partially decoded result:

err := api.Fetch()
data := api.Data()

if data != nil && data.Name != "" {
	fmt.Println(data.Name)
}

if err != nil {
	fmt.Printf("author response was partially decoded: %v\n", err)
}

However, this workaround relies on partial JSON decoding and should not be necessary once the SDK model matches the API response.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions