Convert OpenAPI3 examples to postman examples

How can i convert OpenAPI examples to Postman Collection examples?

In OpenAPI 3, we can define different named examples for parameters, request bodies and responses, for instance

"/test-resources/{TYPE}": {
            "post": {
                "tags": [
                    "TestResource"
                ],
                "operationId": "create_test_resource",
                "summary": "Creates a test resource of type",
                "parameters": [
                    {
                        "name": "TYPE",
                        "in": "path",
                        "description": "Resource type",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "examples": {
                            "Success": {
                                "value": "CorrectType"
                            },
                            "Incorrect": {
                                "value": "InvalidType"
                            }
                        }
                    }
                ],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "element_one": {
                                        "type": "string",
                                        "nullable": false
                                    },
                                    "element_two": {
                                        "type": "number",
                                        "nullable": false
                                    }
                                }
                            },
                            "examples": {
                                "Success": {
                                    "value": {
                                        "element_one": "yes",
                                        "element_two": 20
                                    }
                                },
                                "Incorrect": {
                                    "value": {
                                        "element_one": "yes"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Successful",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "element_one": {
                                            "type": "string",
                                            "nullable": false
                                        },
                                        "element_two": {
                                            "type": "number",
                                            "nullable": false
                                        },
                                        "type": {
                                            "type": "string",
                                            "nullable": false
                                        }
                                    }
                                },
                                "examples": {
                                    "Success": {
                                        "value": {
                                            "element_one": "yes",
                                            "element_two": 20,
                                            "type": "CorrectType"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "400": {
                        "description": "Input error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "error_code": {
                                            "type": "string",
                                            "nullable": false
                                        },
                                        "error_message": {
                                            "type": "string",
                                            "nullable": false
                                        }
                                    }
                                },
                                "examples": {
                                    "Incorrect": {
                                        "value": {
                                            "error_code": "400",
                                            "error_message": "Invalid Request"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

In this case, it looks like when I generate a definition, I will get two examples (“Success” and “Incorrect”), with the request and the response defined with that name, but no, I’m only getting the examples based on the response codes (named with the description of each response) and the values are the first example defined.

I want to know if it’s possible to define the postman examples from the openapi definition, it’ll be nice because the mock generation would be a lot faster working only on the oas definition.

Hey @joint-operations-ca1 !

Not quite sure if I’m following, but this should work if you edit “Parameter Generation” to use “Example” instead of “Schema” while importing the OpenAPI file

Example: https://beta-sdk.photoroom.com/openapi

Thanks for your reply.

In the example you provided (Photoroom API), they don’t use named examples, they just put a single example for every field and/or a default value.

image

Hence, the parameters, body and response values will be the same in all example requests when generating a collection.

What I want to do is generate example scenarios using named examples in OpenApi, in such a way that in a case like this

{
    "requestBody": {
        "content": {
            "application/json": {
                "schema": {
                    "type": "object",
                    "properties": {
                        "element_one": {
                            "type": "string",
                            "nullable": false
                        },
                        "element_two": {
                            "type": "number",
                            "nullable": false
                        }
                    }
                },
                "examples": {
                    "Success": { // Success Scenario
                        "value": {
                            "element_one": "yes",
                            "element_two": 20
                        }
                    },
                    "Incorrect": {  // Incorrect scenario
                        "value": {
                            "element_one": "yes"
                        }
                    }
                }
            }
        }
    }
}

It will generate two examples in collection for this path.

In the Success scenario, the request body should be

{
  "element_one": "yes",
  "element_two": 20
}

but in the Incorrect scenario, the request body should be

{
"element_one": "yes"
}

And applying the same logic for parameters and responses.