swagger C#使用IOpenApiAny实现OpenApiDocument的Object

lztngnrs  于 2023-10-18  发布在  C#
关注(0)|答案(1)|浏览(204)

类型命名空间是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类型的内部属性,则存在类型为IOpenApiAnyDefault属性。
现在序列化部分工作得很好,但是我在序列化整个文档时遇到了麻烦,因为我得到了这样的异常:

  • {“将值“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,
                });

我怎样才能以某种方式帮助整个OpenApiDocumentdeserialization,这样我就不会得到一个异常,因为有一些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());

提前感谢,

z9smfwbn

z9smfwbn1#

JsonSerializerDataContractResolver

_serializerDataContractResolver = new JsonSerializerDataContractResolver(new JsonSerializerOptions());

var headersObject = JsonConvert.DeserializeObject<Dictionary<string, OpenApiHeaderExtend>>(headers);
response.Headers = headersObject.ToDictionary(
    ho => ho.Key, 
    ho =>
    {
        var type = ho.Value.Schema.Example.GetType();
        var dataContract = _serializerDataContractResolver.GetDataContractForType(type);
        var exampleAsJson = dataContract.JsonConverter(ho.Value.Schema.Example);
        
        return new OpenApiHeader
        {
            Description = ho.Value.Description,
            Schema = new OpenApiSchema
            {
                Type = ho.Value.Schema.Type,
                Example = OpenApiAnyFactory.CreateFromJson(exampleAsJson)
            }
        };
    });

public class OpenApiHeaderExtend : OpenApiHeader
{
    public new OpenApiSchemaExtend Schema { get; set; }
}

public class OpenApiSchemaExtend : OpenApiSchema
{
    public new object Example { get; set; }
}

相关问题