Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for slice of byte as example #379

Closed
prometherion opened this issue Apr 17, 2019 · 14 comments
Closed

Add support for slice of byte as example #379

prometherion opened this issue Apr 17, 2019 · 14 comments

Comments

@prometherion
Copy link

prometherion commented Apr 17, 2019

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.

@easonlin404
Copy link
Member

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

@prometherion
Copy link
Author

@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

@ubogdan
Copy link
Contributor

ubogdan commented Jul 8, 2019

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=="`
}

@prometherion
Copy link
Author

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

@ubogdan
Copy link
Contributor

ubogdan commented Jul 16, 2019

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

@ubogdan ubogdan closed this as completed Jul 16, 2019
@ubogdan ubogdan reopened this Jul 16, 2019
@ubogdan
Copy link
Contributor

ubogdan commented Jul 16, 2019

Closed it by mistake.

@prometherion
Copy link
Author

because I'd like to make requests directly from UI

@ubogdan
Copy link
Contributor

ubogdan commented Jul 17, 2019

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 .

@prometherion
Copy link
Author

@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.

@ubogdan
Copy link
Contributor

ubogdan commented Aug 19, 2019

@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=="
                }
            }
        },

@tomquist
Copy link

tomquist commented May 7, 2020

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.

@Jake-Convictional
Copy link
Contributor

@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.

@ubogdan
Copy link
Contributor

ubogdan commented Jul 21, 2022

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

@pasquale95
Copy link

Have you find a solution for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants