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;
|
2017-12-07 03:36:06 +01:00
|
|
|
|
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>
|
2019-09-25 01:06:15 +02:00
|
|
|
|
/// Convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/> using default settings.
|
2017-11-11 04:31:01 +01:00
|
|
|
|
/// </summary>
|
2017-11-13 21:46:11 +01:00
|
|
|
|
/// <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>
|
2017-11-13 21:46:11 +01:00
|
|
|
|
/// <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
|
|
|
|
|
2017-12-07 03:36:06 +01:00
|
|
|
|
if (settings.VerifyEdmModel)
|
|
|
|
|
{
|
2021-11-22 18:43:05 +01:00
|
|
|
|
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()));
|
|
|
|
|
}
|
2017-12-07 03:36:06 +01:00
|
|
|
|
|
2021-11-22 18:43:05 +01:00
|
|
|
|
return document;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-07 03:36:06 +01:00
|
|
|
|
|
2021-11-22 18:43:05 +01:00
|
|
|
|
ODataContext context = new(model, settings);
|
2017-11-22 04:44:38 +01:00
|
|
|
|
return context.CreateDocument();
|
2017-11-11 04:31:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|