Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

Spring-MVC: date and date-time format #1235

Closed
glederrey opened this issue Sep 15, 2015 · 17 comments · Fixed by #11153
Closed

Spring-MVC: date and date-time format #1235

glederrey opened this issue Sep 15, 2015 · 17 comments · Fixed by #11153

Comments

@glederrey
Copy link
Contributor

Hi,

I'm working with a Spring MVC stub server.. I have some date and date-time formats too.. I just found that there is a problem when you're giving a date or a date-time format as a parameter in an endpoint..

I may be wrong, but it seems that you need to specify the format of the Date if you want to use it as a parameter.. Therefore, I propose you to add this:

For the date-time format, @DateTimeFormat(pattern="yyyy-MM-dd'T'hh:mm:ss'Z'") if we assume UTC. And for the date format, @DateTimeFormat(pattern="yyyy-MM-dd")..

This should give something like this at the end:

public ResponseEntity<String> endSession(@ApiParam(value = "ID of the session", required = true) @RequestParam(value = "sessionId", required = true) String sessionId, @ApiParam(value = "End time of the session using the format date-time.", required = true) @RequestParam(value = "sessionEnd", required = true) @DateTimeFormat(pattern="yyyy-MM-dd'T'hh:mm:ss'Z'") Date sessionEnd)

Is it clear??

@glederrey
Copy link
Contributor Author

Hi again,

I'm trying to find where the parameters are added in the api.mustache file in order to deal with this problem on my side for the moment.. Does anyone know where this is handled??

Thanks

@wing328
Copy link
Contributor

wing328 commented Sep 18, 2015

@glederrey for the datetime format, I think it's worthwhile to consider the datetime format use by the Java API client: ApiClient.mustache#L60

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

For your 2nd question (where the parameters are added in the api.mustache template), if you look at this line

  public ResponseEntity<{{>returnTypes}}> {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},

{{>queryParams}} means using the template file queryParams.mustache.

In other words, api.mustache is composed of other templates (partial in Ruby on Rails terminology) using the > notation.

@glederrey
Copy link
Contributor Author

@wing328 Thanks a lot for your answer..

So, the thing is that it doesn't work.. I tried with this new format.. And I still get the 400 errors..

For the 2nd question, I understood that.. The thing I'd like to know is where in the JAVA code are these mustache files used.. (My idea would be to change these files and add some if conditions) Do you see what I mean?

@wing328
Copy link
Contributor

wing328 commented Sep 18, 2015

@glederrey For the 400 error, did you get an exception in the server side? Have you used tcpdump or other tool to inspect the request to ensure it's what your API client has sent out?

If I understand correctly, you would like to add @DateTimeFormat(pattern="yyyy-MM-dd'T'hh:mm:ss'Z'") or something similar only if the type is date or dateTime, right ?

@glederrey
Copy link
Contributor Author

@wing328 I didn't get any exception.. Here is the full 400 error:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 400 Bad Request</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /api/emotionadvisor/cloud/v1/webapp/session/end. Reason:
<pre>    Bad Request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>

</body>
</html>

If I add this @DateTimeFormat(pattern="yyyy-MM-dd'T'hh:mm:ss'Z'"), I get the same error if I don't use the right format.. So, I think that's normal.. The Date type in JAVA only accept a specific format..

And that's exactly what I want to do.. =)

@glederrey
Copy link
Contributor Author

@wing328
Hi,
Did you found where I can add this DateTimeFormat in the code to generate the good files??
Thanks

@glederrey
Copy link
Contributor Author

@wing328
Hi again,

I found a very ugly solution to do this.. I added this DateTimeFormat in the queryParams.mustache.. It works because it's "activated" only if the type of parameter is Date.. But I don't like this at all. =)

@anand4sn
Copy link

Hi

I need to generate date format ("format": "date") in swagger json doc for GET parameters. Im using spring rest @RequestParam with @ApiParam() but in @ApiParma we dont have property like dataType then how can i generate format property in json doc . ("format": "date").if someone help on this it will be great help to me. Thanks in advance.

@glederrey
Copy link
Contributor Author

Not from Swagger dev Team

Hi,

What do you mean exactly?? I don't understand your problem.. Is your problem linked to Spring-MVC or to the spec??

@anand4sn
Copy link

Hi @glederrey,

we need to generate "format": "date" json propery for the GET serive parameter like below

{
"name": "toDate",
"in": "query",
"description": "The toDate is ending data range of given dateType value. format should be YYYY-MM-DD.",
"required": false,
"type": "string",
"format": "date"
}

we have this dateType option in @ApiModelProperty(dataType="java.sql.Date",notes="The toDate is ending data range of given dateType value. format should be YYYY-MM-DD.")

but dateType property not in not in @ApiParam? so not able to generate the "format": "date" attibute in json doc??

@anand4sn
Copy link

@glederrey

working- this is in @apimodel -used in POST Method

@ApiModelProperty(dataType="java.sql.Date",notes="The toDate is ending data range of given dateType value. format should be YYYY-MM-DD.")
private String toDate;

not working - this is in method parameter-used for GET method

@ApiParam(value="The toDate is ending data range of given dateType value. format should be YYYY-MM-DD.")
@RequestParam(required=false) Date toDate,

@glederrey
Copy link
Contributor Author

I think you have the same problem that I had..

Here is the trick I used to get rid of this problem. Go there: swagger-codegen/modules/swagger-codegen/src/main/resources/JavaSpringMVC and open the file called queryParams.mustache.. Then, in this file, just before this: {{{dataType}}} {{paramName}}{{/isQueryParam}} (This is the end of the file), you add this: @DateTimeFormat(pattern = "yyyy-MM-dd")

Then, you can regenerate your Spring-MVC server and test it..

@anand4sn
Copy link

@glederrey I already use @DateTimeFormat(pattern = "yyyy-MM-dd") this annotation but not worked.

like below

@ApiParam(value="The toDate is ending data range of given dateType value. format should be YYYY-MM-DD.")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@RequestParam(required=false) Date toDate,

what is mustache? its need any configuration or need to addd queryParams.mustache. file in my classpath?

@glederrey
Copy link
Contributor Author

@anand4sn The mustache files are used to generate the Spring MVC stub server.. I don't know if you're using Swagger Codegen to generate your server or if you're building it from scratch..

In my endpoint definition (in the Spring MVC server), I have something like this: @ApiParam(value = "Time and Date when the session ends. Format: yyyy-MM-ddTHH:mm:ssZ", required = true) @RequestParam(value = "timeEnd", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'") Date timeEnd and it's working..

The only things that changes between your definition and mine is the order of @DateTimeFormat and @RequestParam.. I don't know if the order can change something..

Try changing this order.. If it doesn't fix, you should wait until a guy from Swagger answer you..

@anand4sn
Copy link

@glederrey Many Thanks . Let me try changing the order.

@erreobi
Copy link

erreobi commented Nov 8, 2017

Hi All,

I have a similar problem.

In my swagger the header is defined like the following:
x-CreateDateTime: &responseCreateDateTime
type: string
format: date-time

The generated code is:
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) DateTime xCreateDateTime

We expected that input like the following are accpeted:

  • 2017-11-07T15:44:48+00:00
  • 2017-11-07T15:44:48Z
  • 2017-08-15T14:34:51.934Z
  • 2017-08-15T14:34:51.934+00:00

does it make sense?

Thanks
Rob

@abbasadel
Copy link

any updates on this issue?

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

Successfully merging a pull request may close this issue.

5 participants