Skip to content

Commit 6ccd92f

Browse files
committed
Initial controllers serialization
1 parent 9cbe26c commit 6ccd92f

File tree

8 files changed

+93
-3
lines changed

8 files changed

+93
-3
lines changed

src/Simplify.Web.Postman/CollectionModelSerializer.cs renamed to src/Simplify.Web.Postman/Json/CollectionModelSerializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public class CollectionModelSerializer
1515
/// <returns></returns>
1616
public string Serialize(CollectionModel model)
1717
{
18-
return JsonSerializer.Serialize(model);
18+
return JsonSerializer.Serialize(model, new JsonSerializerOptions
19+
{
20+
PropertyNamingPolicy = new LowerCamelCasePolicy(),
21+
WriteIndented = true
22+
});
1923
}
2024
}
2125
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Text.Json;
2+
3+
namespace Simplify.Web.Postman
4+
{
5+
public class LowerCamelCasePolicy : JsonNamingPolicy
6+
{
7+
public override string ConvertName(string name)
8+
{
9+
if (name.Length > 0)
10+
return name.Substring(0, 1).ToLower() + name.Substring(1);
11+
12+
return "";
13+
}
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Simplify.Web.Postman.Models
2+
{
3+
public class CollectionItem
4+
{
5+
public string Name { get; set; }
6+
7+
public Request Request { get; set; }
8+
}
9+
}

src/Simplify.Web.Postman/Models/CollectionModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace Simplify.Web.Postman.Models
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Simplify.Web.Postman.Models
25
{
36
/// <summary>
47
/// Provides postman collection model
@@ -12,5 +15,8 @@ public class CollectionModel
1215
/// The header.
1316
/// </value>
1417
public CollectionHeader? Header { get; set; }
18+
19+
[JsonPropertyName("item")]
20+
public IList<CollectionItem> Items { get; set; } = new List<CollectionItem>();
1521
}
1622
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Simplify.Web.Postman.Models
2+
{
3+
public class Request
4+
{
5+
public string Method { get; set; }
6+
7+
public Url Url { get; set; }
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Simplify.Web.Postman.Models
2+
{
3+
public class Url
4+
{
5+
public string Raw { get; set; }
6+
}
7+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Linq;
2+
using Simplify.Web.Meta;
3+
using Simplify.Web.Postman.Models;
4+
5+
namespace Simplify.Web.Postman
6+
{
7+
public class CollectionItemsBuilder : ICollectionPartBuilder
8+
{
9+
public void Build(CollectionModel model)
10+
{
11+
foreach (var item in ControllersMetaStore.Current.ControllersMetaData)
12+
{
13+
model.Items.Add(BuildCollectionItem(item));
14+
}
15+
}
16+
17+
private static CollectionItem BuildCollectionItem(IControllerMetaData metaData)
18+
{
19+
var route = metaData.ExecParameters.Routes.FirstOrDefault();
20+
21+
var item = new CollectionItem
22+
{
23+
Name = metaData.ControllerType.Name,
24+
Request = new Request
25+
{
26+
Url = new Url
27+
{
28+
Raw = route.Value
29+
},
30+
Method = route.Key.ToString().ToUpper()
31+
}
32+
};
33+
34+
return item;
35+
}
36+
}
37+
}

src/Simplify.Web.Postman/SimplifyDIRegistratorExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public static IDIRegistrator RegisterSimplifyWebPostman(this IDIRegistrator regi
2020
{
2121
settings = DefaultPostmanGenerationSettingsFactory.CreateOrUpdateSettings(settings, Assembly.GetCallingAssembly().GetName().Name);
2222

23-
registrator.Register(r => new CollectionBuilder(new List<ICollectionPartBuilder> { }))
23+
registrator.Register(r => new CollectionBuilder(new List<ICollectionPartBuilder>
24+
{
25+
new CollectionItemsBuilder()
26+
}))
2427
.Register<ICollectionExporter>(r => new FileCollectionExporter(r.Resolve<CollectionModelSerializer>(), r.Resolve<IEnvironment>(),
2528
settings))
2629
.Register<CollectionModelSerializer>()

0 commit comments

Comments
 (0)