Skip to content

Fresa/OpenAPI.ParameterStyleParsers

Repository files navigation

OpenAPI.ParameterStyleParsers

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.

Continuous Delivery

Installation

dotnet add package ParameterStyleParsers.OpenAPI

https://www.nuget.org/packages/ParameterStyleParsers.OpenAPI/

Getting Started

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

Parse a style serialized parameter

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"]

Serialize json to a parameter style.

var json = JsonNode.Parse("""
    ["blue","black","brown"]
""");
var styleSerializedParameter = parser.Serialize(json);
Console.WriteLine(styleSerializedParameter);
// color=blue&color=black&color=brown

Schema References

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.

Contributing

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.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages