When a schema property uses the JSON Schema const keyword directly, the codegen silently drops the constraint and generates a plain field with no indication of the fixed value.
const is a JSON Schema keyword (draft-06+) that OpenAPI adopted when 3.1 aligned its Schema Object with JSON Schema draft 2020-12. The codegen only recognises const inside the oneOf-of-consts enum idiom — it never reads schema.Const from a direct property schema.
This affects any OpenAPI 3.1+ spec. The reproducing example below uses 3.2 but the behaviour is identical with openapi: "3.1.0".
Reproducing Spec
openapi: "3.2.0"
components:
schemas:
ResourceNotFound:
type: object
required: [type, title, status]
properties:
type:
type: string
const: https://example.com/errors/resource-not-found
title:
type: string
const: Resource Not Found
status:
type: integer
const: 404
What is currently generated
type ResourceNotFound struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
}
The const values are silently dropped. Nothing in the generated code enforces or reflects the fixed values.
What should be generated
At minimum, the constant values should be preserved as named Go constants and the fields should be initialised via something like ApplyDefaults()?. Ideally the struct gets a constructor that pre-populates them:
const (
ResourceNotFoundType = "https://example.com/errors/resource-not-found"
ResourceNotFoundTitle = "Resource Not Found"
ResourceNotFoundStatus = 404
)
type ResourceNotFound struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
}
func (s *ResourceNotFound) ApplyDefaults() {
s.Type = ResourceNotFoundType
s.Title = ResourceNotFoundTitle
s.Status = ResourceNotFoundStatus
}
I have a rough idea about a fix for this and up for attempting if you are accepting contributions?
When a schema property uses the JSON Schema
constkeyword directly, the codegen silently drops the constraint and generates a plain field with no indication of the fixed value.constis a JSON Schema keyword (draft-06+) that OpenAPI adopted when 3.1 aligned its Schema Object with JSON Schema draft 2020-12. The codegen only recognisesconstinside theoneOf-of-constsenum idiom — it never readsschema.Constfrom a direct property schema.This affects any OpenAPI 3.1+ spec. The reproducing example below uses 3.2 but the behaviour is identical with
openapi: "3.1.0".Reproducing Spec
What is currently generated
The
constvalues are silently dropped. Nothing in the generated code enforces or reflects the fixed values.What should be generated
At minimum, the constant values should be preserved as named Go constants and the fields should be initialised via something like
ApplyDefaults()?. Ideally the struct gets a constructor that pre-populates them:I have a rough idea about a fix for this and up for attempting if you are accepting contributions?