smithy-lang/smithy

Allow opting out of mixin flattening in OpenAPI conversion

Opened this issue · 0 comments

Currently a Smithy spec always has its mixins flattened and erased when being converted to OpenAPI/JsonSchema. This makes it difficult to represent for example fields that are global to all or a subset of operations and should be generated as a ResponseBase class etc. As such I propose to allow opting out of mixin flattening and have them instead generated as an allOf schema in OpenAPI.

Example Smithy:

$version: "2"
namespace repro

@aws.protocols#restJson1
service MyService {
    version: "2021-11-23",
    operations: [
        Get,
    ]
}

@readonly
@http(method: "GET", uri: "/")
operation Get {
    output: Output
}

structure Output with [OutputBase] {
    message: String
}

@mixin
structure OutputBase {
    success: Boolean
}

Currently generates OpenAPI:

{
    "openapi": "3.0.2",
    "info": {
        "title": "MyService",
        "version": "2021-11-23"
    },
    "paths": {
        "/": {
            "get": {
                "operationId": "Get",
                "responses": {
                    "200": {
                        "description": "Get 200 response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/GetResponseContent"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "GetResponseContent": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean"
                    },
                    "message": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

What I'd like the output to be:

{
    "openapi": "3.0.2",
    "info": {
        "title": "MyService",
        "version": "2021-11-23"
    },
    "paths": {
        "/": {
            "get": {
                "operationId": "Get",
                "responses": {
                    "200": {
                        "description": "Get 200 response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/GetResponseContent"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "GetResponseContent": {
                "allOf": [
                    {
                        "$ref": "#/components/schemas/OutputBase"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "message": {
                                "type": "string"
                            }
                        }
                    }
                ]
            },
            "OutputBase": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean"
                    }
                }
            }
        }
    }
}