question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to show HttpStatusCode.Created status code

See original GitHub issue

I am not able to show status code & Example value if method return type is HttpStatusCode.Created on swagger ui. It’s working fine for return type HttpStatusCode.Ok. This is my controller and Example value class

In PersonController.cs

 public class PersonController : ApiController
    {
        [HttpPost]
        [Route("api/Person/save")]
        [SwaggerResponseExample(HttpStatusCode.Created, typeof(PersonResponseExample))]
        public HttpResponseMessage PostenericPerson(PersonRequest personRequest)
        {
            var result = 1;// "Created";
            return Request.CreateResponse(HttpStatusCode.Created, result);
        }
    }

In PersonResponseExample.cs

 internal class PersonResponseExample : IExamplesProvider
    {
        public object GetExamples()
        {
            return new PersonResponse
            {
                Id = 123,
                Title = Title.Dr,
                FirstName = "John",
                LastName = "Doe",
                Age = 27,
                Income = null
            };
        }
    }

This is the output i want to get i have mark in red. ui

Please let me know am i missing anything.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mattfrearcommented, Nov 26, 2017

I just did a quick google, and this will get rid of the unwanted 200 response, and now the example is displayed fine for the 201:

[HttpPost]
[Route("api/Person")]
[SwaggerResponseRemoveDefaults]
[SwaggerResponse(HttpStatusCode.Created, "Created", typeof(PersonResponse))]
[SwaggerResponseExample(HttpStatusCode.Created, typeof(PersonResponseExample))]
public HttpResponseMessage Created(PersonRequest personRequest)
1reaction
mattfrearcommented, Nov 26, 2017

I had a look at your example. Your syntax is incorrect, it should be like this:

[HttpPost]
[Route("api/Person/savecreted")]
       
[SwaggerResponse(HttpStatusCode.Created, "Created", typeof(PersonResponse))]
[SwaggerResponseExample(HttpStatusCode.Created, typeof(PersonResponseExample))]
public HttpResponseMessage SavePersonCreated(PersonRequest personRequest)
{

With that syntax, it IS working, as the Swagger document does contain the correct example for the 201 response:

"/api/Person/savecreted": {
	"post": {
		"tags": ["Person"],
		"operationId": "Person_SavePersonCreated",
		"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": "OK",
				"schema": {
					"type": "object"
				}
			},
			"201": {
				"description": "Created",
				"schema": {
					"$ref": "#/definitions/PersonResponse"
				},
				"examples": {
					"application/json": {
						"id": 123,
						"title": 1,
						"firstName": "John",
						"lastName": "Doe",
						"age": 27
					}
				}
			}
		}
	}
},

However, the example is NOT showing up in swagger-ui. I forgot about that, but this is a Known issue and already documented in my project’s readme section under Known Issues.

Although you can add a response examples for each HTTP status code (200, 400, 404 etc), and they will appear in the swagger.json, they will not display correctly. This is due to an bug in swagger-ui. Issue 9

Just so you know, the 201 example should be here: swagger 2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to get a HttpStatusCode
I have written the following code on Visual Studio. I have been newly working on http protocols to study how they function.
Read more >
What's the most appropiate http status code for 'not ...
In this case, it would return a 412 precondition failed. There's a case to be made for returning a 200, but a client's...
Read more >
A Complete Guide and List of HTTP Status Codes
A complete list of HTTP status codes with explaination of what they are, why they occur and what you can do to fix...
Read more >
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
HttpStatusCode Enum (System.Net)
Equivalent to HTTP status 405. MethodNotAllowed indicates that the request method (POST or GET) is not allowed on the requested resource. MisdirectedRequest ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found