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
27 changes: 27 additions & 0 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,30 @@ custom format, `date-time-ms`, like `date-basic`, makes them a `time.Time` in
milliseconds, written and read through jsonutil helpers beside
`TimeMarshalIntUnix`, in a body and a query parameter alike. openapi-enrich
must keep the format when a recording's integer meets it.

## More of oapi-codegen's extensions

`x-go-name` and `x-enum-varnames` / `x-enumNames` work as in
[oapi-codegen](https://github.com/oapi-codegen/oapi-codegen/blob/main/docs/extensions.md),
a property's own extensions beside a `$ref` winning over the referenced
schema's. These would fit too, each as oapi-codegen has it, once a spec needs
it:

- **`x-go-type` / `x-go-type-import`:** a field's or a type's Go type, given,
such as a decimal type for a number written as a string.
- **`x-go-type-skip-optional-pointer`:** an optional field without a pointer,
where its zero value needs no telling apart from its absence; see also "No
pointers in types only ever decoded".
- **`x-omitzero`:** `omitzero` on a field that would not otherwise have it.
- **`x-oapi-codegen-extra-tags`:** more struct tags on a field.
- **`x-deprecated-reason`:** a `Deprecated:` paragraph in a type's or field's
doc comment.
- **`x-order`:** a field's place in its struct, where the spec's order of
properties is not the one wanted.

The others do not fit: `x-omitempty` is `x-omitzero`'s older form,
`x-go-type-name` a second way to name a type beside `x-go-name`,
`x-go-json-ignore` a property better left out of the spec,
`x-oapi-codegen-only-honour-go-name` an unexported field, and
`x-oapi-codegen-enum-merge` a choice between ways of merging one property's
enums across an `allOf`'s parts, which the generator does not merge.
4 changes: 2 additions & 2 deletions ir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func getField(jsonName string, propRef *openapi.Schema, requiredSet map[string]b
}

fieldName := fieldGoName(jsonName)
if override := goNameOverride(v); override != "" {
if override := cmp.Or(goNameOverride(propRef), goNameOverride(v)); override != "" {
fieldName = override
}

Expand Down Expand Up @@ -806,7 +806,7 @@ func fromTupleSchema(name string, s *openapi.Schema) (*Schema, error) {
}

fieldName := fmt.Sprintf("Item%0*d", width, i)
if override := goNameOverride(deref(p)); override != "" {
if override := cmp.Or(goNameOverride(p), goNameOverride(deref(p))); override != "" {
fieldName = override
}

Expand Down
28 changes: 28 additions & 0 deletions ir/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1735,3 +1735,31 @@ func TestEnumConstNames(t *testing.T) {
}
}
}

// A property that is a $ref may name its field with an x-go-name of its own, beside the reference, as it may describe
// itself: many properties of one component type each get their own name.
func TestFromComponentSchemas_XGoNameBesideRef(t *testing.T) {
value := &openapi.Schema{Type: openapi.TypeObject}

props := openapi.Schemas{}
props.Set("netliquidation", &openapi.Schema{
Ref: &openapi.SchemaRef{Identifier: "#/components/schemas/SummaryValue", Value: value},
Extensions: jsontext.Value(`{"x-go-name":"NetLiquidation"}`),
})

schemas := openapi.Schemas{}
schemas.Set("Summary", &openapi.Schema{Type: openapi.TypeObject, Properties: props})

got, err := ir.FromComponentSchemas(schemas)
if err != nil {
t.Fatal(err)
}

if len(got) != 1 || len(got[0].Fields) != 1 {
t.Fatalf("unexpected schemas/fields: %+v", got)
}

if f := got[0].Fields[0]; f.Name != "NetLiquidation" || f.JSONName != "netliquidation" {
t.Errorf("got field %q (json %q), want NetLiquidation (json netliquidation)", f.Name, f.JSONName)
}
}
Loading