OpenCageData / scala-opencage-geocoder

Scala client for the OpenCage forward/reverse geocoding API
Other
4 stars 2 forks source link

Timezone in annotation has string offset while decoder thinks it is an Int #6

Closed louis-byers closed 4 years ago

louis-byers commented 5 years ago

As per https://opencagedata.com/api#response the field results.annontations.timezone.offset_string is returned as a string. In the declaration for the JSON decoder it is listed as an Int.

@ConfiguredJsonCodec case class Timezone(
    name:         String,
    nowInDst:     Boolean,
    offsetSec:    Int,
    offsetString: Int,
    shortName:    String
  )

Which in turn causes this error:

Caused by: com.opencagedata.geocoder.DeserializationError: Failed to deserialize the response from the OpenCage server
    at com.opencagedata.geocoder.OpenCageClient.handleHttpRequestSuccess(OpenCageClient.scala:120)
    at com.opencagedata.geocoder.OpenCageClient.$anonfun$doCall$1(OpenCageClient.scala:77)
    at scala.util.Success.$anonfun$map$1(Try.scala:251)
    at scala.util.Success.map(Try.scala:209)
    at scala.concurrent.Future.$anonfun$map$1(Future.scala:287)
    at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
    at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:29)
    at scala.concurrent.impl.CallbackRunnable.run_aroundBody0(Promise.scala:60)
    at scala.concurrent.impl.CallbackRunnable$AjcClosure1.run(Promise.scala:1)
    at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149)
Caused by: io.circe.DecodingFailure$$anon$2: Int: DownField(offset_string),DownField(timezone),DownField(annotations),DownArray,DownField(results)

For the request to: https://api.opencagedata.com/geocode/v1/json?q=38.87789,-77.02259&key=xxxxxxxxxxxxxxxxxxxx

With a result of:


    "documentation": "https://opencagedata.com/api",
    "licenses": [
        {
            "name": "see attribution guide",
            "url": "https://opencagedata.com/credits"
        }
    ],
    "rate": {
        "limit": 2500,
        "remaining": 2498,
        "reset": 1570320000
    },
    "results": [
        {
            "annotations": {
                "DMS": {
                    "lat": "38° 52' 39.61704'' N",
                    "lng": "77° 1' 21.93060'' W"
                },
                "FIPS": {
                    "county": "11001",
                    "state": "11"
                },
                "MGRS": "18SUJ2453605146",
                "Maidenhead": "FM18lv70gp",
                "Mercator": {
                    "x": -8574134.255,
                    "y": 4677340.956
                },
                "OSM": {
                    "edit_url": "https://www.openstreetmap.org/edit?way=722495228#map=16/38.87767/-77.02276",
                    "url": "https://www.openstreetmap.org/?mlat=38.87767&mlon=-77.02276#map=16/38.87767/-77.02276"
                },
                "UN_M49": {
                    "regions": {
                        "AMERICAS": "019",
                        "NORTHERN_AMERICA": "021",
                        "US": "840",
                        "WORLD": "001"
                    },
                    "statistical_groupings": [
                        "MEDC"
                    ]
                },
                "callingcode": 1,
                "currency": {
                    "alternate_symbols": [
                        "US$"
                    ],
                    "decimal_mark": ".",
                    "disambiguate_symbol": "US$",
                    "html_entity": "$",
                    "iso_code": "USD",
                    "iso_numeric": "840",
                    "name": "United States Dollar",
                    "smallest_denomination": 1,
                    "subunit": "Cent",
                    "subunit_to_unit": 100,
                    "symbol": "$",
                    "symbol_first": 1,
                    "thousands_separator": ","
                },
                "flag": "🇺🇸",
                "geohash": "dqcjpm3wzehx5syrjs2d",
                "qibla": 56.57,
                "roadinfo": {
                    "drive_on": "right",
                    "speed_in": "mph"
                },
                "sun": {
                    "rise": {
                        "apparent": 1570273620,
                        "astronomical": 1570268340,
                        "civil": 1570272060,
                        "nautical": 1570270200
                    },
                    "set": {
                        "apparent": 1570315380,
                        "astronomical": 1570234260,
                        "civil": 1570317000,
                        "nautical": 1570318800
                    }
                },
                "timezone": {
                    "name": "America/New_York",
                    "now_in_dst": 1,
                    "offset_sec": -14400,
                    "offset_string": "-0400",
                    "short_name": "EDT"
                },
                "what3words": {
                    "words": "rock.served.treat"
                }
            },
            "bounds": {
                "northeast": {
                    "lat": 38.8780685,
                    "lng": -77.0217203
                },
                "southwest": {
                    "lat": 38.8768485,
                    "lng": -77.0232738
                }
            },
            "components": {
                "ISO_3166-1_alpha-2": "US",
                "ISO_3166-1_alpha-3": "USA",
                "_type": "neighbourhood",
                "city": "Washington",
                "continent": "North America",
                "country": "USA",
                "country_code": "us",
                "neighbourhood": "Southwest Waterfront",
                "postcode": "20024",
                "state": "District of Columbia",
                "state_code": "DC",
                "suburb": "Penn Quarter",
                "unknown": "Water Street Southwest"
            },
            "confidence": 9,
            "formatted": "Water Street Southwest, Washington, DC 20024, United States of America",
            "geometry": {
                "lat": 38.8776714,
                "lng": -77.0227585
            }
        }
    ],
    "status": {
        "code": 200,
        "message": "OK"
    },
    "stay_informed": {
        "blog": "https://blog.opencagedata.com",
        "twitter": "https://twitter.com/opencagedata"
    },
    "thanks": "For using an OpenCage API",
    "timestamp": {
        "created_http": "Sat, 05 Oct 2019 19:35:03 GMT",
        "created_unix": 1570304103
    },
    "total_results": 1
}```
louis-byers commented 5 years ago

Pull request submitted

louis-byers commented 4 years ago

It looks like this only happens when the offset is a negative number. It can be presented as a negative number with a leading 0. e.g. "-0500"