Skip to content
Open
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 @@ -34,8 +34,8 @@ public static void AddParametersToDocument(this ODataContext context, OpenApiDoc

// It allows defining query options and headers that can be reused across operations of the service.
// The value of parameters is a map of Parameter Objects.
document.AddComponent("top", CreateTop(context.Settings.TopExample));
document.AddComponent("skip", CreateSkip());
document.AddComponent("top", CreateTop(context.Settings.TopExample, context.Settings.UseInt32ForPaginationParameters));
document.AddComponent("skip", CreateSkip(context.Settings.UseInt32ForPaginationParameters));
document.AddComponent("count", CreateCount());
document.AddComponent("filter", CreateFilter());
document.AddComponent("search", CreateSearch());
Expand Down Expand Up @@ -886,7 +886,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
}

// #top
private static OpenApiParameter CreateTop(int topExample)
private static OpenApiParameter CreateTop(int topExample, bool useInt32Format = false)
{
return new OpenApiParameter
{
Expand All @@ -896,7 +896,7 @@ private static OpenApiParameter CreateTop(int topExample)
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Number,
Format = "int64",
Format = useInt32Format ? "int32" : "int64",
Minimum = "0",
},
Example = topExample,
Expand All @@ -906,7 +906,7 @@ private static OpenApiParameter CreateTop(int topExample)
}

// $skip
private static OpenApiParameter CreateSkip()
private static OpenApiParameter CreateSkip(bool useInt32Format = false)
{
return new OpenApiParameter
{
Expand All @@ -916,7 +916,7 @@ private static OpenApiParameter CreateSkip()
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Number,
Format = "int64",
Format = useInt32Format ? "int32" : "int64",
Minimum = "0",
},
Style = ParameterStyle.Form,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp
}
if (context.Settings.EnableCount)
{
baseSchema.Properties.Add(ODataConstants.OdataCount.Key, ODataConstants.OdataCount.Value);
var odataCount = ODataConstants.CreateOdataCount(context.Settings.UseInt32ForCountResponses);
baseSchema.Properties.Add(odataCount.Key, odataCount.Value);
}
schema = baseSchema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
if(context.Settings.EnableDollarCountPath)
document.AddComponent(Constants.DollarCountSchemaName, new OpenApiSchema {
Type = JsonSchemaType.Number,
Format = "int64"
Format = context.Settings.UseInt32ForCountResponses ? "int32" : "int64"
});

if(context.HasAnyNonContainedCollections())
Expand Down Expand Up @@ -101,7 +101,10 @@

responseSchema.Properties ??= new Dictionary<string, IOpenApiSchema>();
if (context.Settings.EnableCount)
responseSchema.Properties.Add(ODataConstants.OdataCount.Key, ODataConstants.OdataCount.Value);
{
var odataCount = ODataConstants.CreateOdataCount(context.Settings.UseInt32ForCountResponses);
responseSchema.Properties.Add(odataCount.Key, odataCount.Value);
}
if (context.Settings.EnablePagination)
responseSchema.Properties.Add(ODataConstants.OdataNextLink.Key, ODataConstants.OdataNextLink.Value);
}
Expand Down Expand Up @@ -261,10 +264,13 @@
baseSchema.Properties.Add(ODataConstants.OdataNextLink.Key, ODataConstants.OdataNextLink.Value);

if (context.Settings.EnableCount)
baseSchema.Properties.Add(ODataConstants.OdataCount.Key, ODataConstants.OdataCount.Value);
{
var odataCount = ODataConstants.CreateOdataCount(context.Settings.UseInt32ForCountResponses);
baseSchema.Properties.Add(odataCount.Key, odataCount.Value);
}

collectionSchema = baseSchema;
}
}
}
else
{
Expand Down Expand Up @@ -749,7 +755,7 @@
// The type 'System.Double' is not supported in Open API document.
case EdmPrimitiveTypeKind.Double:
/*
{

Check warning on line 758 in src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this commented out code.
double result;
if (Double.TryParse(property.DefaultValueString, out result))
{
Expand Down
12 changes: 12 additions & 0 deletions src/Microsoft.OpenApi.OData.Reader/OData/ODataConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ internal static class ODataConstants
/// </summary>
public readonly static KeyValuePair<string, IOpenApiSchema> OdataCount = new("@odata.count", new OpenApiSchema { Type = JsonSchemaType.Number | JsonSchemaType.Null, Format = "int64"});

/// <summary>
/// Creates an @odata.count KeyValue pair with the specified format.
/// </summary>
public static KeyValuePair<string, IOpenApiSchema> CreateOdataCount(bool useInt32Format)
{
return new("@odata.count", new OpenApiSchema
{
Type = JsonSchemaType.Number | JsonSchemaType.Null,
Format = useInt32Format ? "int32" : "int64"
});
}

/// <summary>
/// @odata.deltaLink KeyValue pair
/// </summary>
Expand Down
14 changes: 13 additions & 1 deletion src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// <summary>
/// Gets/sets the service root.
/// </summary>
public Uri ServiceRoot { get; set; } = new Uri("http://localhost");

Check warning on line 22 in src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor your code not to use hardcoded absolute paths or URIs.

/// <summary>
/// Get/set the metadata version.
Expand Down Expand Up @@ -337,6 +337,16 @@
/// </summary>
public int ComposableFunctionsExpansionDepth { get; set; } = 1;

/// <summary>
/// Gets/Sets a value indicating whether common OData number parameters ($top, $skip) should use int32 format instead of int64.
/// </summary>
public bool UseInt32ForPaginationParameters { get; set; } = false;

/// <summary>
/// Gets/Sets a value indicating whether the $count response schema should use int32 format instead of int64.
/// </summary>
public bool UseInt32ForCountResponses { get; set; } = false;

internal OpenApiConvertSettings Clone()
{
var newSettings = new OpenApiConvertSettings
Expand Down Expand Up @@ -392,7 +402,9 @@
SemVerVersion = this.SemVerVersion,
EnableAliasForOperationSegments = this.EnableAliasForOperationSegments,
UseStringArrayForQueryOptionsSchema = this.UseStringArrayForQueryOptionsSchema,
ComposableFunctionsExpansionDepth = this.ComposableFunctionsExpansionDepth
ComposableFunctionsExpansionDepth = this.ComposableFunctionsExpansionDepth,
UseInt32ForPaginationParameters = this.UseInt32ForPaginationParameters,
UseInt32ForCountResponses = this.UseInt32ForCountResponses
};

return newSettings;
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ Microsoft.OpenApi.OData.OpenApiConvertSettings.RoutePathPrefixProvider.get -> Mi
Microsoft.OpenApi.OData.OpenApiConvertSettings.RoutePathPrefixProvider.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.SemVerVersion.get -> string!
Microsoft.OpenApi.OData.OpenApiConvertSettings.SemVerVersion.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseInt32ForPaginationParameters.get -> bool
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseInt32ForPaginationParameters.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseInt32ForCountResponses.get -> bool
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseInt32ForCountResponses.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.ServiceRoot.get -> System.Uri!
Microsoft.OpenApi.OData.OpenApiConvertSettings.ServiceRoot.set -> void
Microsoft.OpenApi.OData.OpenApiConvertSettings.ShowExternalDocs.get -> bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,5 +655,35 @@ public static IEdmModel GetEdmModel()
Assert.True(result);
return model;
}

[Theory]
[InlineData(true, "int32")]
[InlineData(false, "int64")]
public async Task CreateParametersRespectsUseInt32ForPaginationParameters(bool useInt32, string expectedFormat)
{
// Arrange
IEdmModel model = EdmCoreModel.Instance;
OpenApiConvertSettings settings = new()
{
UseInt32ForPaginationParameters = useInt32
};
ODataContext context = new ODataContext(model, settings);
OpenApiDocument openApiDocument = new();

// Act
context.AddParametersToDocument(openApiDocument);
var parameters = openApiDocument.Components.Parameters;

// Assert
Assert.NotNull(parameters);
var topParam = parameters.First(p => p.Key == "top").Value;
var skipParam = parameters.First(p => p.Key == "skip").Value;

var topJson = JsonNode.Parse(await topParam.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));
var skipJson = JsonNode.Parse(await skipParam.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0));

Assert.Equal(expectedFormat, topJson["schema"]["format"]?.GetValue<string>());
Assert.Equal(expectedFormat, skipJson["schema"]["format"]?.GetValue<string>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1161,5 +1161,30 @@ public async Task NonNullableUntypedPropertyWorks()

Assert.Equal("{ }", json);
}

[Theory]
[InlineData(true, "int32")]
[InlineData(false, "int64")]
public void DollarCountSchemaRespectsUseInt32ForCountResponses(bool useInt32, string expectedFormat)
{
// Arrange
IEdmModel model = EdmModelHelper.TripServiceModel;
OpenApiDocument openApiDocument = new();
OpenApiConvertSettings settings = new()
{
EnableDollarCountPath = true,
UseInt32ForCountResponses = useInt32
};
ODataContext context = new(model, settings);

// Act
context.AddSchemasToDocument(openApiDocument);

// Assert
Assert.True(openApiDocument.Components.Schemas.TryGetValue(Constants.DollarCountSchemaName, out var countSchema));
Assert.NotNull(countSchema);
Assert.Equal(JsonSchemaType.Number, countSchema.Type);
Assert.Equal(expectedFormat, countSchema.Format);
}
}
}
Loading