forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support additionalProperties that accept all unbound properties. (see A…
…zure#1057) (Azure#1112) * WIP: additionalProperties * Add support for OpenAPI 'additionalProperties' that act as catch-all properties. * Added description to generated additionalProperties * updated gulpfile for additional properties test
- Loading branch information
1 parent
09341fd
commit 836c71f
Showing
25 changed files
with
1,654 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
AutoRest/Generators/CSharp/CSharp.Tests/AdditionalPropertiesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
using Fixtures.AdditionalProperties; | ||
using Fixtures.AdditionalProperties.Models; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Rest.Generator.CSharp.Tests | ||
{ | ||
[Collection("AdditionalProperties Tests")] | ||
public class AdditionalPropertiesTests | ||
{ | ||
[Fact] | ||
public void VerifyWithTypedDictionary() | ||
{ | ||
// make sure it's constructable | ||
var withTypedDictionary = new WithTypedDictionary(); | ||
Assert.NotNull(withTypedDictionary); | ||
|
||
// check to see if the JsonExtensionData attribute is on the member. | ||
var property = typeof(WithTypedDictionary).GetProperty("AdditionalProperties"); | ||
Assert.NotNull(property); | ||
Assert.True(property.GetCustomAttributes().Any(each => each.GetType().Name == "JsonExtensionDataAttribute")); | ||
|
||
// check to see that the types of the AdditionalProperties object are correct | ||
var propertyType = property.PropertyType; | ||
Assert.Equal(new Type[] { typeof(string), typeof(Feature) }, propertyType.GenericTypeArguments); | ||
} | ||
|
||
[Fact] | ||
public void VerifyWithUntypedDictionary() | ||
{ | ||
// make sure it's constructable | ||
var withUntypedDictionary = new WithUntypedDictionary(); | ||
Assert.NotNull(withUntypedDictionary); | ||
|
||
// check to see if the JsonExtensionData attribute is on the member. | ||
var property = typeof(WithUntypedDictionary).GetProperty("AdditionalProperties"); | ||
Assert.NotNull(property); | ||
Assert.True(property.GetCustomAttributes().Any(each => each.GetType().Name == "JsonExtensionDataAttribute")); | ||
|
||
// check to see that the types of the AdditionalProperties object are correct | ||
var propertyType = property.PropertyType; | ||
Assert.Equal(new Type[] { typeof(string), typeof(Object) }, propertyType.GenericTypeArguments); | ||
} | ||
|
||
[Fact] | ||
public void VerifyWithStringDictionary() | ||
{ | ||
// make sure it's constructable | ||
var withStringDictionary = new WithStringDictionary(); | ||
Assert.NotNull(withStringDictionary); | ||
|
||
// check to see if the JsonExtensionData attribute is on the member. | ||
var property = typeof(WithStringDictionary).GetProperty("AdditionalProperties"); | ||
Assert.NotNull(property); | ||
Assert.True(property.GetCustomAttributes().Any(each => each.GetType().Name == "JsonExtensionDataAttribute")); | ||
|
||
// check to see that the types of the AdditionalProperties object are correct | ||
var propertyType = property.PropertyType; | ||
Assert.Equal(new Type[] { typeof(string), typeof(string) }, propertyType.GenericTypeArguments); | ||
} | ||
|
||
} | ||
} |
177 changes: 177 additions & 0 deletions
177
AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/Get.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for | ||
// license information. | ||
// | ||
// Code generated by Microsoft (R) AutoRest Code Generator. | ||
// Changes may cause incorrect behavior and will be lost if the code is | ||
// regenerated. | ||
|
||
namespace Fixtures.AdditionalProperties | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Rest; | ||
using Microsoft.Rest.Serialization; | ||
using Newtonsoft.Json; | ||
using Models; | ||
|
||
/// <summary> | ||
/// Get operations. | ||
/// </summary> | ||
public partial class Get : IServiceOperations<PetStoreonHeroku>, IGet | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the Get class. | ||
/// </summary> | ||
/// <param name='client'> | ||
/// Reference to the service client. | ||
/// </param> | ||
public Get(PetStoreonHeroku client) | ||
{ | ||
if (client == null) | ||
{ | ||
throw new ArgumentNullException("client"); | ||
} | ||
this.Client = client; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a reference to the PetStoreonHeroku | ||
/// </summary> | ||
public PetStoreonHeroku Client { get; private set; } | ||
|
||
/// <param name='limit'> | ||
/// number of pets to return | ||
/// </param> | ||
/// <param name='customHeaders'> | ||
/// Headers that will be added to request. | ||
/// </param> | ||
/// <param name='cancellationToken'> | ||
/// The cancellation token. | ||
/// </param> | ||
/// <return> | ||
/// A response object containing the response body and response headers. | ||
/// </return> | ||
public async Task<HttpOperationResponse<IList<Pet>>> PetsWithHttpMessagesAsync(int? limit = 11, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
if (limit > 10000) | ||
{ | ||
throw new ValidationException(ValidationRules.InclusiveMaximum, "limit", 10000); | ||
} | ||
if (limit < 11) | ||
{ | ||
throw new ValidationException(ValidationRules.InclusiveMinimum, "limit", 11); | ||
} | ||
// Tracing | ||
bool _shouldTrace = ServiceClientTracing.IsEnabled; | ||
string _invocationId = null; | ||
if (_shouldTrace) | ||
{ | ||
_invocationId = ServiceClientTracing.NextInvocationId.ToString(); | ||
Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); | ||
tracingParameters.Add("limit", limit); | ||
tracingParameters.Add("cancellationToken", cancellationToken); | ||
ServiceClientTracing.Enter(_invocationId, this, "Pets", tracingParameters); | ||
} | ||
// Construct URL | ||
var _baseUrl = this.Client.BaseUri.AbsoluteUri; | ||
var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "").ToString(); | ||
List<string> _queryParameters = new List<string>(); | ||
if (limit != null) | ||
{ | ||
_queryParameters.Add(string.Format("limit={0}", Uri.EscapeDataString(SafeJsonConvert.SerializeObject(limit, this.Client.SerializationSettings).Trim('"')))); | ||
} | ||
if (_queryParameters.Count > 0) | ||
{ | ||
_url += "?" + string.Join("&", _queryParameters); | ||
} | ||
// Create HTTP transport objects | ||
HttpRequestMessage _httpRequest = new HttpRequestMessage(); | ||
HttpResponseMessage _httpResponse = null; | ||
_httpRequest.Method = new HttpMethod("GET"); | ||
_httpRequest.RequestUri = new Uri(_url); | ||
// Set Headers | ||
if (customHeaders != null) | ||
{ | ||
foreach(var _header in customHeaders) | ||
{ | ||
if (_httpRequest.Headers.Contains(_header.Key)) | ||
{ | ||
_httpRequest.Headers.Remove(_header.Key); | ||
} | ||
_httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); | ||
} | ||
} | ||
|
||
// Serialize Request | ||
string _requestContent = null; | ||
// Send Request | ||
if (_shouldTrace) | ||
{ | ||
ServiceClientTracing.SendRequest(_invocationId, _httpRequest); | ||
} | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
_httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); | ||
if (_shouldTrace) | ||
{ | ||
ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); | ||
} | ||
HttpStatusCode _statusCode = _httpResponse.StatusCode; | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
string _responseContent = null; | ||
if ((int)_statusCode != 200) | ||
{ | ||
var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); | ||
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); | ||
ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); | ||
if (_shouldTrace) | ||
{ | ||
ServiceClientTracing.Error(_invocationId, ex); | ||
} | ||
_httpRequest.Dispose(); | ||
if (_httpResponse != null) | ||
{ | ||
_httpResponse.Dispose(); | ||
} | ||
throw ex; | ||
} | ||
// Create Result | ||
var _result = new HttpOperationResponse<IList<Pet>>(); | ||
_result.Request = _httpRequest; | ||
_result.Response = _httpResponse; | ||
// Deserialize Response | ||
if ((int)_statusCode == 200) | ||
{ | ||
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
try | ||
{ | ||
_result.Body = SafeJsonConvert.DeserializeObject<IList<Pet>>(_responseContent, this.Client.DeserializationSettings); | ||
} | ||
catch (JsonException ex) | ||
{ | ||
_httpRequest.Dispose(); | ||
if (_httpResponse != null) | ||
{ | ||
_httpResponse.Dispose(); | ||
} | ||
throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); | ||
} | ||
} | ||
if (_shouldTrace) | ||
{ | ||
ServiceClientTracing.Exit(_invocationId, _result); | ||
} | ||
return _result; | ||
} | ||
|
||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/GetExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for | ||
// license information. | ||
// | ||
// Code generated by Microsoft (R) AutoRest Code Generator. | ||
// Changes may cause incorrect behavior and will be lost if the code is | ||
// regenerated. | ||
|
||
namespace Fixtures.AdditionalProperties | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Rest; | ||
using Models; | ||
|
||
/// <summary> | ||
/// Extension methods for Get. | ||
/// </summary> | ||
public static partial class GetExtensions | ||
{ | ||
/// <param name='operations'> | ||
/// The operations group for this extension method. | ||
/// </param> | ||
/// <param name='limit'> | ||
/// number of pets to return | ||
/// </param> | ||
public static IList<Pet> Pets(this IGet operations, int? limit = 11) | ||
{ | ||
return Task.Factory.StartNew(s => ((IGet)s).PetsAsync(limit), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult(); | ||
} | ||
|
||
/// <param name='operations'> | ||
/// The operations group for this extension method. | ||
/// </param> | ||
/// <param name='limit'> | ||
/// number of pets to return | ||
/// </param> | ||
/// <param name='cancellationToken'> | ||
/// The cancellation token. | ||
/// </param> | ||
public static async Task<IList<Pet>> PetsAsync(this IGet operations, int? limit = 11, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
using (var _result = await operations.PetsWithHttpMessagesAsync(limit, null, cancellationToken).ConfigureAwait(false)) | ||
{ | ||
return _result.Body; | ||
} | ||
} | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
AutoRest/Generators/CSharp/CSharp.Tests/Expected/Additional.Properties/IGet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for | ||
// license information. | ||
// | ||
// Code generated by Microsoft (R) AutoRest Code Generator. | ||
// Changes may cause incorrect behavior and will be lost if the code is | ||
// regenerated. | ||
|
||
namespace Fixtures.AdditionalProperties | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Rest; | ||
using Models; | ||
|
||
/// <summary> | ||
/// Get operations. | ||
/// </summary> | ||
public partial interface IGet | ||
{ | ||
/// <param name='limit'> | ||
/// number of pets to return | ||
/// </param> | ||
/// <param name='customHeaders'> | ||
/// The headers that will be added to request. | ||
/// </param> | ||
/// <param name='cancellationToken'> | ||
/// The cancellation token. | ||
/// </param> | ||
Task<HttpOperationResponse<IList<Pet>>> PetsWithHttpMessagesAsync(int? limit = 11, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)); | ||
} | ||
} |
Oops, something went wrong.