-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Camel case scenarios | ||
|
||
## how to enabel camel-case in $select | ||
|
||
Given an OData service as following configuration (It's a non-Edm scenario): | ||
```C# | ||
builder.Services.AddControllers().AddOData(options => options.Select().Filter().OrderBy()); | ||
``` | ||
|
||
If you send request: | ||
```C# | ||
https://localhost:7158/WeatherForecast | ||
``` | ||
|
||
The response looks like: | ||
```json | ||
[ | ||
{ | ||
"date": "2022-02-08T14:56:26.7139132-08:00", | ||
"temperatureC": -3, | ||
"temperatureF": 27, | ||
"summary": "Mild" | ||
}, | ||
|
||
... | ||
``` | ||
|
||
The property name is camel case. However, if you have an OData $select query as: | ||
|
||
``` | ||
https://localhost:7158/WeatherForecast?$select=summary | ||
``` | ||
The response looks like: | ||
```json | ||
[ | ||
{ | ||
"Summary": "Cool" | ||
}, | ||
{ | ||
"Summary": "Warm" | ||
} | ||
... | ||
] | ||
|
||
``` | ||
|
||
The property name is not camel-case. | ||
|
||
## The root problem is that: | ||
|
||
$select will generate a select expand wrapper, which will output the selected property as a 'dictionary'. The key of dictionary is the property name, the value of dictionary is the selected property value. | ||
In order to make the dictionary key as camel case, you have to config the 'DictionaryKeyPolicy` for JSON serializer. | ||
|
||
Here's the configuration: | ||
|
||
```C# | ||
builder.Services.AddControllers() | ||
.AddJsonOptions(options => | ||
{ | ||
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase; | ||
}) | ||
.AddOData(options => options.Select().Filter().OrderBy()); | ||
``` | ||
|
||
Now, | ||
``` | ||
https://localhost:7158/WeatherForecast?$select=summary | ||
``` | ||
has the following response: | ||
|
||
```json | ||
[ | ||
{ | ||
"summary": "Cool" | ||
}, | ||
{ | ||
"summary": "Warm" | ||
}, | ||
{ | ||
"summary": "Chilly" | ||
}, | ||
{ | ||
"summary": "Balmy" | ||
}, | ||
{ | ||
"summary": "Balmy" | ||
} | ||
] | ||
``` |