swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.07k stars 6.03k forks source link

Java Codegen Problem #10439

Open agehling opened 4 years ago

agehling commented 4 years ago
Wrong/different types in Java/C# for "type" : "string","format" : "binary" declarations

When we define something like this to transfer binary data we could do it like this:

"transferData" : { "description" : "Any data to be exchanged between callers and callees.", "type" : "string", "format" : "base64" }

The generated code for this is correct for both, C# and Java (both String). – Perfect!

But if we define the following:

"deviceData" : { "type" : "string", "format" : "binary" }

The generated code for C# is correct (public byte[] DeviceData { get; set; }), but for Java a File type is generated (private File deviceData = null;) which is definitely not the intention behind ‘binary’.

3.0.21
The SwaggerHub CodeGen feature for 'java' and 'csharp' were used.
The problematic definition:
      "RequestData" : {
        "description" : "Data to be passed with directive calls.",
        "type" : "object",
        "properties" : {
          "deviceData" : {
            "type" : "string",
            "format" : "binary"
         }        
        }
      }

creates this Java code:

public class RequestData {
  @SerializedName("deviceData")
  private File deviceData = null;

  public RequestData deviceData(File deviceData) {
    this.deviceData = deviceData;
    return this;
  }
...
HugoMario commented 3 years ago

Hey @agehling

Actually, the C# code should also works with a File class. I guess the best approach here is to set an option to force the use of byte arrays.

Wdyt?

agehling commented 3 years ago

Dear HugoMario,

I think in both (probably all) cases it should generate a byte-array, nothing beyond that. - That would much more reflect the intention.

brgds, Andreas

jan-pesta commented 16 hours ago

I agree with @agehling, In documentation https://swagger.io/docs/specification/v3_0/data-models/data-types/#files

is specified that it should be binary content of File, how can be content handled by File type?