Skip to content

Commit 42885de

Browse files
committed
complete swagger integration
1 parent 5d3dc2a commit 42885de

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

.circleci/config.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ jobs:
2020
- run: dotnet build -c Release -p:Version=${CIRCLE_TAG}
2121
- run: dotnet test -c Release --no-build
2222
- run: dotnet pack ./src/MicroBatchFramework/MicroBatchFramework.csproj -c Release --no-build -p:Version=${CIRCLE_TAG}
23+
- run: dotnet pack ./src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj -c Release --no-build -p:Version=${CIRCLE_TAG}
2324
- store_artifacts:
2425
path: ./src/MicroBatchFramework/bin/Release
25-
destination: .
26+
destination: ./MicroBatchFramework/
27+
- store_artifacts:
28+
path: ./src/MicroBatchFramework.WebHosting/bin/Release
29+
destination: ./MicroBatchFramework.WebHosting/
2630
- run: dotnet nuget push ./src/MicroBatchFramework/bin/Release/MicroBatchFramework.${CIRCLE_TAG}.nupkg -s https://www.nuget.org/api/v2/package -k ${NUGET_KEY}
31+
- run: dotnet nuget push ./src/MicroBatchFramework.WebHosting/bin/Release/MicroBatchFramework.WebHosting.${CIRCLE_TAG}.nupkg -s https://www.nuget.org/api/v2/package -k ${NUGET_KEY}
2732
workflows:
2833
version: 2
2934
build-and-push:

sandbox/WebHostingApp/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@ public void Sum(int x, int y)
2626
{
2727
Context.Logger.LogInformation((x + y).ToString());
2828
}
29+
public void SimpleInt(int input)
30+
{
31+
Context.Logger.LogInformation("In: " + input);
32+
}
2933
public void SimpleString(string input)
3034
{
35+
Context.Logger.LogInformation("in is null?: " + (input == null));
3136
Context.Logger.LogInformation("In: " + input);
3237
}
3338
public void SimpleObject(Person person)
3439
{
3540
Context.Logger.LogInformation(person.Name + ":" + person.Age);
3641
}
3742

43+
public void SimpleEnum(MyFruit fruit)
44+
{
45+
Context.Logger.LogInformation(fruit.ToString());
46+
}
47+
3848
public void InOut(string input, Person person)
3949
{
4050
Context.Logger.LogInformation(person.Name + ":" + person.Age);
@@ -46,6 +56,21 @@ public void SimpleArray(int[] simpleArray)
4656
Context.Logger.LogInformation(string.Join(", ", simpleArray));
4757
}
4858

59+
public void StringArray(string[] simpleArray)
60+
{
61+
Context.Logger.LogInformation(string.Join(", ", simpleArray));
62+
}
63+
public void OjectArray(Person[] objectArray)
64+
{
65+
Context.Logger.LogInformation(string.Join(", ", objectArray.Select(x =>(x==null) ? "nul" : x.Age + ":" + x.Name)));
66+
}
67+
68+
public void DefaultV(int x = 100, int y = 200, string foo = null)
69+
{
70+
Context.Logger.LogInformation("X: " + x);
71+
Context.Logger.LogInformation("Y: " + y);
72+
Context.Logger.LogInformation("Foo: " + foo);
73+
}
4974

5075
public void Array(int[] intArray, string[] stringArray, Person[] objectArray, MyFruit[] fruits)
5176
{

src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public async Task Invoke(HttpContext httpContext)
127127
foreach (var item in httpContext.Request.Form)
128128
{
129129
args[i++] = "-" + item.Key;
130-
args[i++] = (item.Value.Count == 0) ? null : item.Value[0];
130+
args[i++] = (item.Value.Count == 0) ? null
131+
: (item.Value.Count == 1) ? item.Value[0]
132+
: "[" + string.Join(", ", item.Value) + "]";
131133
}
132134
}
133135
}

src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
140140

141141
var collectionType = GetCollectionType(x.ParameterType);
142142
var items = collectionType != null
143-
? new PartialSchema { type = ToSwaggerDataType(collectionType), }
143+
? new PartialSchema { type = ToSwaggerDataType(collectionType) }
144144
: null;
145145

146146
string defaultObjectExample = null;
@@ -169,31 +169,22 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
169169
{
170170
BuildSchema(definitions, x.ParameterType);
171171
refSchema = new Schema { @ref = BuildSchema(definitions, x.ParameterType) };
172-
if (parameterInfos.Length != 1)
173-
{
174-
var unknownObj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(x.ParameterType);
175-
if (collectionType != null)
176-
{
177-
defaultObjectExample = JsonConvert.SerializeObject(new[] { unknownObj }, new[] { new Newtonsoft.Json.Converters.StringEnumConverter() });
178-
}
179-
else
180-
{
181-
defaultObjectExample = JsonConvert.SerializeObject(unknownObj, new[] { new Newtonsoft.Json.Converters.StringEnumConverter() });
182-
}
183-
}
172+
var unknownObj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(x.ParameterType);
173+
defaultObjectExample = JsonConvert.SerializeObject(unknownObj, new[] { new Newtonsoft.Json.Converters.StringEnumConverter() });
174+
swaggerDataType = "string"; // object can not attach formData.
184175
}
185176

186177
return new Schemas.Parameter
187178
{
188179
name = x.Name,
189-
@in = parameterInfos.Length == 1 ? "body" : "formData",
180+
@in = "formData",
190181
type = swaggerDataType,
191182
description = parameterXmlComment,
192183
required = !x.IsOptional,
193184
@default = defaultObjectExample ?? ((x.IsOptional) ? defaultValue : null),
194185
items = items,
195186
@enum = enums,
196-
collectionFormat = "multi",
187+
collectionFormat = "multi", // csv or multi
197188
schema = refSchema
198189
};
199190
})
@@ -286,7 +277,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
286277
schema = new Schema
287278
{
288279
type = "object",
289-
properties = props
280+
properties = props,
290281
};
291282

292283
definitions.Add(fullName, schema);

src/MicroBatchFramework/BatchEngine.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,37 @@ static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int
197197
invokeArgs[i] = value.Value;
198198
continue;
199199
}
200+
else if (parameters[i].ParameterType.IsEnum)
201+
{
202+
try
203+
{
204+
invokeArgs[i] = Enum.Parse(parameters[i].ParameterType, value.Value, true);
205+
continue;
206+
}
207+
catch
208+
{
209+
errorMessage = "Parameter \"" + item.Name + "\"" + " fail on Enum parsing.";
210+
return false;
211+
}
212+
}
213+
else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(parameters[i].ParameterType))
214+
{
215+
var v = value.Value;
216+
if (!(v.StartsWith("[") && v.EndsWith("]")))
217+
{
218+
v = "[" + v + "]";
219+
}
220+
try
221+
{
222+
invokeArgs[i] = JsonSerializer.NonGeneric.Deserialize(parameters[i].ParameterType, v);
223+
continue;
224+
}
225+
catch
226+
{
227+
errorMessage = "Parameter \"" + item.Name + "\"" + " fail on JSON deserialize, plaease check type or JSON escape or add double-quotation.";
228+
return false;
229+
}
230+
}
200231
else
201232
{
202233
// decouple dependency?
@@ -207,7 +238,7 @@ static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int
207238
}
208239
catch
209240
{
210-
errorMessage = "Parameter \"" + item.Name + "\"" + " fail on JSON deserialize, plaease check type or JSON escape.";
241+
errorMessage = "Parameter \"" + item.Name + "\"" + " fail on JSON deserialize, plaease check type or JSON escape or add double-quotation.";
211242
return false;
212243
}
213244
}

0 commit comments

Comments
 (0)