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

Wrap property example value in double quotes to avoid hex string being evaluated as a number? #875

Open
Flugplatz opened this issue Feb 26, 2024 · 3 comments

Comments

@Flugplatz
Copy link

Flugplatz commented Feb 26, 2024

I have a property definition like below with an example hexadecimal string, because the example field is not quoted the swagger UI interprets this as a decimal string instead of the raw hexadecimal string which I want.

Example output:

      properties:
        sellToken:
          type: string
          description: Address of token sold.
          example: 0x6810e776880c02933d47db1b9fc05908e5386b96

Wanted output:

      properties:
        sellToken:
          type: string
          description: Address of token sold.
          example: "0x6810e776880c02933d47db1b9fc05908e5386b96"

The rust code which generates this is below, i've tried multiple combinations of wrapping the example string in single/double quotes (and inside a json! macro), but this adds multiple quotes (ie. '"0x6810e776880c02933d47db1b9fc05908e5386b96"') and makes the value broken in swagger as a raw (unquoted string) is required.

    /// Address of token sold.
    #[schema(value_type = String, example = "0x6810e776880c02933d47db1b9fc05908e5386b96")]
    pub sell_token: H160,

Can someone help me quote this string in the produced yaml?

Thanks,
Matt

@squadgazzz
Copy link

squadgazzz commented Apr 30, 2024

One more vote for that. Quotes disappear if the example value string length is >= 36. The issue lies in the serde_yaml::to_string. Providing a custom serializer works, but it breaks json backward compatibility. Will try to find out why it happens, but serde_yaml project is not maintained anymore...

@juhaku
Copy link
Owner

juhaku commented Sep 18, 2024

I reckon in what comes to utoipa there is very little to nothing that can be actually done. What does the serde_json output? Does it output a String? The example field is specifically serde_json::Value in order to serialize as any value. If the example is defined with double quotes, it will be put as a string to the generated spec as well. Then it is up to the serializer to decide how the value is serialized. In this case I bet serde_yaml does not do what it should.

but serde_yaml project is not maintained anymore...

I don't know whether serde_yaml has some flags that you could give it before serializing to yaml in order to preserve the double quotes. But since it is unmaintained I am not sure how much support it will get in the future.

@squadgazzz
Copy link

But since it is unmaintained I am not sure how much support it will get in the future.

It is a public archive now https://github.com/dtolnay/serde-yaml

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

No branches or pull requests

3 participants