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

Use Additional API Explorer Metadata #1134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Swashbuckle.Core/Swagger/ApiDescriptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public static string FriendlyId(this ApiDescription apiDescription)
public static IEnumerable<string> Consumes(this ApiDescription apiDescription)
{
return apiDescription.SupportedRequestBodyFormatters
.SelectMany(formatter => formatter.SupportedMediaTypes.Select(mediaType => mediaType.MediaType))
.SelectMany(formatter => formatter.SupportedMediaTypes.Select(mediaType => mediaType.ToString()))
.Distinct();
}

public static IEnumerable<string> Produces(this ApiDescription apiDescription)
{
return apiDescription.SupportedResponseFormatters
.SelectMany(formatter => formatter.SupportedMediaTypes.Select(mediaType => mediaType.MediaType))
.SelectMany(formatter => formatter.SupportedMediaTypes.Select(mediaType => mediaType.ToString()))
.Distinct();
}

Expand Down
6 changes: 5 additions & 1 deletion Swashbuckle.Core/Swagger/SwaggerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,18 @@ private Parameter CreateParameter(string location, ApiParameterDescription param
}

parameter.required = location == "path" || !paramDesc.ParameterDescriptor.IsOptional;
parameter.@default = paramDesc.ParameterDescriptor.DefaultValue;
parameter.description = paramDesc.Documentation;

var schema = schemaRegistry.GetOrRegister(paramDesc.ParameterDescriptor.ParameterType);

if (parameter.@in == "body")
parameter.schema = schema;
else
parameter.PopulateFrom(schema);

if (paramDesc.ParameterDescriptor.DefaultValue != null)
parameter.@default = paramDesc.ParameterDescriptor.DefaultValue;

return parameter;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ private static void ApplyParamComments(Operation operation, XPathNavigator metho
if (actionParameter == null) continue;

var paramNode = methodNode.SelectSingleNode(string.Format(ParamXPath, actionParameter.Name));

if (paramNode != null)
parameter.description = paramNode.ExtractContent();
{
var description = paramNode.ExtractContent();

if (!string.IsNullOrEmpty(description))
parameter.description = description;
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions Swashbuckle.Dummy.Core/Controllers/XmlAnnotatedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public void UpdateMetadata(int id, KeyValuePair<string, string>[] metadata)
{
throw new NotImplementedException();
}

[HttpGet]
[Route("GetById")]
public void GetById(string id = "123456")
{
throw new NotImplementedException();
}
}

public class Page
Expand Down
15 changes: 15 additions & 0 deletions Swashbuckle.Tests/Swagger/XmlCommentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ public void It_does_not_clear_previously_added_responses()
Assert.IsNotNull(responsesProperty["500"]);
}

[Test]
public void It_documents_schema_default_parameters()
{
var swagger = GetContent<JObject>( "http://tempuri.org/swagger/docs/v1" );

var parameters = swagger["paths"]["/xmlannotated/GetById"]["get"]["parameters"];
Assert.IsNotNull( parameters );

Assert.IsNotNull( parameters.First["required"] );
Assert.AreEqual( "False", parameters.First["required"].ToString() );

Assert.IsNotNull( parameters.First["default"] );
Assert.AreEqual( "123456", parameters.First["default"].ToString() );
}

private void IncludeXmlComments(SwaggerDocsConfig config)
{
config.IncludeXmlComments(String.Format(@"{0}\XmlComments.xml", AppDomain.CurrentDomain.BaseDirectory));
Expand Down