Skip to content

fix: handle description and bio as String or Map in Work/Author models#1

Open
ketanchoyal wants to merge 2 commits into
sauravgpt:masterfrom
ketanchoyal:master
Open

fix: handle description and bio as String or Map in Work/Author models#1
ketanchoyal wants to merge 2 commits into
sauravgpt:masterfrom
ketanchoyal:master

Conversation

@ketanchoyal

@ketanchoyal ketanchoyal commented Mar 2, 2026

Copy link
Copy Markdown

Problem

The OpenLibrary API returns certain text fields in two different formats, but the package models only handle the simple string format.

Affected Fields

  1. Work.description - Returns either:

    { "description": "Simple text" }

    or:

    { "description": { "type": "/type/text", "value": "Structured text" } }
  2. Author.bio - Returns either:

    { "bio": "Simple bio text" }

    or:

    { "bio": { "type": "/type/text", "value": "Structured bio text" } }

Error

When the API returns the object format, the current code throws:

type '_Map<String, dynamic>' is not a subtype of type 'String?' in type cast

This caused:

  • Work fetches to fail completely
  • Author fetches to fail silently (resulting in empty author names in library)

Solution

Added custom JSON converters for both fields:

/// Handles both String and Map formats from OpenLibrary API
String? _descriptionFromJson(dynamic value) {
  if (value == null) return null;
  if (value is String) return value;
  if (value is Map) return value['value'] as String?;
  return null;
}

Applied via @JsonKey(fromJson: _descriptionFromJson) annotation.

Testing

  • ✅ Code compiles without errors
  • flutter analyze passes
  • ✅ Tested with real OpenLibrary API responses

Example API Responses

Impact

  • Breaking Change: No
  • Backward Compatible: Yes
  • Files Changed: work_edition.dart, author.dart + generated files

OpenLibrary API returns description in two formats:
- String: "Simple description text"
- Map: {"type": "/type/text", "value": "Description text"}

Added _descriptionFromJson helper to handle both cases.
Same issue as description - OpenLibrary API returns bio in two formats:
- String: "Simple bio text"
- Map: {"type": "/type/text", "value": "Bio text"}

This was causing author fetches to fail silently, resulting in empty author names.
@ketanchoyal ketanchoyal changed the title fix: handle description as String or Map in Work model fix: handle description and bio as String or Map in Work/Author models Mar 2, 2026
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

Successfully merging this pull request may close these issues.

1 participant