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

String Example Value #1140

Closed
mschneider82 opened this issue Feb 16, 2022 · 14 comments
Closed

String Example Value #1140

mschneider82 opened this issue Feb 16, 2022 · 14 comments

Comments

@mschneider82
Copy link
Contributor

mschneider82 commented Feb 16, 2022

Hi,

I tried the new example feature for string:
// @Param email body string true "message/rfc822" example(Subject: Testmail\r\n\r\nBody Message\r\n)
Going to the swagger page it still shows "string" as the default Example Value. Howto change this to the example value? I would like to add a basic test mail as the text (which has a subject header and a message body)

image

@ubogdan
Copy link
Contributor

ubogdan commented Feb 23, 2022

Not sure why do you want to implement rfc822 over HTTP but you should try the following

type Message struct {
    Subject string `example:"Test mail"`
    Body string `example:"Message"`
}
// @consume json
// @Param email body Message true "Json Payload"

@mschneider82
Copy link
Contributor Author

mschneider82 commented Feb 23, 2022

A Content-Type of "message/rfc822" indicates that the body contains an encapsulated message, with the syntax of an RFC 822 message.

A valid Body could be the String:
Subject: Testmail\r\n\r\nBody Message\r\n

It has one Header followed by a newline that indicates that the Body Starts with the Text "Body messeage"

So json is wrong here. An String example would solve this

@ubogdan
Copy link
Contributor

ubogdan commented Feb 23, 2022

See OAS 2 specs regarding parameter object.

You are free to use "message/rfc822" or anything else that's not in the specs, but you are on your own.

@mschneider82
Copy link
Contributor Author

I have tried your Message struct, it gives an json record in the "edit Value" field instead of an email.

So its not possible to enter an normal string as body example?

@mschneider82
Copy link
Contributor Author

mschneider82 commented Feb 24, 2022

I got what i want by editing the docs/docs.go file:

                     {
-                        "example": "\"Subject: Testmail\\r\\n\\r\\nBody Message\\r\\n\"",
                         "description": "message/rfc822",
                         "name": "email",
                         "in": "body",
                         "required": true,
                         "schema": {
-                            "type": "string"
+                            "type": "string",
+                            "example": "Subject: Testmail\r\n\r\nBody Message\r\n"
                         }
                     }
                     

It would be nice if it would be possible to generate this (also without quoting the \r\n chars). An example of a string in case of a body should be in the schema section

@mschneider82
Copy link
Contributor Author

results in:
image

@mschneider82
Copy link
Contributor Author

@ubogdan what do you think about the PR ? Other idea is to include the Schema Example as default when body and example is used

@davidkhala
Copy link

By the issue, I see a great job on schemaExample tag. Meanwhile, will you consider also have this tag available also for
@Success.

I have no idea on how to set example value for it.

@ubogdan
Copy link
Contributor

ubogdan commented Mar 5, 2022

@mschneider82 The pr looks good so far, You only need to write the unit tests to cover the new code. See the codecov comments here https://github.com/swaggo/swag/pull/1148/files.

mschneider82 added a commit to mschneider82/swag that referenced this issue Mar 7, 2022
@mschneider82
Copy link
Contributor Author

@ubogdan i have covered the new code, please re-run codecov

@mschneider82
Copy link
Contributor Author

mschneider82 commented Mar 8, 2022

Hi @ubogdan i still have the problem that \r\n gets escaped. Do you have an idea how to solve it? My idea would be if the attr begings with ` and ends with ` (like in golang) there will be no escaping..

@mschneider82
Copy link
Contributor Author

// @Param email body string true "message/rfc822" SchemaExample(Subject: Testmail\r\n\r\nBody Message\r\n)

gets to:

          example: 'Subject: Testmail\r\n\r\nBody Message\r\n'

What i need is:

          example: "Subject: Testmail\r\n\r\nBody Message\r\n"

@ubogdan
Copy link
Contributor

ubogdan commented Mar 8, 2022

It looks like you got defeated by your own unit tests. 🥲
What about replacing '\r' with "\r" and '\n' with "\n" ?

@mschneider82
Copy link
Contributor Author

@ubogdan the PR works with my Example!

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