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

Updated documentation regarding dictionaries #1115

Merged
merged 2 commits into from
Jun 1, 2016
Merged
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
56 changes: 56 additions & 0 deletions Documentation/defining-clients-swagger.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ 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`.

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
"StringDictionary": {
Expand Down Expand Up @@ -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
{
/// <summary>
/// Initializes a new instance of the WithStringDictionary class.
/// </summary>
public MyResponseObject() { }

/// <summary>
/// Initializes a new instance of the WithStringDictionary class.
/// </summary>
public MyResponseObject(IDictionary<string, string> additionalProperties = default(IDictionary<string, string>), string someProperty = default(string))
{
AdditionalProperties = additionalProperties;
SomeProperty = someProperty;
}

/// <summary>
/// Gets or sets unmatched properties from the message are
/// deserialized this collection
/// </summary>
[JsonExtensionData]
public IDictionary<string, string> AdditionalProperties { get; set; }

/// <summary>
/// </summary>
[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.

Expand Down