Parameter style parsers for OpenAPI.
The examples in the OpenAPI specification doesn't match RFC6570 fully, in those cases the examples in the specifications are followed.
dotnet add package ParameterStyleParsers.OpenAPIhttps://www.nuget.org/packages/ParameterStyleParsers.OpenAPI/
Create a parser by providing the OpenAPI parameter specification using OpenAPI.ParameterStyleParsers.ParameterValueParserFactory.
It's also possible to go via the respective parameter using OpenAPI.ParameterStyleParsers.ParameterFactory and from there use the extension method CreateParameterValueParser.
var parser = OpenAPI.ParameterStyleParsers.ParameterValueParserFactory.OpenApi32(
"""
{
"name": "color",
"in": "query",
"schema": {
"type": "array",
"items": {
"type": "string"
}
},
"style": "form",
"explode": true
}
""");
var parser = OpenAPI.ParameterStyleParsers.ParameterValueParserFactory.OpenApi31(
"""
{
"name": "color",
"in": "query",
"schema": {
"type": "array",
"items": {
"type": "string"
}
},
"style": "form",
"explode": true
}
""");
var parser = OpenAPI.ParameterStyleParsers.ParameterValueParserFactory.OpenApi30(
"""
{
"name": "color",
"in": "query",
"schema": {
"type": "array",
"items": {
"type": "string"
}
},
"style": "form",
"explode": true
}
""");
var parser = OpenAPI.ParameterStyleParsers.ParameterValueParserFactory.OpenApi20(
"""
{
"name": "color",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
}
""");
string styleSerializedParameter = "color=blue&color=black&color=brown";
Console.WriteLine(
parser.TryParse(styleSerializedParameter, out JsonNode? json, out string? error)
? json?.ToJsonString()
: error);
// ["blue","black","brown"]
var json = JsonNode.Parse("""
["blue","black","brown"]
""");
var styleSerializedParameter = parser.Serialize(json);
Console.WriteLine(styleSerializedParameter);
// color=blue&color=black&color=brown
Json pointers represented as URI fragments are supported, other URI's are currently not. It is possible to bring your own Json Schema implementation though.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.