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

Example not added to swagger output in case of string, byte and timespan #25

Closed
jitsingh89 opened this issue Nov 21, 2017 · 11 comments
Closed

Comments

@jitsingh89
Copy link

jitsingh89 commented Nov 21, 2017

I not able to show example value of string, timespan, byte in swagger ui page. Error: 0x800a1391- Javascript runtime error: ‘jsyaml’ is undifned. To show example value i have created a class liker this

public class EmployeeExample: IExamplesProvider
{
    public object GetExamples()
    {
      return "Sample Value"; //  doing same this for timespan, byte , It's working fine for integer.
    }

Please let me how can i resolve this issue ?

@jitsingh89 jitsingh89 changed the title Example not added to swagger output in case of string and timespan Example not added to swagger output in case of string, byte and timespan Nov 21, 2017
@mattfrear
Copy link
Owner

I've never seen that error before. Can you show me what your controller looks like?

@jitsingh89
Copy link
Author

jitsingh89 commented Nov 24, 2017

In StringResponseExample.cs

using Swashbuckle.Examples;
namespace WebApi.Models.Examples
{
    public class StringResponseExample : IExamplesProvider
    {
        public object GetExamples()
        {
            return "Sample String 1";
        }
    }
}

In PersonController.cs

using Swashbuckle.Examples;
using Swashbuckle.Swagger.Annotations;
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using Newtonsoft.Json.Converters;
using WebApi.Models;
using WebApi.Models.Examples;
using System.Net.Http;
namespace WebApi.Controllers
{
    public class PersonController : ApiController
    {
        [HttpPost]
        [Route("api/Person/save")]
        [SwaggerResponseExample(HttpStatusCode.OK, typeof(StringRequestExample))]
        public HttpResponseMessage PostenericPerson(PersonRequest personRequest)
        {
            string result =  "Created";
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}

**This is the error i am getting **
error

@mattfrear
Copy link
Owner

mattfrear commented Nov 25, 2017

The jsyaml undefined thing looks like a known issue of swagger-ui which was fixed in 2016. swagger-api/swagger-ui#2369

Aside from that, you're missing the [SwaggerResponse] attribute. I suggest you consult the Swashbuckle documentation to get started - this library is merely an extension of that one.

I am able to get the Swagger page loading for you with this:

[HttpPost]
[Route("api/Person/save")]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StringResponseExample))]
[SwaggerResponse(HttpStatusCode.OK, "Saves the person", typeof(string))]
public HttpResponseMessage PostenericPerson(PersonRequest personRequest)
{
    string result = "Created";
    return Request.CreateResponse(HttpStatusCode.OK, result);
}

public class StringResponseExample : IExamplesProvider
{
    public object GetExamples()
    {
        return "Sample String 1";
    }
}

The page loads, and the underlying Swagger document contains the correct example:

"/api/Person/save": {
	"post": {
		"tags": ["Values"],
		"operationId": "Values_PostenericPerson",
		"consumes": ["application/json", "text/json", "application/xml", "text/xml", "application/x-www-form-urlencoded"],
		"produces": ["application/json", "text/json", "application/xml", "text/xml"],
		"parameters": [{
				"name": "personRequest",
				"in": "body",
				"required": true,
				"schema": {
					"$ref": "#/definitions/PersonRequest"
				}
			}
		],
		"responses": {
			"200": {
				"description": "Saves the person",
				"schema": {
					"type": "string"
				},
				"examples": {
					"application/json": "Sample String 1"
				}
			}
		}
	}

However, unfortunately the Swagger-UI page doesn't display the example:
swagger

This seems like another bug in swagger-ui; there's nothing I can do to fix it, but you may want to raise a bug against swagger-ui.

@mattfrear
Copy link
Owner

This works:

public class StringResponseExample : IExamplesProvider
{
    public object GetExamples()
    {
        return new { id = "Sample String 1" };
    }
}

@jitsingh89
Copy link
Author

jitsingh89 commented Feb 27, 2018

yes It's working but You are returning object with id not a string. I have to return only string.
currently i am working in my project like this

 public object GetExamples()
        {            
            List<string> lst = new List<string>();
            lst.Add("Sample String");
            return lstl
        }

This is also working fine. Output

image

@TheBels
Copy link

TheBels commented May 2, 2018

Hello- I am a bit confused on this issue. As I understand the Open API v2.0 spec for examples, the examples should include the mime-type. Per ExamplesOperationFilter the mime type is being given as application/json.

This works for JSON responses; however, what if you have a response mime type of text/csv? The Open API spec link above has an example with two mime types: JSON and text.

I tried adding [Produces("text/csv")] to my action, as well as the attribute [SwaggerResponse(200, typeof(string))]. However it doesn't seem to pick up that mime type for the example.

Specifically, this is the swagger YAML:
example-json

This is how swagger-ui renders it:
example-swagger-ui-json

And this is what I would expect/want based off the Produces attribute:
example-text png

Is it possible to use a mime-type other than JSON?

@mattfrear
Copy link
Owner

mattfrear commented May 3, 2018 via email

@TheBels
Copy link

TheBels commented May 3, 2018

Thanks for the reply, Matt, and understood.

I actually meant to log this against Swashbuckle.AspNetCore.Examples. I have entered an issue there: mattfrear/Swashbuckle.AspNetCore.Filters#43.

@mattfrear
Copy link
Owner

@jitsingh89 I'm sure this is probably too late for you, but I have just tested this locally like this:

        [HttpPost]
        [Route("api/Person/save")]
        [SwaggerResponse(200, "The person was saved", typeof(string))]
        [SwaggerResponseExample(200, typeof(StringResponseExample))]
        public IActionResult PostenericPerson(PersonRequest personRequest)
        {
            string result = "Created";
            return Ok(result);
        }

        public class StringResponseExample : IExamplesProvider
        {
            public object GetExamples()
            {
                return "Sample String 1";
            }
        }

and I think it's working
image

This is using Swashbuckle.AspNetCore 3.0 and my new Swashbuckle.AspNetCore.Filters package (to be released soon)

@jitsingh89
Copy link
Author

jitsingh89 commented Jul 17, 2018

Thanks @mattfrear

@diaznicolasandres1
Copy link

@jitsingh89 I'm sure this is probably too late for you, but I have just tested this locally like this:

        [HttpPost]
        [Route("api/Person/save")]
        [SwaggerResponse(200, "The person was saved", typeof(string))]
        [SwaggerResponseExample(200, typeof(StringResponseExample))]
        public IActionResult PostenericPerson(PersonRequest personRequest)
        {
            string result = "Created";
            return Ok(result);
        }

        public class StringResponseExample : IExamplesProvider
        {
            public object GetExamples()
            {
                return "Sample String 1";
            }
        }

and I think it's working
image

This is using Swashbuckle.AspNetCore 3.0 and my new Swashbuckle.AspNetCore.Filters package (to be released soon)

This works, you saved me

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

4 participants