swaggest / openapi-go

OpenAPI structures for Go
https://pkg.go.dev/github.com/swaggest/openapi-go/openapi3
MIT License
223 stars 18 forks source link

The OAS definition produced for []bytes seems to be wrong #111

Closed Keithwachira closed 3 days ago

Keithwachira commented 1 month ago

Describe the bug If you have []bytes as one of your field in the OAS it is represented as an array

At the moment as a work arounf I am using PrepareJSONSchema to change the generated output.

Sample struct

type Asset struct {
    DBID        ObjectId  `bson:"_id,omitempty" json:"_id,omitempty" gorm:"primaryKey;column:_id" example:"664c9b735715ec3f798bbcd2"`
    ID          string    `bson:"id" json:"id" example:"my-unique-template-id"`
    Data        []byte    `bson:"data" json:"data"`

}

What is produced in the OAS:

  Asset:
      properties:
        _id:
          example: 664c9b735715ec3f798bbcd2
          type: string
        data:
          items:
            minimum: 0
            type: integer
          nullable: true
          type: array

Expected behavior I would expect it to have produced a string with binary format like below:

data:
         type: string
         format: byte
vearutop commented 3 days ago

Indeed, the definition of []byte was wrong. The behavior of enconding/json for []byte is base64 string, this behavior was applied to schema in v0.2.52.

vearutop commented 3 days ago

Please feel free to reopen if current behavior still needs adjustment.