swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.46k stars 1.19k forks source link

fix enum field #1772

Closed qingtao closed 5 months ago

qingtao commented 6 months ago

Describe the PR

The fileld which type is enum, use oneOf not allOf, when it has ref value.

Relation issue

https://github.com/swaggo/swag/issues/1524

Additional context

we has code like this:


type Operator string

const (
    OperatorEq Operator = "eq"
    OperatorGt Operator = "gt"
    OperatorGte Operator = "gte"
    OperatorLt Operator = "lt"
    OperatorLte Operator = "lte"
    OperatorIn Operator = "in"
    OperatorRange Operator = "range"
    OperatorLike Operator = "like"
    OperatorHasPrefix Operator = "has_prefix"
)

// DemoQueryPageReq query params with pagination
type DemoQueryPageReq struct {
    // 1-N
    Current int `json:"current,omitempty" form:"current" binding:"required,min=1"`
    // 1-100
    PageSize int `json:"pageSize,omitempty" form:"pageSize" binding:"required,min=1,max=100"`
    // params to filter record
    DemoQueryReq
        // order option
    Order OrderOption `json:"order"`
}

// DemoQueryReq the params to query demo records
type DemoQueryReq struct {
    Code struct {
        Op     Operator   `json:"op" binding:"oneof=eq in"`
        Values []string `json:"values"`
    } `json:"code"`
    Value struct {
        Op     string   `json:"op" binding:"oneof=eq like has_prefix in"`
        Values []string `json:"values"`
    } `json:"value"`
}

use swag gen:

swag init -g internal/server/http/doc.go \
    --exclude internal/server/http/ctl.go \
    -outputTypes="json,yaml" \
    -o docs/openapi/

before:

"schema.DemoQueryPageReq": {
            "type": "object",
            "required": [
                "current",
                "pageSize"
            ],
            "properties": {
                "code": {
                    "description": "编码",
                    "type": "object",
                    "properties": {
                        "op": {
                            "enum": [
                                "eq",
                                "in"
                            ],
                            "allOf": [
                                {
                                    "$ref": "#/definitions/schema.Operator"
                                }
                            ]
                        },
                        "values": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    }
                },
                "current": {
                    "description": "分页页码, 可用范围[1-N]",
                    "type": "integer",
                    "minimum": 1
                },
                "order": {
                    "$ref": "#/definitions/schema.OrderOption"
                },
                "pageSize": {
                    "description": "分页数量, 可用范围[1-100]",
                    "type": "integer",
                    "maximum": 100,
                    "minimum": 1
                },
                "value": {
                    "description": "值",
                    "type": "object",
                    "properties": {
                        "op": {
                            "enum": [
                                "eq",
                                "like",
                                "has_prefix",
                                "in"
                            ],
                            "allOf": [
                                {
                                    "$ref": "#/definitions/schema.Operator"
                                }
                            ]
                        },
                        "values": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    }
                }
            }

after:

"schema.DemoQueryPageReq": {
            "type": "object",
            "required": [
                "current",
                "pageSize"
            ],
            "properties": {
                "code": {
                    "description": "编码",
                    "type": "object",
                    "properties": {
                        "op": {
                            "enum": [
                                "eq",
                                "in"
                            ],
                            "oneOf": [
                                {
                                    "$ref": "#/definitions/schema.Operator"
                                }
                            ]
                        },
                        "values": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    }
                },
                "current": {
                    "description": "分页页码, 可用范围[1-N]",
                    "type": "integer",
                    "minimum": 1
                },
                "order": {
                    "$ref": "#/definitions/schema.OrderOption"
                },
                "pageSize": {
                    "description": "分页数量, 可用范围[1-100]",
                    "type": "integer",
                    "maximum": 100,
                    "minimum": 1
                },
                "value": {
                    "description": "值",
                    "type": "object",
                    "properties": {
                        "op": {
                            "enum": [
                                "eq",
                                "like",
                                "has_prefix",
                                "in"
                            ],
                            "oneOf": [
                                {
                                    "$ref": "#/definitions/schema.Operator"
                                }
                            ]
                        },
                        "values": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
sdghchj commented 5 months ago

You have to make sure swagger 2.0 support 'oneof'. On the other hand, you dropped the 'description' of the field itself.

qingtao commented 5 months ago

You have to make sure swagger 2.0 support 'oneof'. On the other hand, you dropped the 'description' of the field itself.

I checked the v2 branch and Swagger used spec As an anonymous field, Scheme contains OneOf. I don't know if this means it? I used Redoc to display documents, which can correctly display Enums constraints and display referenced custom type constants.

sdghchj commented 5 months ago

Maybe you can commit PR to v2 branch. The master branch currently follows OpenAPI Specification v2. Try swagger editor2.0.