Skip to content

v2.1.0: Nullable, external reference improvements, x-order, and many more!

Latest
Compare
Choose a tag to compare
@github-actions github-actions released this 25 Jan 15:02
· 68 commits to refs/heads/master since this release

馃攰 Notable features

Nullable types

It's possible that you want to be able to determine whether a field isn't sent, is sent as null or has a value.

For instance, if you had the following OpenAPI property:

S:
  type: object
  properties:
    Field:
      type: string
      nullable: true
    required: []

The current behaviour in oapi-codegen is to generate:

type S struct {
	Field *string `json:"field,omitempty"`
}

However, you lose the ability to understand the three cases, as there's no way to distinguish two of the types from each other:

  • is this field not sent? (Can be checked with S.Field == nil)
  • is this field null? (Can be checked with S.Field == nil)
  • does this field have a value? (S.Field != nil && *S.Field == "123")

Therefore, as requested in #1039, this is now possible to represent with the nullable.Nullable type from our new library, oapi-codegen/nullable.

If you configure your generator's Output Options as so:

output-options:
  nullable-type: true

You will now receive the following output:

type S struct {
    Field nullable.Nullable[string] `json:"field,omitempty"`
}

Note that this is opt-in only, due to it being a break in existing signatures and behaviour.

You can find out more about how this works in a blog post with further details.

External references are now handled better

A big change has come in which handling of external references (also called import mappings) is much more resilient and predictable for generated code.

This allows cases where multiple files referencing each other (for instance if you've split your API across multiple files, and join them using $refs) now correctly generate code.

There are a few cases that won't be covered, that we'll complete in #1440 but until then, it hopefully should work better.

Thank you to Ejendomstorvet for sponsoring this work.

馃殌 New features and improvements

馃悰 Bug fixes

馃摑 Documentation updates

馃懟 Maintenance

馃摝 Dependency updates

New Contributors