swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for slice of byte as example

prometherion opened this issue · comments

I'm accepting a payload made of a slice of byte ([]byte) but providing the example as string it's not rendered correctly.

Definition of the struct:

type MyBinding struct {
	Crt []byte `json:"crt" example:"-----BEGIN CERTIFICATE-----(...)-----END CERTIFICATE-----"`
	Key []byte `json:"key" example:"-----BEGIN RSA PRIVATE KEY-----(...)-----END RSA PRIVATE KEY-----"`
}

Rendered swagger definition

        "models.MyBinding": {
            "type": "object",
            "properties": {
                "crt": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                },
                "key": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                }
            }
        },

Describe the solution you'd like
I'd like to get the example data already base64 encoded

Describe alternatives you've considered
Maybe using string but for this kind of scenarios (like PKI) it's not so elegant.

Additional context
Nothing to add.

what's your expected output for base64 encoded? would you provide expected swagger json output ?

@easonlin404 sorry for the long wait, lots to do :)

Anyway, I'd like to get something like this:

        "models.MyBinding": {
            "type": "object",
            "properties": {
                "crt": {
                    "type": "string",
                    "format": "base64",
                    "example": "U3dhZ2dlciByb2Nrcw=="
                },
                "key": {
                    "type": "string",
                    "format": "base64",
                    "example": "U3dhZ2dlciByb2Nrcw=="
                }
            }
        },

Just to adhere to Swagger standards regarding string formats

Swagger allready do this. Have you tried ?

type MyBinding struct {
	Crt string `json:"crt" format:"base64" example:"U3dhZ2dlciByb2Nrcw=="`
	Key string `json:"key" format:"base64" example:"U3dhZ2dlciByb2Nrcw=="`
}

I'm not complaining about Swagger but regarding the inability of swaggo to generate it the right way.

@prometherion as your definition is []byte , why do you expect to be type="string"

Closed it by mistake.

because I'd like to make requests directly from UI

I'm going to ask you one more time.
Definition of the struct:

type MyBinding struct {
	Crt []byte `json:"crt" example:"-----BEGIN CERTIFICATE-----(...)-----END CERTIFICATE-----"`
	Key []byte `json:"key" example:"-----BEGIN RSA PRIVATE KEY-----(...)-----END RSA PRIVATE KEY-----"`
}

Why are u declaring slice of byte when you expect string in documentation ?

        "models.MyBinding": {
            "type": "object",
            "properties": {
                "crt": {
                    "type": "string",
                    "format": "base64",
                    "example": "U3dhZ2dlciByb2Nrcw=="
                },
                "key": {
                    "type": "string",
                    "format": "base64",
                    "example": "U3dhZ2dlciByb2Nrcw=="
                }
            }
        },

I guess you know the difference between []byte and string .

@ubogdan my bad in the first comment opening the issue :(
I'd like to get the binding []byte expecting the forma base64 in the Swagger definition.

@prometherion There is a way to do this

type MyBinding struct {
	Crt []byte `json:"crt" swaggertype:"string" format:"base64" example:"U3dhZ2dlciByb2Nrcw=="`
	Key []byte `json:"key" swaggertype:"string" format:"base64" example:"U3dhZ2dlciByb2Nrcw=="`
}

And will produce

        "api.MyBinding": {
            "type": "object",
            "properties": {
                "crt": {
                    "type": "string",
                    "format": "base64",
                    "example": "U3dhZ2dlciByb2Nrcw=="
                },
                "key": {
                    "type": "string",
                    "format": "base64",
                    "example": "U3dhZ2dlciByb2Nrcw=="
                }
            }
        },

I think this should be reopened. Go json marshalling and unmarshalling automatically encodes and decodes a field of type []byte as Base64 string.
See https://golang.org/pkg/encoding/json/#Marshal:

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

I think Swaggo should by default use string as a type for these fields.

@tomquist agreed. Swag labelling []byte type as an integer array is actually a bug. []byte is always marshalled to JSON as a base64 encoded string type.

This is an open source project, anyone is wellcome to improve it.

Have you find a solution for this issue?