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.08k stars 6.03k forks source link

[Spring] application/octet-stream generate an Objetc File instead of byte[] #8872

Open BenjGue opened 6 years ago

BenjGue commented 6 years ago
Swagger-codegen version

V 3.0.2

Description

I was using swagger 2.0 with mavenplugin-codegen V 2.3.1 : When i have a response type application/octet-stream => Json file : "schema" : { "type" : "string", "format" : "binary"} => Genetares this signature in the Interface API:
@RequestMapping(value = "/addressbooks/export/{addressBookId}", produces = { "application/octet-stream" }, method = RequestMethod.GET) public ResponseEntity<byte[]>

Now i have migrated in openApi3 with mavenplugin-codegen V3.0.2 When i have a response type application/octet-stream => Json file : "content":{"application/octet-stream":{"schema":{"type":"string","format":"binary"}}} => it generates this signature Interface API : @RequestMapping(value = "/addressbooks/export/{addressBookId}",produces = { "application/octet-stream", "application/json" }, method = RequestMethod.GET) default ResponseEntity< File >

The fact that the response is a File instead of byte[] makes spring raise this exception : "org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation"

Steps to reproduce
  1. Create a openAPi3 with this response : responses: '200': content: application/octet-stream: schema: type: string format: binary

  2. Generate the Json file.

  3. Launch the build with codegen 3.0.2 for spring

  4. Check you have a signature => default ResponseEntity< File >

  5. Try to reach this endpoint

  6. Must get: "org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation"

Related issues/PRs
Suggest a fix/enhancement
Tyngstast commented 5 years ago

You probably solved this by now, but for anybody else encountering this issue.

I don't know if there is a fix planned for this, but in the mean time you can add a typeMappings config in you pom.

<configuration>
    .....
    <typeMappings>
        <typeMapping>File=byte[]</typeMapping>
    </typeMappings>
</configuration>
stiwari99 commented 5 years ago

I

You probably solved this by now, but for anybody else encountering this issue.

I don't know if there is a fix planned for this, but in the mean time you can add a typeMappings config in you pom.

<configuration>
    .....
    <typeMappings>
        <typeMapping>File=byte[]</typeMapping>
    </typeMappings>
</configuration>

I am facing similar issue. I am using gradle. is there a way to fix this in gradle

stiwari99 commented 5 years ago

I am using io.swagger.core.v3.swagger-gradle-plugin