Skip to content

Commit c878896

Browse files
committed
swagger
1 parent c7fe719 commit c878896

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

src/Built.Grpcc.SwaggerGen/Swagger/SwaggerDefinitionBuilder.cs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ public byte[] BuildSwaggerJson()
3636
{
3737
try
3838
{
39-
if (options.XmlDocumentPath != null && !File.Exists(options.XmlDocumentPath))
39+
var xmlList = new List<XmlCommentStructure>();
40+
foreach (var srvXml in handlers.SrvGroup)
4041
{
41-
return Encoding.UTF8.GetBytes("Xml doesn't exists at " + options.XmlDocumentPath);
42+
var xmlDocumentPath = srvXml.Value;
43+
xmlDocumentPath = xmlDocumentPath.Substring(0, xmlDocumentPath.Length - 4) + ".xml";
44+
if (File.Exists(xmlDocumentPath))
45+
{
46+
var lookUp = BuildXmlMemberCommentStructureList(xmlDocumentPath);
47+
xmlList = xmlList.Concat(lookUp).ToList();
48+
}
4249
}
43-
44-
xDocLookup = (options.XmlDocumentPath != null)
45-
? BuildXmlMemberCommentStructure(options.XmlDocumentPath)
46-
: null;
50+
var methodList = handlers.MethodList();
51+
xDocLookup = xmlList.ToLookup(x => Tuple.Create(x.ClassName, x.MethodName));
4752

4853
var doc = new SwaggerDocument();
4954
doc.info = options.Info;
@@ -58,7 +63,7 @@ public byte[] BuildSwaggerJson()
5863
? BuildXmlTypeSummary(options.XmlDocumentPath)
5964
: null;
6065

61-
doc.tags = handlers.Descriptor.Keys
66+
doc.tags = methodList.Select(t => t.Service.FullName).Distinct()
6267
.Select(x =>
6368
{
6469
string desc = null;
@@ -74,13 +79,12 @@ public byte[] BuildSwaggerJson()
7479
})
7580
.ToArray();
7681

77-
// Unary only
78-
foreach (var item in handlers.MethodList().Where(x => x.IsClientStreaming == false && x.IsServerStreaming == false))
82+
foreach (var item in handlers.MethodList())
7983
{
8084
XmlCommentStructure xmlComment = null;
8185
if (xDocLookup != null)
8286
{
83-
xmlComment = xDocLookup[Tuple.Create(item.Service.FullName, item.Name)].FirstOrDefault();
87+
xmlComment = xDocLookup[Tuple.Create(item.Service.Name, item.Name)].FirstOrDefault();
8488
}
8589

8690
var parameters = BuildParameters(doc.definitions, xmlComment, item);
@@ -354,6 +358,37 @@ private static Type GetCollectionType(Type type)
354358
return null; // not collection
355359
}
356360

361+
private static IEnumerable<XmlCommentStructure> BuildXmlMemberCommentStructureList(string xmlDocumentPath)
362+
{
363+
var file = File.ReadAllText(xmlDocumentPath);
364+
var xDoc = XDocument.Parse(file);
365+
var xDocLookup = xDoc.Descendants("member")
366+
.Where(x => x.Attribute("name").Value.StartsWith("M:"))
367+
.Select(x =>
368+
{
369+
var match = Regex.Match(x.Attribute("name").Value, @"(\w+)\.(\w+)?(\(.+\)|$)");
370+
371+
var summary = ((string)x.Element("summary")) ?? "";
372+
var returns = ((string)x.Element("returns")) ?? "";
373+
var remarks = ((string)x.Element("remarks")) ?? "";
374+
var parameters = x.Elements("param")
375+
.Select(e => Tuple.Create(e.Attribute("name").Value, e))
376+
.Distinct(new Item1EqualityCompaerer<string, XElement>())
377+
.ToDictionary(e => e.Item1, e => e.Item2.Value.Trim());
378+
379+
return new XmlCommentStructure
380+
{
381+
ClassName = match.Groups[1].Value,
382+
MethodName = match.Groups[2].Value,
383+
Summary = summary.Trim(),
384+
Remarks = remarks.Trim(),
385+
Parameters = parameters,
386+
Returns = returns.Trim()
387+
};
388+
});
389+
return xDocLookup;
390+
}
391+
357392
private static ILookup<Tuple<string, string>, XmlCommentStructure> BuildXmlMemberCommentStructure(string xmlDocumentPath)
358393
{
359394
var file = File.ReadAllText(xmlDocumentPath);

src/Ocelot.GrpcHttpGateway/Built.Grpcc/GrpcPluginFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Task LoadAsync(string fileFullPath)
6969
byte[] assemblyBuf = File.ReadAllBytes(fileFullPath);
7070
var assembly = Assembly.Load(assemblyBuf);
7171
var types = assembly.GetTypes();
72-
serviceDescriptor.AddGrpcDescript(Path.GetFileNameWithoutExtension(fileFullPath), types);
72+
serviceDescriptor.AddGrpcDescript(fileFullPath, types);
7373
logger.LogDebug($"Run End[{fileFullPath}]");
7474
});
7575
}

src/Ocelot.GrpcHttpGateway/Built.Grpcc/ServiceDescriptor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ public class ServiceDescriptor
1515

1616
public ConcurrentDictionary<string, string> SrvGroup { get; } = new ConcurrentDictionary<string, string>();
1717

18-
public virtual void AddGrpcDescript(string group, Type[] types)
18+
public virtual void AddGrpcDescript(string dll, Type[] types)
1919
{
20-
if (string.IsNullOrWhiteSpace(group)) group = "default";
2120
var fileTypes = types.Where(type => type.Name.EndsWith("Reflection"));
2221
foreach (var type in fileTypes)
2322
{
@@ -37,7 +36,7 @@ public virtual void AddGrpcDescript(string group, Type[] types)
3736
methodDic.TryAdd(method.Name.ToUpper(), method);
3837
}
3938
Descriptor.AddOrUpdate(srvName, methodDic);
40-
SrvGroup.AddOrUpdate(srvName, group);
39+
SrvGroup.AddOrUpdate(srvName, dll);
4140
//if (Descriptor.ContainsKey(srvName))
4241
// continue;
4342
//if (Descriptor.TryAdd(srvName, new ConcurrentDictionary<string, MethodDescriptor>()))

0 commit comments

Comments
 (0)