Skip to content

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

Closed
MilkeeyCat wants to merge 1 commit into
oapi-codegen:mainfrom
MilkeeyCat:fix/uuid-encoding
Closed

fix: use .String() method for making a string from uuid#1922
MilkeeyCat wants to merge 1 commit into
oapi-codegen:mainfrom
MilkeeyCat:fix/uuid-encoding

Conversation

@MilkeeyCat

Copy link
Copy Markdown

fixes #1914

@0rid

0rid commented Apr 18, 2025

Copy link
Copy Markdown

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

@ysuzuki19

Copy link
Copy Markdown

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.

As noted in oapi-codegen#1914, there are cases where a `text/plain` **??**, such as
a UUID , **??**

Closes oapi-codegen#1914.
@jamietanna jamietanna force-pushed the fix/uuid-encoding branch from 962f98b to 1ad5124 Compare May 10, 2025 09:36
@jamietanna

Copy link
Copy Markdown
Member

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?

@MilkeeyCat

MilkeeyCat commented May 10, 2025

Copy link
Copy Markdown
Author

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

Hm, honestly I prefer compilation error over incorrectly stringified value

@jamietanna

Copy link
Copy Markdown
Member

Hmm - I get your thinking, but also not sure 🤔 I've raised #1975 in the meantime, let's see what some others think about the behaviour 👍

@jamietanna jamietanna added this to the v2.5.0 milestone May 10, 2025
@jamietanna

Copy link
Copy Markdown
Member

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

@MilkeeyCat

Copy link
Copy Markdown
Author

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

@jamietanna

jamietanna commented May 10, 2025

Copy link
Copy Markdown
Member

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?

@MilkeeyCat

Copy link
Copy Markdown
Author

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

@jamietanna

Copy link
Copy Markdown
Member

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 jamietanna modified the milestones: v2.5.0, v2.6.0 Jul 15, 2025
@mromaszewicz

Copy link
Copy Markdown
Member

I think that automatically looking for a Stringer implementation via a safe type cast is about as good as we can do.

Marshaling primitive types as text/plain makes sense.

In the case of having text/plain media types for complex types, I think we should error out, the question is where to do it. We could look for a stringer implementation at runtime, and if one is there, use that. The way the code works currently is that on the server side, it's up to the user how to encode something as text/plain, we stay out of it.

If a user wants to do some fancy text encoding on complex types, let them implement Stringer manually on those types.

@mromaszewicz

mromaszewicz commented Mar 5, 2026

Copy link
Copy Markdown
Member

Ok, I think the way this should work is that we don't even look at UUID explicitly, the flow would be:

if json:
  ...
elif form
  ...
elif text:
   if bodyStringer, ok := body.(fmt.Stringer); ok : 
      bodyReader = strings.NewReader(bodyStringer.String())
   else if is_primitive_type(body):
      bodyReader = strings.NewReader(string(body))
    else:
      return error

The is_primitive_type(body) can be deduced by the codegen and provided as a template context flag.

Not having a codegen error allows a person to implement stringer. If they don't, they're going to get a runtime error.

@mromaszewicz

Copy link
Copy Markdown
Member

It looks like PR #1975 is close to what I want, so I think it supersedes this PR.

@mromaszewicz mromaszewicz added enhancement New feature or request and removed enhancement New feature or request labels Mar 5, 2026
@jamietanna jamietanna closed this Mar 12, 2026
@jamietanna

jamietanna commented Mar 12, 2026

Copy link
Copy Markdown
Member

Thanks for this - we've gone with #1975, as it handled a couple of extra things

@MilkeeyCat MilkeeyCat deleted the fix/uuid-encoding branch March 12, 2026 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot convert text/plain uuid in requestBody to string

5 participants