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

openapi2conv: convert references in nested additionalProperties schemas #1047

Conversation

travisnewhouse
Copy link
Contributor

When the schema specified in additionalProperties contains another nested schema with additionalProperties, the schema references must be converted at all of levels of the nested schemas.

The following examples illustrate the problem. This pull request fixes the issue by traversing all of the nested schemas in additionalProperties, making copies of the objects that are modified by the conversion.

Prior to this change, a schema reference is specified in a top-level additionalProperties was being converted correctly. In the following example OpenAPI 2.0 definition, the reference to "#/definitions/Foo" is correctly converted to "#/components/schemas/Foo" in the OpenAPI 3.0 definition that is produced.

{
    "basePath": "/v2",
    "host": "test.example.com",
    "info": {
        "title": "MyAPI",
        "version": "0.1"
    },
    "paths": {
        "/foo": {
            "get": {
                "operationId": "getFoo",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "returns all information",
                        "schema":{
                            "type":"object",
                            "additionalProperties":{
                               "$ref":"#/definitions/Foo"
                            }
                        }
                    }
                },
                "summary": "get foo"
            }
        }
    },
        "definitions": {
            "Foo": {
                    "type": "object",
                        "properties": {
                            "a": {
                                    "type": "string"
                                }
                        }
                }
        },
    "schemes": [
        "http"
    ],
    "swagger": "2.0"
}

When there is a schema reference in an additionalProperties that is nested in the schema of another additionalProperties, then the conversion from OpenAPI 2.0 to OpenAPI 3.0 did not convert the schema reference to "#/components/schema/Foo". This case is fixed by this pull request.

{
    "basePath": "/v2",
    "host": "test.example.com",
    "info": {
        "title": "MyAPI",
        "version": "0.1"
    },
    "paths": {
        "/foo": {
            "get": {
                "operationId": "getFoo",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "returns all information",
                        "schema":{
                            "type":"object",
                            "additionalProperties":{
                               "type":"object",
                               "additionalProperties":{
                                   "$ref":"#/definitions/Foo"
                               }
                            }
                        }
                    }
                },
                "summary": "get foo"
            }
        }
    },
        "definitions": {
            "Foo": {
                    "type": "object",
                        "properties": {
                            "a": {
                                    "type": "string"
                                }
                        }
                }
        },
    "schemes": [
        "http"
    ],
    "swagger": "2.0"
}

When the schema specified in additionalProperties contains another
nested schema with additionalProperties, the references must be
converted at all of levels of the nested schemas.
@fenollp fenollp changed the title Convert references in nested additionalProperties schemas openapi2conv: convert references in nested additionalProperties schemas Dec 24, 2024
@fenollp fenollp merged commit cea0a13 into getkin:master Dec 24, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants