oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

fix: use `.String()` method for making a string from uuid

Open MilkeeyCat opened this issue 10 months ago • 10 comments

fixes #1914

MilkeeyCat avatar Mar 12 '25 19:03 MilkeeyCat

@MilkeeyCat You can achieve a similar solution using by overriding the property type in overlay.yaml

0rid avatar Apr 18 '25 14:04 0rid

This PR looks good! I have successfully tested this on my local environment, and it works as expected.

It would be even better if you could add or update a test.

For example, you might want to consider adding a paths in:

file: internal/test/schemas/schemas.yaml line: 150 content:

  /issues/1914:
    post:
      operationId: Issue1914
      description: |
        Ensure that anonymous inner types are generated correctly.
      requestBody:
        content:
          text/plain:
            schema:
              type: string
              format: uuid

and then run make generate on this repository root.

ysuzuki19 avatar Apr 26 '25 18:04 ysuzuki19

Thanks!

Looking at this, I'm wondering if we should put in a wider fix, which is that we try and convert the type to a string i.e. with fmt.Sprintf or something similar, so we end up returning something rather than hitting the compilation error - thoughts?

jamietanna avatar May 10 '25 09:05 jamietanna

so we end up returning something rather than hitting the compilation error - thoughts?

Hm, honestly I prefer compilation error over incorrectly stringified value

MilkeeyCat avatar May 10 '25 14:05 MilkeeyCat

Hmm - I get your thinking, but also not sure :thinking: I've raised https://github.com/oapi-codegen/oapi-codegen/pull/1975 in the meantime, let's see what some others think about the behaviour :+1:

jamietanna avatar May 10 '25 15:05 jamietanna

Maybe let's add a compatibility-option to disable the fallback?

jamietanna avatar May 10 '25 15:05 jamietanna

I don't understand what fallback you're talking about, but #1975 looks good to me

MilkeeyCat avatar May 10 '25 17:05 MilkeeyCat

Thanks - so I'm thinking we could add an entry into the CompatibilityOptions to allow us to push #1975 through as-is, but allow folks to opt-in to "I want to have a compilation error when there's not a String() method on the type" which would be best of both worlds?

jamietanna avatar May 10 '25 17:05 jamietanna

With the way #1975 is done I personally don't think this option is needed.

... allow folks to opt-in to "I want to have a compilation error when there's not a String() method on the type"

How would that work with integer, string types? They don't have .String() method

MilkeeyCat avatar May 10 '25 18:05 MilkeeyCat

With the way #1975 is done I personally don't think this option is needed.

... allow folks to opt-in to "I want to have a compilation error when there's not a String() method on the type"

How would that work with integer, string types? They don't have .String() method

Ah yeah, so we'd probably have the fallback of "if someone's using the compatibility option for the fallback, use string(body), and if they're not, use fmt.Sprintf(...)"

jamietanna avatar May 11 '25 10:05 jamietanna