From b2064873144964ef56161c0ac4353ee63e8f01b5 Mon Sep 17 00:00:00 2001 From: Garrett Serack Date: Wed, 1 Jun 2016 13:59:19 -0700 Subject: [PATCH 1/2] updated documentation regarding dictionaries --- Documentation/defining-clients-swagger.md | 58 ++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/Documentation/defining-clients-swagger.md b/Documentation/defining-clients-swagger.md index a846c15e45..ba5dd1850e 100644 --- a/Documentation/defining-clients-swagger.md +++ b/Documentation/defining-clients-swagger.md @@ -165,7 +165,12 @@ public partial class Pet ``` ### Dictionaries -AutoRest generates dictionaries (or hash maps in some contexts) using `additionalProperites` from [JSON-Schema Draft 4][JSON-schema-validation-properties]. The additionalProperties element should specify the Swagger schema of the values in the dictionary . The keys of the generated dictionary will be of type `string`. +utoRest generates dictionaries (or hash maps in some contexts) using `additionalProperites` from [JSON-Schema Draft 4][JSON-schema-validation-properties]. The additionalProperties element should specify the Swagger schema of the values in the dictionary . The keys of the generated dictionary will be of type `string`. + +There are two basic patterns when generating dictionaries in AutoRest. + +#### Dictionaries as a member. +A dictionary can be generated as a member in a object schema, when there are no `properties` defined, the dictionary will be generated for the entire member. The following definition ```json @@ -220,6 +225,57 @@ public partial class Pet } ``` +#### Dictionaries as a catch-all for unlisted properties. +A dictionary can be also generated as way of accepting data for unlisted properties. The code generator (c#, in this case) will emit code that instructs the deserializer to send all unspecified values in the object to the generated `AdditionalProperties` member + +The code : + +``` yaml +definitions: + MyResponseObject: + type: object + properties: + someProperty: + type: string + # because this object has a property, additionalProperties becomes a catch-all for + # any properties in the response that aren't specified. + additionalProperties: + type: string +``` + +Generates code : + +``` c# +public partial class MyResponseObject +{ + /// + /// Initializes a new instance of the WithStringDictionary class. + /// + public MyResponseObject() { } + + /// + /// Initializes a new instance of the WithStringDictionary class. + /// + public MyResponseObject(IDictionary additionalProperties = default(IDictionary), string someProperty = default(string)) + { + AdditionalProperties = additionalProperties; + SomeProperty = someProperty; + } + + /// + /// Gets or sets unmatched properties from the message are + /// deserialized this collection + /// + [JsonExtensionData] + public IDictionary AdditionalProperties { get; set; } + + /// + /// + [JsonProperty(PropertyName = "someProperty")] + public string SomeProperty { get; set; } +} +``` + ### Constants AutoRest generates constant value for _required_ parameters and properties defined with _one_ enum value. Constant operation parameters are not exposed to the end user and are injected in the method body. Constant definition properties are also automatically added to the payload body. From 2ffe7963f3de0151db1ca2cc7a244ca20806cb88 Mon Sep 17 00:00:00 2001 From: Garrett Serack Date: Wed, 1 Jun 2016 14:12:08 -0700 Subject: [PATCH 2/2] fixed typo --- Documentation/defining-clients-swagger.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Documentation/defining-clients-swagger.md b/Documentation/defining-clients-swagger.md index ba5dd1850e..9ad5a05a83 100644 --- a/Documentation/defining-clients-swagger.md +++ b/Documentation/defining-clients-swagger.md @@ -165,7 +165,7 @@ public partial class Pet ``` ### Dictionaries -utoRest generates dictionaries (or hash maps in some contexts) using `additionalProperites` from [JSON-Schema Draft 4][JSON-schema-validation-properties]. The additionalProperties element should specify the Swagger schema of the values in the dictionary . The keys of the generated dictionary will be of type `string`. +AutoRest generates dictionaries (or hash maps in some contexts) using `additionalProperites` from [JSON-Schema Draft 4][JSON-schema-validation-properties]. The additionalProperties element should specify the Swagger schema of the values in the dictionary . The keys of the generated dictionary will be of type `string`. There are two basic patterns when generating dictionaries in AutoRest.