Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CustomNameTypes to specify nillability #220

Merged

Conversation

andrew-farries
Copy link
Contributor

@andrew-farries andrew-farries commented Apr 24, 2024

Allow CustomNameTypes to specify their nillability.

Currently CustomNameTypes are hard-coded to have IsNillable return false. This limitation means that fields in an object having a custom type are always wrapped in a pointer type when they are not required fields. For example:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "goJSONSchema": {
        "type": "map[bool]string"
      }
    }
  }
}

This results in the following Go code:

type Nillability struct {
	// Name corresponds to the JSON schema field "name".
	Name *map[bool]string `json:"name,omitempty" yaml:"name,omitempty" mapstructure:"name,omitempty"`
}

The map[bool]string is nillable, but go-jsonschema's schema generation generates a pointer type for the name field.

By extending the GoJSONSchemaExtension type with an extra Nillable field, custom types can specify their nillability in order to ensure they aren't wrapped in a pointer type when such fields are optional.


The motivation for this change is to be able to use the nullable package to distinguish between set-to-null fields and missing fields.

This snippet:

        "name": {
          "type": "string",
          "goJSONSchema": {
            "imports": ["github.com/oapi-codegen/nullable"],
            "nillable": true,
            "type": "nullable.Nullable[string]"
          }
        },

Should generate a name field of type nullable.Nullable[string] rather than *nullable.Nullable[string], in line with the documentation for nullable which advises against using pointers to the type.

Copy link

codecov bot commented Apr 24, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 75.94%. Comparing base (d963216) to head (27032aa).
Report is 21 commits behind head on main.

Files Patch % Lines
pkg/generator/schema_generator.go 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #220      +/-   ##
==========================================
- Coverage   76.58%   75.94%   -0.65%     
==========================================
  Files          24       26       +2     
  Lines        1892     1646     -246     
==========================================
- Hits         1449     1250     -199     
+ Misses        354      298      -56     
- Partials       89       98       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@andrew-farries andrew-farries marked this pull request as ready for review April 24, 2024 06:32
@omissis omissis self-requested a review April 25, 2024 20:08
@omissis omissis added this to the v0.17.0 milestone Apr 25, 2024
@omissis omissis added the enhancement New feature or request label Apr 25, 2024
Copy link
Owner

@omissis omissis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @andrew-farries, thanks for the contribution! Very nice improvement! :)

@omissis omissis merged commit 7f75500 into omissis:main Apr 25, 2024
1 of 3 checks passed
@andrew-farries andrew-farries deleted the feat/allow-set-custom-type-nillability branch April 26, 2024 07:12
andrew-farries added a commit to xataio/pgroll that referenced this pull request Apr 29, 2024
Build on #344 to allow removing column comments by setting them to
`null`.

Make use of omissis/go-jsonschema#220 and use
the [nullable](https://github.com/oapi-codegen/nullable) package so that
it's possible to distingush between a missing `comment` field and one
that is explicitly set to `null`.

With omissis/go-jsonschema#220 not being part of
a release yet, use a custom build of `go-jsonschema`. It should be
possible to switch back to the official release images once
omissis/go-jsonschema#220 is part of a release.

Without this change it becomes impossible to remove a comment from a
column using the 'set comment' 'alter column' sub-operation
(#344).
andrew-farries added a commit to xataio/pgroll that referenced this pull request Dec 17, 2024
Generate types from the JSON schema using the `omissis/go-jsonschema`
image instead of the `surjection/go-jsonschema` image.

We used the `surjection/go-jsonschema` image because we needed a
feature that was not yet released in the `omissis/go-jsonschema` image:

omissis/go-jsonschema#220

Now that the feature is available in the `omissis/go-jsonschema` `0.17`
release we can switch back to using it.
andrew-farries added a commit to xataio/pgroll that referenced this pull request Dec 18, 2024
Generate types from the JSON schema using the
official`omissis/go-jsonschema` image instead of the custom
`surjection/go-jsonschema` image.

We built and used the `surjection/go-jsonschema` image because we needed
a feature that was not yet released in `omissis/go-jsonschema`:

omissis/go-jsonschema#220

Now that the feature is available in the `omissis/go-jsonschema` `0.17`
[release](https://github.com/omissis/go-jsonschema/releases/tag/v0.17.0)
we can switch back to using it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants