-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Cannot make converter from byte[] to String #693
Comments
Instead of using a converter, have you tried the following? @ApiModelProperty(value = "base64 encoded string", dataType="string", notes = "base64 string", required = true)
private byte[] myBytes; I'm not entirely sure it'll work with |
Yes I've already tried that. It doesn't work. It produces a Scala match error "scala.MatchError: string (of class java.lang.String)" (Using just byte in combination with dataType="string" works fine and the output is a string. It's the byte[] that I can't make work.) |
You've tried all 3 variants? |
Sorry, what are the three variants? Yeah I can try changing the dependency. |
Well, you can try |
I don't believe you can override datatypes in model properties, period. |
Per @webron's comment below, I was mistaken. |
Just tested it and it works fine. I've used @magnusar - if it doesn't work for you, could you please produce a test case I can check locally? |
@webron && @fehguy : I'm not surprised that the annotation worked for long -> String, since it worked for me with byte -> String. The problem is the array primitive. So if you can make anything primitive[] working (preferably byte[] since that is my case) that would probably solve it. If I end up having to patch the code myself, do you please have any pointers as to how I would go about making a converter work for arrays of Java primitives? (I'm totally new to Scala...) Thanx for the feeback. |
Okay, I did some further testing. The problem is not related with primitive arrays but rather with arrays or anything that translates to an array. The type can be a primitive or a class, and the array could be a java array or the collections we parse as arrays (List, Set...). And this is a bug the way I see it. @fehguy - could you look into that please? |
it is impossible to support all overrides in this fashion. It's a string, and to read the annotation, we're instantiating the class. So "List[String]" is not only invalid syntax for java, but impossible to instantiate because List isn't a concrete class. I suggest we document the behavior, add support for overrides in the property parsing, and start using concrete classes for overrides rather than string representations. |
With swagger-core 1.5 I don't think it's relevant anymore, but I'm not sure follow everything you say here. @ApiModelProperty(dataType="string")
private byte[] myBytes; vs @ApiModelProperty(dataType="string")
private long date; The first sample doesn't work, the second works fine. |
As an input to the discussion, when I look at the response body where we also have a byte[] it looks like this in the Swagger UI if I click the Model link: MyClass { So "array[byte]" is used as syntax at least in the output. I don't know if that's relevant but that's why I tried "converter.add("array[byte]", jsonString);" as stated in the issue. |
We absolutely need to make the byte[] => string conversion work. (In the worst case I will have to try and patch the code myself or we have to choose some other framwork.) Do you think you guys could add a fix for this in the near future? |
@magnusar - there is a workaround but that would require you to duplicate your model and may add a few additional annotations for the documentation. It may be enough for you for now. |
@webron could you please give me an example of that workaround and what extra annotations I should add? |
It's a bit difficult since I don't know how you use your model (as a body parameter, response type..). For starters, you'd have to create a copy of the class and replace the |
Ok, here's a simplified version of the model: MyView.java: @DaTa @ApiModelProperty(value = "a name", required = true) @ApiModelProperty(value = "some text", dataType="", notes = "base64 string", required = true) And then we use it like this: @post
} |
Okay, so you'd have something like this: @ApiModel(value = "MyView")
public class MySwaggerView {
@ApiModelProperty(value = "a name", required = true)
private String name;
@ApiModelProperty(value = "some text", notes = "base64 string", required = true)
private String byteArray;
} And: @POST
@Path("/someservice")
@ApiOperation(value = "bla, bla",
notes = "bla bla",
response = MySwaggerView.class)
public MyView doRequest(@ApiParam(value = "Parameters for ...", required = true) InputObject inputObject) {
return getNewMyViewInstance(inputObject);
} (I'm ignoring the fact that in the sample there are only private fields with no setters) |
Yeah, that works. Thanks! (The setters are generated automatically by the Lombok annotation @DaTa.) I'm not sure this duplication of model classes will be acceptable as a solution but perhaps we can use it temporarily. Do you know if we can expect a fix for this issue in a future snapshot release of Swagger? |
As said it's a workaround. I can't comment as to if or when a different solution will be available. |
OK I've dug into this--you cannot change a model property with an override converter from an array to a simple type like string. I understand where it may be desirable but I think this is more of a design challenge in the API than a configuration in swagger. You can do this:
but the model will be this:
I have fixed the error message to not be so opaque, though, as you first reported: |
The examples for how to use an OverrideConverter for Calendar objects, for example described here #359 works fine for us.
But I'm unable to make a converter work for the type byte[] that outputs it as a String. How can this be achieved? The following does not work:
Our property looks like this:
@ApiModelProperty(value = "base64 encoded string", notes = "base64 string", required = true)
private byte[] myBytes;
We are using com.wordnik:swagger-jaxrs_2.11:1.3.10 in a Dropwizard project.
The text was updated successfully, but these errors were encountered: