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

Query Parameters of type integer in format int64 seem to create a broken spring API #1226

Closed
jbumueller opened this issue Oct 12, 2018 · 4 comments · Fixed by #4969
Closed

Comments

@jbumueller
Copy link

Description

Created a API Spec with a query parameter of type integer, with format int64 which results to Long in Java. Leads to a broken spring Api with thr following signature:

    default ResponseEntity<NotificationSummary> notifications(@ApiParam(value = "The maximum number of Records in the Result", defaultValue = "10l") @Valid @RequestParam(value = "maxResults", required = false, defaultValue="10l") Long maxResults) {

10l should be 10 instead, otherwise a numberFormatException occurs.

openapi-generator version

On the Master (3.3.0)

OpenAPI declaration file content or url
Command line used for generation
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ 
-i .../basicspec.yml  \
-l spring --library spring-mvc \ 
o .../
Steps to reproduce
  • create a spec with the postet yaml
  • run the posted command
  • take a look to the generated code in org/openapitools/api/NotificationsApi.java
Suggest a fix/enhancement

Generate

    default ResponseEntity<NotificationSummary> notifications(@ApiParam(value = "The maximum number of Records in the Result", defaultValue = "10") @Valid @RequestParam(value = "maxResults", required = false, defaultValue="10") Long maxResults) {

instead of

    default ResponseEntity<NotificationSummary> notifications(@ApiParam(value = "The maximum number of Records in the Result", defaultValue = "10l") @Valid @RequestParam(value = "maxResults", required = false, defaultValue="10l") Long maxResults) {
@wing328
Copy link
Member

wing328 commented Oct 19, 2018

@jbumueller is it correct to say that the defaultValue must be a string? In other words, the following is still incorrect:

defaultValue =10l

@jbumueller
Copy link
Author

Well the default value can be a String but it must be parsable for parseInt/parseLong that is if it contains only digits. Therefore 10l is still wrong but 10 would be correct as it can be parsed by Long.parseLong(10).

@stephanpelikan
Copy link
Contributor

stephanpelikan commented Dec 6, 2018

I experienced the same behaviour and viewing my API in swagger-ui gives my this exception:

2018-12-06 12:01:13.339  WARN 12096 --- [nio-8400-exec-2] i.s.m.p.AbstractSerializableParameter    : Illegal DefaultValue -1l for parameter type integer

java.lang.NumberFormatException: For input string: "-1l"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:589)
	at java.lang.Long.valueOf(Long.java:803)
	at io.swagger.models.parameters.AbstractSerializableParameter.getDefault(AbstractSerializableParameter.java:322)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:718)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:639)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:33)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3905)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3219)
	at springfox.documentation.spring.web.json.JsonSerializer.toJson(JsonSerializer.java:38)
	at springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(Swagger2Controller.java:105)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)

I asked myself why the are any suffixes defined at

} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) {
return p.getDefault().toString() + "l";
} else {
return p.getDefault().toString();
}
}
return null;
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
if (SchemaTypeUtil.FLOAT_FORMAT.equals(p.getFormat())) {
return p.getDefault().toString() + "f";
} else {
return p.getDefault().toString() + "d";
}
}
return null;

I do not see in https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md whether this is required (and specified).

Can we remove those suffixes safely? Or (in my case) should be swagger adopted to be able to parse default values using those suffixes?

@jan-berger
Copy link

Is there any workaround for 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.

4 participants