How to define byte array in OpenAPI 3.0
Asked Answered
D

1

20

I'm migrating my API from Swagger 2.0 to OpenAPI 3.0. In a DTO I have a field specified as a byte array. Swagger definition of the DTO:

Job:
   type: object
   properties:
       body:
         type: string
         format: binary

Using the definition above the swagger code generator generates an object that accepts byte[] array as the body field new Job().setBody(new byte[1]).

After converting the API definition to OpenAPI the definition for that object stayed the same but the openapi code generator now requires org.springframework.core.io.Resource instead of byte[] (new Job().setBody(org.springframework.core.io.Resource)). There are some places in my code where I have to serialize the Job object but it's no longer possible because Resource doesn't implement serializable.

As a workaround I changed the type to object:

Job:
   type: object
   properties:
       body:
         type: object

Now I have to cast the body to String and then convert to byte[] everywhere and I'd rather have the type as byte[] as it was before.

How can I specify the type as byte[] using OpenAPI 3.0?

Destroyer answered 8/7, 2020 at 12:35 Comment(0)
B
20

You must set type: string and format: byte

Original answer: when using swagger codegen getting 'List<byte[]>' instead of simply 'byte[]'

Bergamot answered 4/9, 2020 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.