Forcing a conformant format
codescan derives a JSON-Schema type and format from each Go type. For the
unsized and large integer kinds it emits Go-specific vendor formats —
uint64 → {type: integer, format: uint64}, uint32 → {integer, uint32},
and so on. These round-trip cleanly back to Go, but they are not part of the
Swagger 2.0 format set, and a uint64 value can exceed what a JSON number
safely represents.
When you need conformant, precision-safe output, place a field-level
swagger:strfmt on the field to override just that property’s format. Overriding
to int64 publishes the value as a string-encoded {type: string, format: int64}:
// Measurement forces a JSON-conformant format on a field. A uint64 field emits
// the Go-specific `{integer, format: uint64}` by default; overriding it with a
// field-level `swagger:strfmt int64` (below) yields a precision-safe,
// string-encoded `{string, format: int64}`.
//
// swagger:model
type Measurement struct {
// Raw keeps the default Go-derived vendor format (uint64).
Raw uint64 `json:"raw"`
// Bounded is forced to a conformant, string-encoded int64.
//
// swagger:strfmt int64
Bounded uint64 `json:"bounded"`
}Full source: docs/examples/shaping/formats/formats.go
{
"description": "Measurement forces a JSON-conformant format on a field. A uint64 field emits\nthe Go-specific `{integer, format: uint64}` by default; overriding it with a\nfield-level `swagger:strfmt int64` (below) yields a precision-safe,\nstring-encoded `{string, format: int64}`.",
"type": "object",
"properties": {
"bounded": {
"description": "Bounded is forced to a conformant, string-encoded int64.",
"type": "string",
"format": "int64",
"x-go-name": "Bounded"
},
"raw": {
"description": "Raw keeps the default Go-derived vendor format (uint64).",
"type": "integer",
"format": "uint64",
"x-go-name": "Raw"
}
},
"x-go-package": "github.com/go-openapi/codescan/docs/examples/shaping/formats"
}
Full source: docs/examples/shaping/formats/testdata/measurement.json
Raw keeps the default {integer, uint64} vendor format; Bounded carries
swagger:strfmt int64, so it renders as a string-encoded int64. The override
is per-field — the underlying Go type is untouched everywhere else.
Info
swagger:strfmt also names a custom string format on a type declaration (e.g.
a UUID type → {string, format: uuid}); see
Model definitions → swagger:strfmt.
The swagger:type annotation is the related tool when you want to override the
whole type, not just its format — see
Type discovery and the
swagger:type reference.