0

I use go-swagger. I have model like:

// Pet pet
// swagger:model
type Pet struct {
    // id
    ID int64 `json:"id,omitempty"`
    // name
    // Required: true
    Name *string `json:"name"`
}

and for example in my POST field Name have to be required but for GET not. So how should I add it if I don't want create similar model without required? Because for now my only thought is to create type PetGET struct and type PetPOST struct which, I guess is stupid idea.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Bubuu
  • 1
  • 3

1 Answers1

0

If you're generating spec from code, then having a PetGET and PetPOST is not a stupid idea. Usually the program model doesn't match the API model, and you have to repeat definitions just to generate the spec right. So you have a couple of options here:

You can simply mark the field required: false, and deal with it in the GET handler.

If you're generating spec from code, you can create swagger-only models separate from your program models, and for cases like this, create two models with different annotations.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • And if now I want `default` only for **PUT** I have to create another model? – Bubuu Dec 03 '19 at 21:00
  • If you want *that* level of precision in your model, then yes. Or, use the model for GET, and define the body inline for PUT/POST without a separate model struct. – Burak Serdar Dec 03 '19 at 21:03