APIDevTools / swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser/validator
https://apitools.dev/swagger-parser
MIT License
1.09k stars 154 forks source link

Is it possible replace $ref with the actual definiton data in the parsing process? #162

Closed robvik closed 3 years ago

robvik commented 3 years ago

Fantastic tool! Is it possible to dereference $ref entries and insert the actual definition data defined in the JSON-file in the parsing process? Here is an example:

The Petstore Swagger-specification: https://petstore.swagger.io/v2/swagger.json has the following path:

"/pet": {
    "post": {
        ...
        "parameters": [{
            "in": "body",
            "name": "body",
            "description": "Pet object that needs to be added to the store",
            "required": true,
            "schema": {
                "$ref": "#/definitions/Pet"
            }
        }],
        ...
    }
}

The definition is found in the JSON-file:

"Pet": {
    "type": "object",
    "required": ["name", "photoUrls"],
    "properties": {
        "id": {
            "type": "integer",
            "format": "int64"
        },
        "category": {
            "$ref": "#/definitions/Category"
        },
        "name": {
            "type": "string",
            "example": "doggie"
        },
        "photoUrls": {
            "type": "array",
            "xml": {
                "wrapped": true
            },
            "items": {
                "type": "string",
                "xml": {
                    "name": "photoUrl"
                }
            }
        },
        "tags": {
            "type": "array",
            "xml": {
                "wrapped": true
            },
            "items": {
                "xml": {
                    "name": "tag"
                },
                "$ref": "#/definitions/Tag"
            }
        },
        "status": {
            "type": "string",
            "description": "pet status in the store",
            "enum": ["available", "pending", "sold"]
        }
    },
    "xml": {
        "name": "Pet"
    }
}

Is it possible for swagger-parser to return the following dereferenced output?

"/pet": {
    "post": {
        ...
        "parameters": [{
            "in": "body",
            "name": "body",
            "description": "Pet object that needs to be added to the store",
            "required": true,
            "schema": {
                    "type": "object",
                    "required": ["name", "photoUrls"],
                    "properties": {
                        "id": {
                            "type": "integer",
                            "format": "int64"
                        },
                        "category": {
                            "type": "object",
                            "properties": {
                                "id": {
                                    "type": "integer",
                                    "format": "int64"
                                },
                                "name": {
                                    "type": "string"
                                }
                            },
                            "xml": {
                                "name": "Category"
                            }
                        },
                        "name": {
                            "type": "string",
                            "example": "doggie"
                        },
                        "photoUrls": {
                            "type": "array",
                            "xml": {
                                "wrapped": true
                            },
                            "items": {
                                "type": "string",
                                "xml": {
                                    "name": "photoUrl"
                                }
                            }
                        },
                        "tags": {
                            "type": "array",
                            "xml": {
                                "wrapped": true
                            },
                            "items": {
                                "type": "object",
                                "properties": {
                                    "id": {
                                        "type": "integer",
                                        "format": "int64"
                                    },
                                    "name": {
                                        "type": "string"
                                    }
                                },
                                "xml": {
                                    "name": "Tag"
                                }
                            }
                        },
                        "status": {
                            "type": "string",
                            "description": "pet status in the store",
                            "enum": ["available", "pending", "sold"]
                        }
                    },
                    "xml": {
                        "name": "Pet"
                    }
            }
        }],
        ...
    }
}

Thanks!

philsturgeon commented 3 years ago

I cant quite tell what is being asked here, sorry.

philsturgeon commented 3 years ago

Dereferencing