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

Improve OpenAPI support for enums as map keys #1664

Closed
denisrosca opened this issue Mar 7, 2023 · 2 comments · Fixed by #1905
Closed

Improve OpenAPI support for enums as map keys #1664

denisrosca opened this issue Mar 7, 2023 · 2 comments · Fixed by #1905
Labels
feature-request A feature should be added or improved. OpenAPI

Comments

@denisrosca
Copy link
Contributor

Improve enum support as map keys by utilizing the newly added propertyNames JSON Schema in OpenAPI 3.1.

Background

There's a mismatch in support of enums as map keys between Smithy and OpenAPI. Currently when converting a smithy definition containing a map shape with an enum as key, the generated Open API spec discards all enum information and treats the map shape a simple string to object mapping.

Given this simple Smithy service:

$version: "2"

namespace enumkeys.example

use aws.protocols#restJson1

@restJson1
service ExampleService {
  version: "1.0.0"
  operations: [MajorCitiesByCountry]
}

@http(method: "GET", uri: "/cities", code: 200)
@readonly
operation MajorCitiesByCountry {
  output : Response
}

enum Country {
  UK = "UK"
  FR = "FR"
  US = "US"
  DE = "DE"
}

string City

list Cities {
    member: City
}

map CitiesByCountry {
  key: Country
  value: Cities
}

structure Response {
  @required
  majorCities: CitiesByCountry
}

the output open api spec looks like this (notice how the CitiesByCountry object has lost all traces of the enum and it's values):

{
    "openapi": "3.1.0",
    "info": {
        "title": "ExampleService",
        "version": "1.0.0"
    },
    "paths": {
        "/cities": {
            "get": {
                "operationId": "MajorCitiesByCountry",
                "responses": {
                    "200": {
                        "description": "MajorCitiesByCountry 200 response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/MajorCitiesByCountryResponseContent"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "CitiesByCountry": {
                "type": "object",
                "additionalProperties": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            },
            "MajorCitiesByCountryResponseContent": {
                "type": "object",
                "properties": {
                    "majorCities": {
                        "$ref": "#/components/schemas/CitiesByCountry"
                    }
                },
                "required": [
                    "majorCities"
                ]
            }
        }
    }
}

Suggested approach

Instead of discarding all enum information, we could leverage the newly added support for the propertyNames JSON Schema and output the CitiesByCountry object like this:

...
    "components": {
        "schemas": {
            "CitiesByCountry": {
                "type": "object",
                "propertyNames": {
                    "enum": ["UK", "US", "DE", "FR"]
                }
                "additionalProperties": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            }
...
@denisrosca denisrosca changed the title OpenAPI support for enums as map keys Improve OpenAPI support for enums as map keys Mar 7, 2023
@denisrosca
Copy link
Contributor Author

If the proposal looks like something that would be accepted, myself or someone from the team I'm working with could try to tackle it in a PR.

@kstich
Copy link
Contributor

kstich commented Mar 10, 2023

Thanks for opening this issue! We'd accept a change along these lines if you or your team is open to contributing it. For this specific feature, it should be gated by the OpenApiVersion.VERSION_3_1_0 configuration.

@kstich kstich added the feature-request A feature should be added or improved. label Mar 10, 2023
@srchase srchase added the OpenAPI label Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. OpenAPI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants