Skip to content

Add support for slice of byte as example #379

Closed

Description

@prometherion

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.

Activity

  1. easonlin404 commented on Apr 22, 2019

    @easonlin404
    Member

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

  2. prometherion commented on May 24, 2019

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

  3. ubogdan commented on Jul 8, 2019

    @ubogdan
    Contributor

    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=="`
    }
    
  4. prometherion commented on Jul 16, 2019

    @prometherion
    Author

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

  5. ubogdan commented on Jul 16, 2019

    @ubogdan
    Contributor

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

  6. ubogdan commented on Jul 16, 2019

    @ubogdan
    Contributor

    Closed it by mistake.

  7. prometherion commented on Jul 16, 2019

    @prometherion
    Author

    because I'd like to make requests directly from UI

  8. ubogdan commented on Jul 17, 2019

    @ubogdan
    Contributor

    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 .

  9. prometherion commented on Jul 17, 2019

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

  10. ubogdan commented on Aug 19, 2019

    @ubogdan
    Contributor

    @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=="
                    }
                }
            },
    
    
  11. tomquist commented on May 7, 2020

    @tomquist

    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.

  12. Jake-Convictional commented on Jul 20, 2022

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

  13. ubogdan commented on Jul 21, 2022

    @ubogdan
    Contributor

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

  14. pasquale95 commented on Feb 16, 2024

    @pasquale95

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址