添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account
Description

The generated Java client does not correctly parse the Swagger date format from API responses.

According to the swagger spec a response field of type string, format date must return a full-date defined by RFC 3339 which specifies the format as

full-date = date-fullyear "-" date-month "-" date-mday

This is not supported by the default Java client, which generates using a single date format, used for both date and date-time which is

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

This means simple date fields are not supported and cause the client to throw an InvalidFormatException during the parsing of a response model which contains Date fields..

Swagger-codegen version

2.1.6

Swagger declaration file content or url

Source swagger file is https://github.com/amadeus-travel-innovation-sandbox/sandbox-content/blob/master/swagger.yml

Command line used for generation

java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i https://raw.githubusercontent.com/amadeus-travel-innovation-sandbox/sandbox-content/master/swagger.yml -l java -o /java-client

Steps to reproduce
  • Build java client using above command
  • Register on sandbox.amadeus.com and create a new app to get an API key
  • Create a new unit test using the java client to invoke our hotel API to demonstrate the bug, like so:
  • private static final String APIKEY = "YOUR_API_KEY_HERE"; private static final DefaultApi API = new DefaultApi(); private static final DateTimeFormatter DATE = ISODateTimeFormat.date(); @Test public void testHotelAirportSearch() throws ApiException { LocalDate checkIn = new LocalDate().plusWeeks(6); LocalDate checkOut = checkIn.plusDays(1); HotelSearchResponse hotelSearchRS = API.hotelAirportSearch(APIKEY, "BOS", DATE.print(checkIn), DATE.print(checkOut), 25, "EN", "USD", null, null, 3, false, false, null); assertEquals("US", hotelSearchRS.getResults().get(0).getAddress().getCountry()); assertEquals(checkIn, hotelSearchRS.getResults().get(0).getRooms().get(0).getRates().get(0).getStartDate());
    Related issues

    No noted related issues

    Suggest a Fix

    There are two good approaches to this.
    Either define a date and a dateTime formatter, and use them appropriately, or define a looser date parser for all API responses that can handle any valid ISO Date/Time

    The best way is to use the joda date lib (or JSR310 on java8).
    It has been implemented on feign and retrofit clients (generate with -DdateLibrary=joda)
    When all java clients are done, we will be able to switch the default date lib to joda.

    When generating the Java client, did you use -DdateLibrary=joda as suggested by @cbornet ?

    Which Java HTTP library did you use? default, jersey2, okhttp-gson, etc?