学习Source Generators之从swagger中生成类
前面学习了一些Source Generators的基础只是,接下来就来实践一下,用这个来生成我们所需要的代码。
本文将通过读取swagger.json的内容,解析并生成对应的请求响应类的代码。
创建项目
首先还是先创建两个项目,一个控制台程序,一个类库。

添加swagger文件
在控制台程序中添加Files目录,并把swagger文件放进去。别忘了还需要添加AdditionalFiles。
<ItemGroup>
<AdditionalFiles Include="Files\swagger.json" />
</ItemGroup>

实现ClassFromSwaggerGenerator
安装依赖
由于我们需要解析swagger,所以需要安装一下JSON相关的包。这里我们安装了Newtonsoft.Json。
需要注意的是,依赖第三方包的时候需要在项目文件添加下面内容:
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>
<Target Name="GetDependencyTargetPaths" AfterTargets="ResolvePackageDependenciesForBuild">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="@(ResolvedCompileFileDefinitions)" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>
否则编译时会出现FileNotFound的异常。
构建管道
这里我们通过AdditionalTextsProvider筛选以及过滤我们的swagger文件。
var pipeline = context.AdditionalTextsProvider.Select(static (text, cancellationToken) =>
{
if (!text.Path.EndsWith("swagger.json", StringComparison.OrdinalIgnoreCase))
{
return default;
}
return JObject.Parse(text.GetText(cancellationToken)!.ToString());
})
.Where((pair) => pair is not null);
实现生成代码逻辑
接下来我们就解析Swagger中的内容,并且动态拼接代码内容。主要代码部分如下:
context.RegisterSourceOutput(pipeline, static (context, swagger) =>
{
List<(string name, string sourceString)> sources = new List<(string name, string sourceString)>();
#region 生成实体
var schemas = (JObject)swagger["components"]!["schemas"]!;
foreach (JProperty item in schemas.Properties())
{
if (item != null)
{
sources.Add((HandleClassName(item.Name), $@"#nullable enable
using System;
using System.Collections.Generic;
namespace SwaggerEntities;
public {ClassOrEnum((JObject)item.Value)} {HandleClassName(item.Name)}
{{
{BuildProperty((JObject)item.Value)}
}}
"));
}
}
foreach (var (name, sourceString) in sources)
{
var sourceText = SourceText.From(sourceString, Encoding.UTF8);
context.AddSource($"{name}.g.cs", sourceText);
}
#endregion
});
完整的代码如下:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace GenerateClassFromSwagger.Analysis
{
[Generator]
public class ClassFromSwaggerGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var pipeline = context.AdditionalTextsProvider.Select(static (text, cancellationToken) =>
{
if (!text.Path.EndsWith("swagger.json", StringComparison.OrdinalIgnoreCase))
{
return default;
}
return JObject.Parse(text.GetText(cancellationToken)!.ToString());
})
.Where((pair) => pair is not null);
context.RegisterSourceOutput(pipeline, static (context, swagger) =>
{
List<(string name, string sourceString)> sources = new List<(string name, string sourceString)>();
#region 生成实体
var schemas = (JObject)swagger["components"]!["schemas"]!;
foreach (JProperty item in schemas.Properties())
{
if (item != null)
{
sources.Add((HandleClassName(item.Name), $@"#nullable enable
using System;
using System.Collections.Generic;
namespace SwaggerEntities;
public {ClassOrEnum((JObject)item.Value)} {HandleClassName(item.Name)}
{{
{BuildProperty((JObject)item.Value)}
}}
"));
}
}
foreach (var (name, sourceString) in sources)
{
var sourceText = SourceText.From(sourceString, Encoding.UTF8);
context.AddSource($"{name}.g.cs", sourceText);
}
#endregion
});
}
static string HandleClassName(string name)
{
return name.Split('.').Last().Replace("<", "").Replace(">", "").Replace(",", "");
}
static string ClassOrEnum(JObject value)
{
return value.ContainsKey("enum") ? "enum" : "partial class";
}
static string BuildProperty(JObject value)
{
var sb = new StringBuilder();
if (value.ContainsKey("properties"))
{
var propertys = (JObject)value["properties"]!;
foreach (JProperty item in propertys!.Properties())
{
sb.AppendLine($@"
public {BuildProertyType((JObject)item.Value)} {ToUpperFirst(item.Name)} {{ get; set; }}
");
}
}
if (value.ContainsKey("enum"))
{
foreach (var item in JsonConvert.DeserializeObject<List<int>>(value["enum"]!.ToString())!)
{
sb.Append($@"
_{item},
");
}
sb.Remove(sb.Length - 1, 1);
}
return sb.ToString();
}
static string BuildProertyType(JObject value)
{
;
var type = GetType(value);
var nullable = value.ContainsKey("nullable") ? value["nullable"]!.Value<bool?>() switch
{
true => "?",
false => "",
_ => ""
} : "";
return type + nullable;
}
static string GetType(JObject value)
{
return value.ContainsKey("type") ? value["type"]!.Value<string>() switch
{
"string" => "string",
"boolean" => "bool",
"number" => value["format"]!.Value<string>() == "float" ? "float" : "double",
"integer" => value["format"]!.Value<string>() == "int32" ? "int" : "long",
"array" => ((JObject)value["items"]!).ContainsKey("items") ?
$"List<{HandleClassName(value["items"]!["$ref"]!.Value<string>()!)}>"
: $"List<{GetType((JObject)value["items"]!)}>",
"object" => value.ContainsKey("additionalProperties") ? $"Dictionary<string, {GetType((JObject)value["additionalProperties"]!)}>" : "object",
_ => "object"
} : value.ContainsKey("$ref") ? HandleClassName(value["$ref"]!.Value<string>()!) : "object";
}
static unsafe string ToUpperFirst(string str)
{
if (str == null) return null;
string ret = string.Copy(str);
fixed (char* ptr = ret)
*ptr = char.ToUpper(*ptr);
return ret;
}
}
}
详细的处理过程大家可以仔细看看代码,这里就不一一解释了。
启动编译
接下来编译控制台程序。编译成功后可以看到生成了很多cs的代码。若是看不见,可以重启VS。

点开一个文件,可以看到内容,并且在上方提示自动生成,无法编辑。

到这我们就完成了通过swagger来生成我们的请求和响应类的功能。
结语
本文章应用SourceGenerator,在编译时读取swagger.json的内容并解析,成功生成了我们API的请求和响应类的代码。
我们可以发现,代码生成没有问题,无法移动或者编辑生成的代码。
下一篇文章我们就来学习下如何输出SourceGenerator生成的代码文件到我们的文件目录。
本文代码仓库地址https://github.com/fanslead/Learn-SourceGenerator
学习Source Generators之从swagger中生成类的更多相关文章
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- 《CoffeeScript应用开发》学习:第五章 CoffeeScript中的类
在CoffeeScript中定义类 在CoffeeScript中,使用class定义类,使用关键字new实例化对象. 给类绑定方法 class Airplane takeOff: -> cons ...
- 使用 MVVM Toolkit Source Generators
关于 MVVM Toolkit 最近 .NET Community Toolkit 发布了 8.0.0 preview1,它包含了从 Windows Community Toolkit 迁移过来的以下 ...
- .NET初探源代码生成(Source Generators)
前言 Source Generators顾名思义代码生成器,可进行创建编译时代码,也就是所谓的编译时元编程,这可让一些运行时映射的代码改为编译时,同样也加快了速度,我们可避免那种昂贵的开销,这是有价值 ...
- Spring Boot中使用Swagger CodeGen生成REST client
文章目录 什么是Open API规范定义文件呢? 生成Rest Client 在Spring Boot中使用 API Client 配置 使用Maven plugin 在线生成API Spring B ...
- 【学习笔记】Java中生成对象的5中方法
概述:本文介绍以下java五种创建对象的方式: 1.用new语句创建对象,这是最常用的创建对象的方式. 2.使用Class类的newInstance方法 3.运用反射手段,调用java.lang.re ...
- golang学习笔记7 使用beego swagger 实现API自动化文档
golang学习笔记7 使用beego swagger 实现API自动化文档 API 自动化文档 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/doc ...
- ASP.NET Core 2 学习笔记(十三)Swagger
Swagger也算是行之有年的API文件生成器,只要在API上使用C#的<summary />文件注解标签,就可以产生精美的线上文件,并且对RESTful API有良好的支持.不仅支持生成 ...
- Swagger自动生成接口文档
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- Python中生成随机数
目录 1. random模块 1.1 设置随机种子 1.2 random模块中的方法 1.3 使用:生成整形随机数 1.3 使用:生成序列随机数 1.4 使用:生成随机实值分布 2. numpy.ra ...
随机推荐
- WriteFile 奇怪的现象
项目中有个需求是要对文本内容检索并重写,我们使用的是 WriteFile 覆盖旧的文本内容 最小示例: #include <Windows.h> #include <iostream ...
- 使用Java线程同步工具类CountDownLatch
java.util.concurrent.CountDownLatch是Java并发并发编程中的线程同步工具类,基于AQS(java.util.concurrent.locks.AbstractQue ...
- zip压缩模块,tarfile压缩模块,包和模块,format格式化的复习--day17
1.zipfile模块 import zipfile #导入模块 1.压缩文件 (1)创建压缩包 参数1压缩包名字,参数2以w模式创建,参数3压缩固定写法 zf = zipfile.ZipFile(& ...
- 【LeetCode回溯算法#11】解数独,这次是真的用回溯法处理二维数组
解数独 力扣题目链接(opens new window) 编写一个程序,通过填充空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出 ...
- 【LeetCode链表#12】链表相交
链表相交 同:160.链表相交 力扣题目链接(opens new window) 给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点.如果两个链表没有交点,返 ...
- nest.sh 脚本 发布服务
每次发布后端nest 直接执行一个脚本即可 给脚本赋值权限 chomd 777 nest.sh nest.sh 脚本 #!/bin/bash cd /root/gateway-study git pu ...
- hesitation 单词学习 犹豫 hes 就是 her 粘 助记单词 here
hesitation 单词学习 犹豫 hes 就是 her 粘 助记单词 here hes + itation(ite + ate + ion) hesitation 美: [ˌhezɪˈteɪʃ(ə ...
- WPF之模板
目录 模板的内涵 数据的外衣DataTemplate UserControl例子 DataTemplate例子 控件的外衣 ControlTemplate 解剖控件 ItemsControl的Pane ...
- MySQL 双主集群搭建
搭建 MySQL 双主集群涉及多个配置步骤,以及对于可能出现的问题的理解和解决.下面将详细说明搭建过程的每个步骤. 前提条件 环境准备:准备两台服务器(物理或虚拟),并确保它们可以互相通信(例如,通过 ...
- Base MYSQL Database create stored procedures resolve the Delimiter error
Base MYSQL Database create stored procedures resolve the Delimiter error, It must be created using a ...