Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use convert setting to toggle between referencing @odata.count and @odata.nextLink #288

Merged
merged 5 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,19 @@ public static IDictionary<string, OpenApiSchema> CreateSchemas(this ODataContext
};

// @odata.nextLink + @odata.count
if (context.Settings.EnablePagination || context.Settings.EnableCount)
if ((context.Settings.EnablePagination || context.Settings.EnableCount) &&
context.Settings.RefBaseCollectionPaginationCountResponse)
{
schemas[Constants.BaseCollectionPaginationCountResponse] = new()
{
Title = "Base collection pagination and count responses",
Type = Constants.ObjectType,
};
if (context.Settings.EnableCount) schemas[Constants.BaseCollectionPaginationCountResponse].Properties.Add(ODataConstants.OdataCount);
if (context.Settings.EnablePagination) schemas[Constants.BaseCollectionPaginationCountResponse].Properties.Add(ODataConstants.OdataNextLink);

if (context.Settings.EnableCount)
schemas[Constants.BaseCollectionPaginationCountResponse].Properties.Add(ODataConstants.OdataCount);
if (context.Settings.EnablePagination)
schemas[Constants.BaseCollectionPaginationCountResponse].Properties.Add(ODataConstants.OdataNextLink);
}

return schemas;
Expand Down Expand Up @@ -228,29 +232,41 @@ private static OpenApiSchema CreateCollectionSchema(ODataContext context, OpenAp
Properties = properties
};


// @odata.nextLink + @odata.count
OpenApiSchema paginationCountSchema = new()
{
UnresolvedReference = true,
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = Constants.BaseCollectionPaginationCountResponse
}
};

OpenApiSchema colSchema;
if (context.Settings.EnablePagination || context.Settings.EnableCount)
{
colSchema = new OpenApiSchema
if (context.Settings.RefBaseCollectionPaginationCountResponse)
{
AllOf = new List<OpenApiSchema>
// @odata.nextLink + @odata.count
OpenApiSchema paginationCountSchema = new()
{
paginationCountSchema,
baseSchema
}
};
UnresolvedReference = true,
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = Constants.BaseCollectionPaginationCountResponse
}
};

colSchema = new OpenApiSchema
{
AllOf = new List<OpenApiSchema>
{
paginationCountSchema,
baseSchema
}
};
}
else
{
if (context.Settings.EnablePagination)
baseSchema.Properties.Add(ODataConstants.OdataNextLink);

if (context.Settings.EnableCount)
baseSchema.Properties.Add(ODataConstants.OdataCount);

colSchema = baseSchema;
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,13 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PackageId>Microsoft.OpenApi.OData</PackageId>
<SignAssembly>true</SignAssembly>
<Version>1.1.0</Version>
<Version>1.2.0-preview1</Version>
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
<RepositoryUrl>https://github.com/Microsoft/OpenAPI.NET.OData</RepositoryUrl>
<PackageReleaseNotes>
- Fixes response schemas of actions and functions that return a collection to contain the nextLink property #231
- Uses Computed annotation in the CSDL to declare property as ReadOnly #254
- Fixes duplicated actions/functions request body/response schemas #241
- Adds documentation links from Links annotations in CSDL to OpenAPI Operations #230
- Sets explode property to false and the style property to form for OData query options #274
- Aliases optional parameters of OData functions #259
- Adds @odata.count parameter to collection responses #196
- Appends OData query parameters to functions #251 #252
- Replaces instances of anyOf to oneOf #245
- Retrieves RestrictedProperties annotations annotated directly on navigations properties #249
- Add x-ms-navigationProperty extension to navigation properties #278
- Use convert setting to toggle between referencing @odata.count and @odata.nextLink #282
</PackageReleaseNotes>
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>
Expand Down
8 changes: 7 additions & 1 deletion src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public class OpenApiConvertSettings
/// </summary>
public bool EnableCount { get; set; }

/// <summary>
/// Gets/sets a value indicating whether or not to reference @odata.nextLink and @odata.count in responses
/// </summary>
public bool RefBaseCollectionPaginationCountResponse { get; set; } = true;

/// <summary>
/// Gets/sets a value that specifies the name of the operation for retrieving the next page in a collection of entities.
/// </summary>
Expand Down Expand Up @@ -329,7 +334,8 @@ internal OpenApiConvertSettings Clone()
CustomHttpMethodLinkRelMapping = this.CustomHttpMethodLinkRelMapping,
AppendBoundOperationsOnDerivedTypeCastSegments = this.AppendBoundOperationsOnDerivedTypeCastSegments,
UseSuccessStatusCodeRange = this.UseSuccessStatusCodeRange,
EnableCount = this.EnableCount
EnableCount = this.EnableCount,
RefBaseCollectionPaginationCountResponse = this.RefBaseCollectionPaginationCountResponse
};

return newSettings;
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Microsoft.OpenApi.OData.OpenApiConvertSettings.ExpandDerivedTypesNavigationPrope
Microsoft.OpenApi.OData.OpenApiConvertSettings.ExpandDerivedTypesNavigationProperties.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.CustomXMLAttributesMapping.get -> System.Collections.Generic.Dictionary<string, string>
Microsoft.OpenApi.OData.OpenApiConvertSettings.CustomXMLAttributesMapping.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.RefBaseCollectionPaginationCountResponse.get -> bool
Microsoft.OpenApi.OData.OpenApiConvertSettings.RefBaseCollectionPaginationCountResponse.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.RequireRestrictionAnnotationsToGenerateComplexPropertyPaths.get -> bool
Microsoft.OpenApi.OData.OpenApiConvertSettings.RequireRestrictionAnnotationsToGenerateComplexPropertyPaths.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.ShowExternalDocs.get -> bool
Expand Down
20 changes: 12 additions & 8 deletions src/OoasGui/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
Expand Down Expand Up @@ -32,10 +32,13 @@ public partial class MainForm : Form

private IEdmModel EdmModel { get; set; }

private OpenApiDocument _document;

public MainForm()
{
InitializeComponent();

saveBtn.Enabled = false;
jsonRadioBtn.Checked = true;
v3RadioButton.Checked = true;
fromFileRadioBtn.Checked = true;
Expand Down Expand Up @@ -179,6 +182,8 @@ private void LoadEdm(string resource, string text)

private async Task Convert()
{
saveBtn.Enabled = false;

if (EdmModel == null)
{
return;
Expand All @@ -187,15 +192,16 @@ private async Task Convert()
string openApi = null;
await Task.Run(() =>
{
OpenApiDocument document = EdmModel.ConvertToOpenApi(Settings);
_document = EdmModel.ConvertToOpenApi(Settings);
MemoryStream stream = new MemoryStream();
document.Serialize(stream, Version, Format);
_document.Serialize(stream, Version, Format);
stream.Flush();
stream.Position = 0;
openApi = new StreamReader(stream).ReadToEnd();
});

oasRichTextBox.Text = openApi;
saveBtn.Enabled = true;
}

private string FormatXml(string xml)
Expand Down Expand Up @@ -227,7 +233,6 @@ private async void saveBtn_Click(object sender, EventArgs e)
saveFileDialog.Filter = "YAML files (*.yaml)|*.yaml|All files (*.*)|*.*";
}

saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;

if (saveFileDialog.ShowDialog() == DialogResult.OK)
Expand All @@ -237,14 +242,13 @@ private async void saveBtn_Click(object sender, EventArgs e)
{
await Task.Run(() =>
{
OpenApiDocument document = EdmModel.ConvertToOpenApi(Settings);
document.Serialize(fs, Version, Format);
_document?.Serialize(fs, Version, Format);
fs.Flush();
});
}
}

MessageBox.Show("Saved successful!");
MessageBox.Show("Saved successfully!");
}
}

private async void operationIdcheckBox_CheckedChanged(object sender, EventArgs e)
Expand Down