Skip to content

Commit

Permalink
Testing for swagger parameter creation
Browse files Browse the repository at this point in the history
  • Loading branch information
KevM committed Feb 15, 2012
1 parent 810dc94 commit d5bb34a
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 43 deletions.
34 changes: 34 additions & 0 deletions src/FubuMVC.Swagger.Tests/Action1.cs
@@ -0,0 +1,34 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using FubuMVC.Core;
using FubuMVC.Swagger.Specification;

namespace FubuMVC.Swagger.Tests
{
[Description("An api description")]
public class ActionRequest
{
[RouteInput, Description("Namey name name"), Required]
public string input { get; set; }

[QueryString, Description("Queryee query")]
public string query { get; set; }

[Description("Fishy fish")]
[AllowableValues("value1", "value2")]
[DefaultValue("value1")]
public string redfish { get; set; }

[Required]
public bool required { get; set; }

public bool notrequired { get; set; }
}

public class Action1
{
public void Execute(ActionRequest request)
{
}
}
}
4 changes: 4 additions & 0 deletions src/FubuMVC.Swagger.Tests/FubuMVC.Swagger.Tests.csproj
Expand Up @@ -73,6 +73,7 @@
<HintPath>..\packages\structuremap.automocking.2.6.3\lib\StructureMap.AutoMocking.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -81,10 +82,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Action1.cs" />
<Compile Include="api_route_grouper.cs" />
<Compile Include="base_url_relative_to_target.cs" />
<Compile Include="Context.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="when_creating_parameters.cs" />
<Compile Include="when_creating_parameters_paramTypes.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
14 changes: 3 additions & 11 deletions src/FubuMVC.Swagger.Tests/api_route_grouper.cs
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.Resources.Conneg;
using NUnit.Framework;

namespace FubuMVC.Swagger.Tests
Expand Down Expand Up @@ -53,25 +52,18 @@ private static IEnumerable<ActionCall> getAPIActions()
{
var graph = new BehaviorGraph();

graph.AddActionFor("api/group1/{Id}", typeof (Action1));
graph.AddActionFor("api/group2/{Id}", typeof(Action1));
graph.AddActionFor("api/group1/{input}", typeof (Action1));
graph.AddActionFor("api/group2/{input}", typeof(Action1));
graph.AddActionFor("api/group2/foo/", typeof(Action1));
graph.AddActionFor("api/group3", typeof(Action1));
graph.AddActionFor("api/group3/foobar", typeof(Action1));
graph.AddActionFor("api/group3/{star}", typeof(Action1));
graph.AddActionFor("api/group3/{input}", typeof(Action1));
graph.AddActionFor("home", typeof(Action1));
graph.AddActionFor("home/foo", typeof(Action1));
graph.AddActionFor("home/foo/baz", typeof(Action1));
graph.AddActionFor("bar", typeof(Action1));

return graph.Actions();
}

public class Action1
{
public void Execute()
{
}
}
}
}
89 changes: 89 additions & 0 deletions src/FubuMVC.Swagger.Tests/when_creating_parameters.cs
@@ -0,0 +1,89 @@
using System.Linq;
using System.Reflection;
using FubuCore.Reflection;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Routes;
using FubuMVC.Swagger.Specification;
using NUnit.Framework;

namespace FubuMVC.Swagger.Tests
{
[TestFixture]
public class when_creating_parameters
{
private Parameter _result;
private PropertyInfo _property;
private IRouteDefinition _route;

[SetUp]
public void Given()
{
var graph = new BehaviorGraph();
var chain = graph.AddActionFor("api/action1/{input}/", typeof (Action1));

_route = chain.Route;
_property = ReflectionHelper.GetProperty<ActionRequest>(a => a.redfish);

_result = ActionCallMapper.createParameter(_property, _route);
}

[Test]
public void allow_multiple_should_be_false()
{
_result.allowMultiple.ShouldBeFalse();
}

[Test]
public void parameter_name_should_match_property()
{
_result.name.ShouldEqual(_property.Name);
}

[Test]
public void data_type_should_match_property()
{
_result.dataType.ShouldEqual(_property.PropertyType.Name);
}

[Test]
public void description_should_match_annotation()
{
_result.description.ShouldEqual(_property.GetAttribute<System.ComponentModel.DescriptionAttribute>().Description);
}

[Test]
public void default_value_should_match_annotation()
{
_result.defaultValue.ShouldEqual(_property.GetAttribute<System.ComponentModel.DefaultValueAttribute>().Value);
}

[Test]
public void allowable_value_should_match_annotation()
{
var allowableValues = _result.allowableValues;
allowableValues.valueType.ShouldEqual("LIST");

var values = allowableValues.values;
values.Count().ShouldEqual(2);
values[0].ShouldEqual("value1");
values[1].ShouldEqual("value2");
}

[Test]
public void required_should_be_false_when_not_annotated()
{
_result.required.ShouldBeFalse();
}

[Test]
public void required_should_be_pulled_from_data_annotation()
{
_property = ReflectionHelper.GetProperty<ActionRequest>(a => a.required);

var result = ActionCallMapper.createParameter(_property, _route);

result.required.ShouldBeTrue();
}

}
}
52 changes: 52 additions & 0 deletions src/FubuMVC.Swagger.Tests/when_creating_parameters_paramTypes.cs
@@ -0,0 +1,52 @@
using FubuCore.Reflection;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Routes;
using NUnit.Framework;

namespace FubuMVC.Swagger.Tests
{
[TestFixture]
public class when_creating_parameters_paramTypes
{
private IRouteDefinition _route;

[SetUp]
public void Given()
{
var graph = new BehaviorGraph();
var chain = graph.AddActionFor("api/action1/{input}/", typeof(Action1));

_route = chain.Route;
}

[Test]
public void for_route_input_should_have_paramType_path()
{
var property = ReflectionHelper.GetProperty<ActionRequest>(a => a.input);

var result = ActionCallMapper.createParameter(property, _route);

result.paramType.ShouldEqual("path");
}

[Test]
public void for_querystring_input_should_have_paramType_query()
{
var property = ReflectionHelper.GetProperty<ActionRequest>(a => a.query);

var result = ActionCallMapper.createParameter(property, _route);

result.paramType.ShouldEqual("query");
}

[Test]
public void for_normal_input_property_should_have_paramType_post()
{
var property = ReflectionHelper.GetProperty<ActionRequest>(a => a.redfish);

var result = ActionCallMapper.createParameter(property, _route);

result.paramType.ShouldEqual("post");
}
}
}
58 changes: 26 additions & 32 deletions src/FubuMVC.Swagger/SwaggerMapper.cs
Expand Up @@ -22,49 +22,43 @@ public ActionCallMapper(ITypeDescriptorCache typeCache)

public IEnumerable<Operation> GetSwaggerOperations(ActionCall call)
{
var route = call.ParentChain().Route;
var httpMethods = route.AllowedHttpMethods;

var parameters = getParameters(call);
var parameters = createParameters(call);
var outputType = call.OutputType();
var route = call.ParentChain().Route;

return route.AllowedHttpMethods.Select(verb =>
{
var summary = call.InputType().GetAttribute<DescriptionAttribute>(d => d.Description);
return new Operation
{
parameters = parameters.ToArray(),
httpMethod = verb,
responseTypeInternal = outputType.FullName,
responseClass = outputType.Name,
nickname = call.InputType().Name,
summary = summary,
//TODO not sure how we'd support error responses
errorResponses = new ErrorResponses[0],

var operations = new List<Operation>();
foreach (var verb in httpMethods)
{
var summary = call.InputType().GetAttribute<DescriptionAttribute>(d => d.Description);

var operation = new Operation
{
parameters = parameters.ToArray(),
httpMethod = verb,
responseTypeInternal = outputType.FullName,
responseClass = outputType.Name,
nickname = call.InputType().Name,
summary = summary,

//TODO not sure how we'd support error responses
errorResponses = new ErrorResponses[0],

//TODO get notes, nickname, summary from metadata?
};
operations.Add(operation);
}
return operations;
//TODO get notes, nickname, summary from metadata?
};
});
}

private IEnumerable<Parameter> getParameters(ActionCall call)
private IEnumerable<Parameter> createParameters(ActionCall call)
{
if (!call.HasInput) return new Parameter[0];

var inputType = call.InputType();
IEnumerable<PropertyInfo> properties = _typeCache.GetPropertiesFor(inputType).Values;
var route = call.ParentChain().Route;

return properties.Select(propertyInfo => createParameterFromProperty(propertyInfo, route));
var inputType = call.InputType();
IEnumerable<PropertyInfo> properties = _typeCache.GetPropertiesFor(inputType).Values;
return properties.Select(propertyInfo => createParameter(propertyInfo, route));
}

private static Parameter createParameterFromProperty(PropertyInfo propertyInfo, IRouteDefinition route)
public static Parameter createParameter(PropertyInfo propertyInfo, IRouteDefinition route)
{
var parameter = new Parameter
{
Expand Down

0 comments on commit d5bb34a

Please sign in to comment.