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

Support custom format types. #1335

Open
abemedia opened this issue Oct 24, 2024 · 3 comments
Open

Support custom format types. #1335

abemedia opened this issue Oct 24, 2024 · 3 comments
Labels
enhancement New feature or request openapi-features OpenAPI features support issues

Comments

@abemedia
Copy link
Contributor

abemedia commented Oct 24, 2024

Description

I'd like to propose adding support for custom format types. These could come from the configuration. Maybe something like the following example:

generator:
  formats:
    decimal: github.com/shopspring/decimal.Decimal

This would result in a field with format: decimal to be of the type decimal.Decimal from the package github.com/shopspring/decimal.

Alternatively, we could implement this as an extension e.g.

type: string
x-ogen-type: github.com/shopspring/decimal.Decimal

We can implement some checks to ensure the package is importable, as well as checking that the type implements one of the following interfaces:

interface {
  Encode(e *jx.Encoder)
  Decode(d *jx.Decoder) error
}
interface {
  json.Marshaler
  json.Unmarshaler
}
interface {
  encoding.TextMarshaler
  encoding.TextUnmarshaler
}

Based on which of the interfaces it implements we can use a different template for encoding and decoding the value.

I'm happy to implement this feature, just wanted to first check if such a feature would be accepted and get alignment on the way it's configured.

@abemedia abemedia added enhancement New feature or request openapi-features OpenAPI features support issues labels Oct 24, 2024
@tdakkota
Copy link
Member

I'm happy to implement this feature, just wanted to first check if such a feature would be accepted and get alignment on the way it's configured.

It would be nice. This feature was requested before.

I think it is better to configure it via extension x-go-type, since it also used by oapi-codegen
and go-swagger.

@abemedia
Copy link
Contributor Author

Thanks for getting back to me on this @tdakkota!

Both oapi-codegen and go-swagger use different formats though:

oapi-codegen

x-go-type: googleuuid.UUID
x-go-type-import:
  path: github.com/google/uuid
  name: googleuuid

go-swagger

x-go-type:
  import:
    package: github.com/google/uuid
    alias: googleuuid
  type: UUID

I personally still prefer the idea of writing a single field e.g.

x-go-type: github.com/google/uuid.UUID

We could also automatically alias it if there's a clash between package names.

What's your preference?

@tdakkota
Copy link
Member

I personally still prefer the idea of writing a single field e.g.

x-go-type: github.com/google/uuid.UUID

Me too.
I didn't notice that oapi-codegen requires a x-go-type-import. In that case, let's keep it x-ogen-type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-features OpenAPI features support issues
Projects
None yet
Development

No branches or pull requests

3 participants
@abemedia @tdakkota and others