类型命名空间是Microsoft.OpenApi.Models
。
首先,我让我的服务在端点公开一个OpenApiDocument
,代码如下:
string serializedOpenApiDocument = this.document.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
JToken openApiDocument = JsonConvert.DeserializeObject<JToken>(
serializedOpenApiDocument,
new JsonSerializerSettings()
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore
});
return Task.FromResult<IActionResult>(this.Ok(openApiDocument));
如果检查OpenApiSchema类型的内部属性,则存在类型为IOpenApiAny的Default属性。
现在序列化部分工作得很好,但是我在序列化整个文档时遇到了麻烦,因为我得到了这样的异常:
- {“将值“HelloDarknessMyOldFriend”转换为类型“Microsoft.OpenApi.Any. IOpenApiAny '时出错。第1行,位置3065。"}*
验证代码如下所示:
var stream = httpClient.GetAsync(urlBuilder.ToString()).Result;
stream.EnsureSuccessStatusCode();
responseBody = stream.Content.ReadAsStringAsync().Result;
var swagger = JsonConvert.DeserializeObject<OpenApiDocument>(responseBody, new JsonSerializerSettings()
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
});
我怎样才能以某种方式帮助整个OpenApiDocument
的deserialization
,这样我就不会得到一个异常,因为有一些IOpenApiAny
属性在使用?
您可以使用以下代码模拟此问题:
我使用的两个核心是:
<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
var document = new OpenApiDocument();
var schema = new OpenApiSchema();
schema.Default = new OpenApiString("Hello");
document.Components = new OpenApiComponents();
document.Components.Schemas.Add("someSchema", schema);
//Serializing
string serializedOpenApiDocument = document.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
//Deserializing - throws an exception
var swagger = JsonConvert.DeserializeObject<OpenApiDocument>(serializedOpenApiDocument, new JsonSerializerSettings());
提前感谢,
1条答案
按热度按时间z9smfwbn1#
JsonSerializerDataContractResolver