OpenAPI.NET.OData/src/Microsoft.OpenApi.OData.Reader/EdmModelOpenApiExtensions.cs

63 lines
2.3 KiB
C#
Raw Permalink Normal View History

2017-11-14 23:41:19 +01:00
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
2017-11-11 04:31:01 +01:00
2018-01-04 20:24:38 +01:00
using System.Collections.Generic;
2017-11-11 04:31:01 +01:00
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Validation;
using Microsoft.OpenApi.Any;
2017-11-11 04:31:01 +01:00
using Microsoft.OpenApi.Models;
2018-08-08 19:18:01 +02:00
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
2017-11-21 23:47:38 +01:00
using Microsoft.OpenApi.OData.Generator;
2017-11-11 04:31:01 +01:00
namespace Microsoft.OpenApi.OData
{
/// <summary>
2017-11-14 23:41:19 +01:00
/// Extension methods to convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/>.
2017-11-11 04:31:01 +01:00
/// </summary>
2017-11-21 23:47:38 +01:00
public static class EdmModelOpenApiExtensions
2017-11-11 04:31:01 +01:00
{
/// <summary>
/// Convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/> using default settings.
2017-11-11 04:31:01 +01:00
/// </summary>
/// <param name="model">The Edm model.</param>
2017-12-06 03:28:34 +01:00
/// <returns>The converted Open API document object, <see cref="OpenApiDocument"/>.</returns>
2017-11-21 23:47:38 +01:00
public static OpenApiDocument ConvertToOpenApi(this IEdmModel model)
{
return model.ConvertToOpenApi(new OpenApiConvertSettings());
}
2017-11-13 21:43:08 +01:00
/// <summary>
2017-12-06 03:28:34 +01:00
/// Convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/> using a convert settings.
2017-11-13 21:43:08 +01:00
/// </summary>
/// <param name="model">The Edm model.</param>
2017-12-06 03:28:34 +01:00
/// <param name="settings">The convert settings.</param>
/// <returns>The converted Open API document object, <see cref="OpenApiDocument"/>.</returns>
2017-11-21 23:47:38 +01:00
public static OpenApiDocument ConvertToOpenApi(this IEdmModel model, OpenApiConvertSettings settings)
2017-11-11 04:31:01 +01:00
{
2018-09-18 23:35:57 +02:00
Utils.CheckArgumentNull(model, nameof(model));
Utils.CheckArgumentNull(settings, nameof(settings));
2017-11-21 23:47:38 +01:00
if (settings.VerifyEdmModel)
{
if (!model.Validate(out var errors))
{
OpenApiDocument document = new();
int index = 1;
foreach (var error in errors)
{
document.Extensions.Add(Constants.xMsEdmModelError + index++, new OpenApiString(error.ToString()));
}
return document;
}
}
ODataContext context = new(model, settings);
2017-11-22 04:44:38 +01:00
return context.CreateDocument();
2017-11-11 04:31:01 +01:00
}
}
}