Skip to content

WeatherAPI Kotlinx Serialization Fails on Missing Fields in Weather Response #435

@coderGtm

Description

@coderGtm

Description:

When using the weather command in the app after #428, deserialization of the API response fails with io.ktor.serialization.JsonConvertException if certain fields are missing from the JSON payload. This is due to the Kotlin data models (Current, Condition, TodayForecast, etc.) having required (non-null, no-default) properties that the API sometimes omits, especially in edge cases or when querying specific endpoints.

Observed Errors:

  • Missing fields in Condition (e.g., icon, code) cause exceptions at $.current.condition.
  • Missing fields in Current (e.g., last_updated_epoch, is_day, wind_degree, etc.) cause exceptions at $.current.
  • Missing astro object in TodayForecast causes exceptions at $.forecast.forecastday[0].

Example Exception:

io.ktor.serialization.JsonConvertException: Illegal input: Fields [icon, code] are required for type with serial name 'com.coderGtm.yantra.commands.weather.Condition', but they were missing at path: $.current.condition

Root Cause:

  • Kotlinx Serialization treats missing required fields as errors.
  • WeatherAPI responses sometimes omit fields, returning only a subset of the expected data.

Impact:

  • Weather command fails and does not return results.
  • Application logs serialization exceptions.

Steps to Reproduce:

  1. Call the weather command with a location or parameters that trigger a minimal or incomplete API response. ( I used weather vadodara )
  2. Observe the exception in logs indicating missing required fields.

Expected Behavior:

  • The weather command should succeed even if some fields are missing in the API response.
  • The command should handle missing data gracefully.

Actual Behavior:

  • The command fails with a serialization exception if any required field is missing.

Solution?
What do you think can be done here @sebastinto ?
I think making the fields nullable with a default value is one solution which I tried.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions