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

Support additionalProperties that accept all unbound properties. (see #1057) #1112

Merged
merged 5 commits into from
Jun 1, 2016
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
7 changes: 7 additions & 0 deletions AutoRest/AutoRest.Core/ClientModel/DictionaryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public DictionaryType()
/// </summary>
public string NameFormat { get; set; }

/// <summary>
/// Indicates that the class should deserialize properties with no matching class member into this collection.
/// </summary>
public bool SupportsAdditionalProperties { get; set; }


/// <summary>
/// Gets the type name
/// </summary>
Expand Down Expand Up @@ -66,5 +72,6 @@ public override int GetHashCode()
{
return ValueType.GetHashCode();
}

}
}
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);
}

}
}
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;
}

}
}
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;
}
}

}
}
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));
}
}
Loading