Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,14 @@ How the specification maps onto Go:
Nothing the specification leaves open decodes into `any` then: the empty
schema, an array without `items`, a free-form object and `not` alone become
`struct{}`, so any value in them is recorded, and any but an object fails.
- **Fields** — a field is a pointer only where its zero value must be told apart
from something else: from leaving the field out, if it is optional, or from
null, if it is nullable. That is a boolean, a number that may be 0, or an object
that requires nothing, so `{}` says something; nil then leaves the field out,
and a pointer to the zero value sends it. A zero value the specification makes
the default, or rules out with a bound or an enum, needs no pointer. A string,
a slice, a map or a union is never a pointer: its zero value leaves it out. A
time or a date is a pointer only if it is nullable, since its zero value is
written as a time, not as null. A struct that would contain itself refers to
itself through a pointer.
- **Fields** — a field is a pointer only where it is optional and its zero value
must be told apart from leaving it out. That is a boolean, a number that may
be 0, or an object that requires nothing, so `{}` says something; nil then
leaves the field out, and a pointer to the zero value sends it. A zero value
the specification makes the default, or rules out with a bound or an enum,
needs no pointer. A string, a time, a date, a slice, a map or a union is never
a pointer: its zero value leaves it out. A struct that would contain itself
refers to itself through a pointer.
- **Omitting** — an optional field is tagged `omitzero`, so it is left out while
it holds its zero value: nil for a pointer, a slice or a map, so an empty one
is still sent. A required field is always sent, `""`, `0` and `false`
Expand All @@ -160,10 +158,12 @@ How the specification maps onto Go:
write-only members out of a response, as their fields hold them or not, through
unions and embedded parts alike; any other encoding keeps them.
- **Null** — a schema that is only ever `null` is `*struct{}`, and "X or null",
a `type` of `[X, "null"]` or a `oneOf` or `anyOf` of `X` and `null`, is `X`, a
pointer to `X` only by the rule for fields above, so that null and
the zero value can differ, or where `X` decodes itself, such as a union or a
tuple, which would refuse null.
a `type` of `[X, "null"]` or a `oneOf` or `anyOf` of `X` and `null`, is `X`:
null reads as its zero value, and a required field sends the zero value where
it would have sent null. A union reads null as no alternative and writes an
unset union as null. `X` is a pointer only by the rule for fields above,
where it is a tuple, which would refuse null, or where it is an object that
requires a union, whose zero value would write that member as null.
- **Fixed parameters** — a required parameter that can take only one value, its
`const` or the only value of its `enum`, is sent by the client itself: in the
path, the query or the headers, such as Notion's `Notion-Version`. The caller
Expand Down
28 changes: 14 additions & 14 deletions README/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ How the specification maps onto Go:
Nothing the specification leaves open decodes into `any` then: the empty
schema, an array without `items`, a free-form object and `not` alone become
`struct{}`, so any value in them is recorded, and any but an object fails.
- **Fields** — a field is a pointer only where its zero value must be told apart
from something else: from leaving the field out, if it is optional, or from
null, if it is nullable. That is a boolean, a number that may be 0, or an object
that requires nothing, so `{}` says something; nil then leaves the field out,
and a pointer to the zero value sends it. A zero value the specification makes
the default, or rules out with a bound or an enum, needs no pointer. A string,
a slice, a map or a union is never a pointer: its zero value leaves it out. A
time or a date is a pointer only if it is nullable, since its zero value is
written as a time, not as null. A struct that would contain itself refers to
itself through a pointer.
- **Fields** — a field is a pointer only where it is optional and its zero value
must be told apart from leaving it out. That is a boolean, a number that may
be 0, or an object that requires nothing, so `{}` says something; nil then
leaves the field out, and a pointer to the zero value sends it. A zero value
the specification makes the default, or rules out with a bound or an enum,
needs no pointer. A string, a time, a date, a slice, a map or a union is never
a pointer: its zero value leaves it out. A struct that would contain itself
refers to itself through a pointer.
- **Omitting** — an optional field is tagged `omitzero`, so it is left out while
it holds its zero value: nil for a pointer, a slice or a map, so an empty one
is still sent. A required field is always sent, `""`, `0` and `false`
Expand All @@ -115,10 +113,12 @@ How the specification maps onto Go:
write-only members out of a response, as their fields hold them or not, through
unions and embedded parts alike; any other encoding keeps them.
- **Null** — a schema that is only ever `null` is `*struct{}`, and "X or null",
a `type` of `[X, "null"]` or a `oneOf` or `anyOf` of `X` and `null`, is `X`, a
pointer to `X` only by the rule for fields above, so that null and
the zero value can differ, or where `X` decodes itself, such as a union or a
tuple, which would refuse null.
a `type` of `[X, "null"]` or a `oneOf` or `anyOf` of `X` and `null`, is `X`:
null reads as its zero value, and a required field sends the zero value where
it would have sent null. A union reads null as no alternative and writes an
unset union as null. `X` is a pointer only by the rule for fields above,
where it is a tuple, which would refuse null, or where it is an object that
requires a union, whose zero value would write that member as null.
- **Fixed parameters** — a required parameter that can take only one value, its
`const` or the only value of its `enum`, is sent by the client itself: in the
path, the query or the headers, such as Notion's `Notion-Version`. The caller
Expand Down
15 changes: 2 additions & 13 deletions ir/allof.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ func unionVariants(u *openapi.Schema, variants openapi.SchemaList) (fields, choi
decider = &openapi.Schema{}
}

// a union that can be null cannot have a discriminator come first
// null, where the union may be it, is read before the discriminator is looked for
member, values, ok := discriminate(decider, leaves)
if !ok || countNull(variants) > 0 {
if !ok {
return fields, choices, "", nil
}

Expand All @@ -367,17 +367,6 @@ func unionVariants(u *openapi.Schema, variants openapi.SchemaList) (fields, choi
return fields, choices, member, nil
}

func countNull(l openapi.SchemaList) int {
n := 0
for _, s := range l {
if isNull(s) {
n++
}
}

return n
}

// fromAllOfSchema builds the struct of an allOf. A part that only this schema uses is folded into its fields, any
// other is embedded, and a union among the parts is held as a field of its own, decoded by the struct's own methods.
func fromAllOfSchema(name string, s *openapi.Schema, uses map[string]int, folded map[string]bool) (*Schema, error) {
Expand Down
14 changes: 8 additions & 6 deletions ir/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ func zeroIsAValue(s *openapi.Schema, tp *GoType) bool {
}

s = deref(s)
if v := nullableVariant(s); v != nil {
s = deref(v)
}

if s == nil || len(s.Default) > 0 && isZeroJSON(s.Default) { // leaving it out already means the zero value
return false
}

if v := nullableVariant(s); v != nil {
if s = deref(v); s == nil || len(s.Default) > 0 && isZeroJSON(s.Default) {
return false
}
}

switch s.Type {
case openapi.TypeBoolean, openapi.TypeInteger, openapi.TypeNumber:
switch {
Expand Down Expand Up @@ -135,8 +137,8 @@ func pointRecursiveFields(schemas []Schema) {
}
}

// zeroUnencodable reports whether the zero value of s's Go type cannot be encoded: an object that requires a member,
// directly or through an object it requires, that is a union, which refuses to encode with no alternative set. Null
// zeroUnencodable reports whether the zero value of s's Go type encodes as nothing s allows: an object that requires a
// member, directly or through an object it requires, that is a union, which writes null with no alternative set. Null
// reads as that zero value, so only a pointer can hold null and write it back.
func zeroUnencodable(s *openapi.Schema) bool {
return zeroUnencodableSeen(s, map[*openapi.Schema]bool{})
Expand Down
11 changes: 6 additions & 5 deletions ir/pointer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,19 @@ func TestNullableGoType(t *testing.T) {
s *openapi.Schema
pointer bool
}{
// null and the zero value mean the same: no pointer
// null reads as the zero value: no pointer
{"a string", &openapi.Schema{Type: openapi.TypeString}, false},
{"a time", &openapi.Schema{Type: openapi.TypeString, Format: openapi.FormatDateTime}, false},
{"a date", &openapi.Schema{Type: openapi.TypeString, Format: openapi.FormatDate}, false},
{"an ID from 1", &openapi.Schema{Type: openapi.TypeInteger, Min: &one}, false},
{"an object requiring nothing", &openapi.Schema{Type: openapi.TypeObject, Properties: openapi.Schemas{
"name": {Type: openapi.TypeString},
}}, false},
// they differ: a pointer
{"a number", &openapi.Schema{Type: openapi.TypeInteger}, true},
{"a boolean", &openapi.Schema{Type: openapi.TypeBoolean}, true},
{"a union, which refuses null", union, true},
{"a number", &openapi.Schema{Type: openapi.TypeInteger}, false},
{"a boolean", &openapi.Schema{Type: openapi.TypeBoolean}, false},
{"a union, which reads null as no alternative", union, false},
// it cannot: a pointer
{"a tuple, which refuses null", &openapi.Schema{Type: openapi.TypeArray, PrefixItems: openapi.SchemaList{{Type: openapi.TypeString}}}, true},
} {
tp, err := nullableGoType(tc.s)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions ir/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func TestQueryParams(t *testing.T) {
t.Errorf("the client guards the required member x of the required object point:\n%s", encode)
}

// a null cannot be sent, so a required member that may be null is left out while it is
if want := "if params.Point.Z != nil {\nparts = append(parts, \"z\", strconv.Itoa(*params.Point.Z))"; !strings.Contains(encode, want) {
t.Errorf("the client sends the nullable member z when nil:\n%s", encode)
// a required member that may be null is sent like any other required member, null being its zero value
if want := "parts = append(parts, \"z\", strconv.Itoa(params.Point.Z))"; !strings.Contains(encode, want) {
t.Errorf("the client does not send the nullable member z:\n%s", encode)
}

// a server whose parameters are all split from the raw query declares no query values it would not use
Expand Down
24 changes: 6 additions & 18 deletions ir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,36 +214,24 @@ func nullableVariant(s *openapi.Schema) *openapi.Schema {
}
}

// nullableGoType is the Go type of X or null: X itself, null reading as its zero value, unless null and the zero value
// mean different things. They do for a number or a boolean whose zero value is a value of its own, such as a count
// that may be 0, but not for a string, a time, a date or an object: an empty one says what null does. A required
// field of X sends its zero value where it would have sent null, which a client sending such a field can live with.
// nullableGoType is the Go type of X or null: X itself, null reading as its zero value. A required field of X sends
// its zero value where it would have sent null, which a client sending such a field can live with, and an optional one
// is a pointer only where X's would be (see [zeroIsAValue]).
func nullableGoType(v *openapi.Schema) (*GoType, error) {
tp, err := SchemaGoType(v)
if err != nil {
return nil, err
}

// a type that decodes itself, such as a union, would refuse null, and one whose zero value cannot be encoded could
// not write it back
if zeroIsAScalarValue(v, tp) || hasJSONMethods(v) || zeroUnencodable(v) {
// a union reads null as no alternative and writes it back, but another type that decodes itself, such as a tuple,
// would refuse null, and an object requiring a union would write it back as an object of a null member
if hasJSONMethods(v) && !isUnion(v) && !isTaggedUnion(v) || zeroUnencodable(v) {
tp.IsPointer = true
}

return tp, nil
}

// zeroIsAScalarValue reports whether s is a number or a boolean whose zero value, that of tp, is a value s can hold
// (see [zeroIsAValue]).
func zeroIsAScalarValue(s *openapi.Schema, tp *GoType) bool {
switch deref(s).Type {
case openapi.TypeBoolean, openapi.TypeInteger, openapi.TypeNumber:
return zeroIsAValue(s, tp)
default:
return false
}
}

// isOneOfOnly reports whether s is an untagged union expressed purely via
// oneOf (no type, allOf, or anyOf of its own), and is not the
// date-time-or-integer pattern that collapses to time.Time.
Expand Down
51 changes: 28 additions & 23 deletions ir/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func TestFromDocument_FieldTypesAndTags(t *testing.T) {
"tags": {"type": "array", "items": {"type": "string"}},
"nullCount": {"oneOf": [{"type": "integer"}, {"type": "null"}]},
"nullName": {"oneOf": [{"type": "string"}, {"type": "null"}]},
"seed": {"anyOf": [{"type": "integer"}, {"type": "null"}], "default": 0},
"nullArchived": {"anyOf": [{"type": "boolean"}, {"type": "null"}], "default": false},
"limit": {"type": "integer"},
"pageSize": {"type": "integer", "minimum": 1},
"offset": {"type": "integer", "default": 0},
Expand Down Expand Up @@ -284,30 +286,33 @@ func TestFromDocument_FieldTypesAndTags(t *testing.T) {
}

// An optional field is omitted when unset, and a required one is always sent. A pointer is there only where its
// zero value is a value of its own, or to keep a struct from containing itself.
// zero value is a value of its own, or to keep a struct from containing itself. Null reads as the zero value, so
// that a field may be null makes it no pointer.
for name, want := range map[string]string{
"count": `int json:"count"`,
"on": `bool json:"on"`,
"id": `string json:"id"`,
"when": `time.Time json:"when"`,
"tags": `[]string json:"tags"`,
"nullCount": `*int json:"nullCount"`,
"nullName": `string json:"nullName"`,
"limit": `*int json:"limit,omitzero"`,
"pageSize": `int json:"pageSize,omitzero"`,
"offset": `int json:"offset,omitzero"`,
"archived": `*bool json:"archived,omitzero"`,
"name": `string json:"name,omitzero"`,
"site": `url.URL json:"site,omitzero"`,
"before": `time.Time json:"before,omitzero"`,
"ids": `[]string json:"ids,omitzero"`,
"labels": `map[string]string json:"labels,omitzero"`,
"ratio": `*float64 json:"ratio,omitzero"`,
"extra": `any json:"extra,omitzero"`,
"empty": `*struct{} json:"empty,omitzero"`,
"owner": `Owner json:"owner,omitzero"`,
"options": `*Options json:"options,omitzero"`,
"next": `*Filter json:"next,omitzero"`,
"count": `int json:"count"`,
"on": `bool json:"on"`,
"id": `string json:"id"`,
"when": `time.Time json:"when"`,
"tags": `[]string json:"tags"`,
"nullCount": `int json:"nullCount"`,
"seed": `int json:"seed,omitzero"`,
"nullArchived": `bool json:"nullArchived,omitzero"`,
"nullName": `string json:"nullName"`,
"limit": `*int json:"limit,omitzero"`,
"pageSize": `int json:"pageSize,omitzero"`,
"offset": `int json:"offset,omitzero"`,
"archived": `*bool json:"archived,omitzero"`,
"name": `string json:"name,omitzero"`,
"site": `url.URL json:"site,omitzero"`,
"before": `time.Time json:"before,omitzero"`,
"ids": `[]string json:"ids,omitzero"`,
"labels": `map[string]string json:"labels,omitzero"`,
"ratio": `*float64 json:"ratio,omitzero"`,
"extra": `any json:"extra,omitzero"`,
"empty": `*struct{} json:"empty,omitzero"`,
"owner": `Owner json:"owner,omitzero"`,
"options": `*Options json:"options,omitzero"`,
"next": `*Filter json:"next,omitzero"`,
} {
if got[name] != want {
t.Errorf("%s: got %s, want %s", name, got[name], want)
Expand Down
2 changes: 1 addition & 1 deletion ir/tagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func taggedFields(shape *taggedShape, existing []Field) ([]Field, *Tagged, bool,
return nil, nil, false, fmt.Errorf("property %q: %w", p.name, err)
}

nullable := tp.IsPointer || nullableVariant(deref(own)) != nil
nullable := tp.IsPointer || allowsNull(own)
tv.Members = append(tv.Members, TaggedOwn{Name: p.name, Required: shape.ownRequired[i][p.name], Nullable: nullable})

if declared[p.name] {
Expand Down
Loading
Loading