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

Endpoint request body model type mappings #1694

Open
addshore opened this issue Oct 31, 2023 · 0 comments
Open

Endpoint request body model type mappings #1694

addshore opened this issue Oct 31, 2023 · 0 comments

Comments

@addshore
Copy link

Is your feature request related to a problem? Please describe.

I have a struct defined that I use as JSON body input for a handler

type PutPlanInput struct {
	PlanId int32 `json:"planId" binding:"required"`
	RenewOnto Optional[int32] `json:"renewOnto"`
	RenewQuantity Optional[int32] `json:"renewQuantity"`
}

I am making use of an Optional pattern, so that I can determine if the value was 1) set and is null 2) not set
The rest of that code looks like this

type Optional[T any] struct {
	Value   *T
	Defined bool
}

func (t *Optional[T]) UnmarshalJSON(data []byte) error {
	t.Defined = true
	return json.Unmarshal(data, &t.Value)
}

func (t *Optional[T]) IsNullDefined() bool {
	return t.Defined && t.Value == nil
}

This however results in swaggo generating the fallowing swagger spec, which is not actually what I want the user to see.

            "properties": {
                "planId": {
                    "type": "integer"
                },
                "renewOnto": {
                    "allOf": [
                        {
                            "$ref": "#/definitions/api_handlers_devices.Optional-int32"
                        }
                    ]
                },
                "renewQuantity": {
                    "allOf": [
                        {
                            "$ref": "#/definitions/api_handlers_devices.Optional-int32"
                        }
                    ]
                }
            }

Describe the solution you'd like

I'd like some way to override Optional[int32] to actual render as int32 in the swagger output.
I took a look at https://github.com/swaggo/swag#model-composition-in-response and if this were implemented for requests too, I could likely do something like this.

// @Param request body PutPlanInput{renewOnto=int32,renewQuantity=int32} true "Request"

Describe alternatives you've considered

  • There could be some generic type mapping so a user defined list of things could be switched Optional[int32] -> int32 for example
  • Extra annotations on the struct could also enable this mapping?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant