jboss-fuse / fuse-apicurito-generator

Fuse Apicurito Generator
5 stars 14 forks source link

Remove host/scheme of the generated OpenAPI specification stored in the project #19

Closed bfitzpat closed 5 years ago

bfitzpat commented 5 years ago

When Apicurito generates an OpenAPI specification (swagger/json file) and hands it to the fuse project generator, that specification sometimes includes the "host" and "schemes" sections. @zregvart and @chirino have asked that we strip those sections out of the json for the file that we stash in the project.

As Hiram said in the e-mail stream that spawned this issue:

So quick recap of my thoughts on this subject. It’s better if they get stripped out of the copy of the open api doc that we add to the generated project. This copy of the api doc is used to describe the api implemented by the project. So letting the host info be implied makes sense.

It’s no longer describing what some other specific host is implementing. That other host could change the api it implements. Rendering our api spec copy invalid since it’s no longer true. But our project still implements the api

bfitzpat commented 5 years ago

Zoran suggested an example here - this is what we will try and remove from the incoming swagger.

host-scheme-section

Example swagger:

{
  "swagger": "2.0",
  "info": {
    "title": "Todo App API",
    "description": "Example Todo Application API",
    "version": "1.0.0",
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "host": "todo-syndesis-staging.b6ff.rh-idev.openshiftapps.com",
  "basePath": "/api",
  "schemes": [
    "http",
    "https"
  ],
  "paths": {
    "/": {
      "get": {
        "tags": [
          "tasks",
          "fetching"
        ],
        "summary": "List all tasks",
        "description": "Fetches all tasks from the database",
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "All is good",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Task"
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "tasks",
          "creating"
        ],
        "summary": "Create new task",
        "description": "Stores new task in the database",
        "produces": [
          "application/json"
        ],
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "Task to create",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Task"
            }
          }
        ],
        "responses": {
          "201": {
            "description": "All is good",
            "schema": {
              "$ref": "#/definitions/Task"
            }
          }
        }
      }
    },
    "/{id}": {
      "get": {
        "tags": [
          "tasks",
          "fetching"
        ],
        "summary": "Fetch task",
        "description": "Fetches task by given identifier",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "type": "integer",
            "format": "int64",
            "description": "Task identifier",
            "required": true
          }
        ],
        "responses": {
          "200": {
            "description": "All is good",
            "schema": {
              "$ref": "#/definitions/Task"
            }
          },
          "404": {
            "description": "No task with provided identifier found"
          }
        }
      },
      "put": {
        "tags": [
          "tasks",
          "updating"
        ],
        "summary": "Update task",
        "description": "Updates task by given identifier",
        "produces": [
          "application/json"
        ],
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "type": "integer",
            "format": "int64",
            "description": "Task identifier",
            "required": true
          },
          {
            "in": "body",
            "name": "body",
            "description": "Task with updates",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Task"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "All is good",
            "schema": {
              "$ref": "#/definitions/Task"
            }
          }
        }
      },
      "delete": {
        "tags": [
          "tasks",
          "destruction"
        ],
        "summary": "Delete task",
        "description": "Deletes task by given identifier",
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "type": "integer",
            "format": "int64",
            "description": "Task identifier to delete",
            "required": true
          }
        ],
        "responses": {
          "204": {
            "description": "Task deleted"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "username_password": {
      "type": "basic"
    }
  },
  "definitions": {
    "Task": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64",
          "title": "Task ID",
          "description": "Unique task identifier"
        },
        "task": {
          "type": "string",
          "title": "The task",
          "description": "Task line"
        },
        "completed": {
          "type": "integer",
          "title": "Task completition status",
          "description": "0 - ongoing, 1 - completed",
          "minimum": 0,
          "maximum": 1
        }
      }
    }
  }
}
bfitzpat commented 5 years ago

With some hacking, I have it reducing to this (host and scheme are still there, but empty). @zregvart is that sufficient or do they need to be completely removed from the exported openapi json?

{
  "swagger" : "2.0",
  "info" : {
    "description" : "Example Todo Application API",
    "version" : "1.0.0",
    "title" : "Todo App API",
    "license" : {
      "name" : "Apache 2.0",
      "url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "host" : "",
  "basePath" : "/api",
  "schemes" : [ ],
  "paths" : {
    "/" : {
      "get" : {
        "tags" : [ "tasks", "fetching" ],
        "summary" : "List all tasks",
        "description" : "Fetches all tasks from the database",
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "All is good",
            "schema" : {
              "type" : "array",
              "items" : {
                "$ref" : "#/definitions/Task"
              }
            }
          }
        }
      },
      "post" : {
        "tags" : [ "tasks", "creating" ],
        "summary" : "Create new task",
        "description" : "Stores new task in the database",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "description" : "Task to create",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/Task"
          }
        } ],
        "responses" : {
          "201" : {
            "description" : "All is good",
            "schema" : {
              "$ref" : "#/definitions/Task"
            }
          }
        }
      }
    },
    "/{id}" : {
      "get" : {
        "tags" : [ "tasks", "fetching" ],
        "summary" : "Fetch task",
        "description" : "Fetches task by given identifier",
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "Task identifier",
          "required" : true,
          "type" : "integer",
          "format" : "int64"
        } ],
        "responses" : {
          "200" : {
            "description" : "All is good",
            "schema" : {
              "$ref" : "#/definitions/Task"
            }
          },
          "404" : {
            "description" : "No task with provided identifier found"
          }
        }
      },
      "put" : {
        "tags" : [ "tasks", "updating" ],
        "summary" : "Update task",
        "description" : "Updates task by given identifier",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "Task identifier",
          "required" : true,
          "type" : "integer",
          "format" : "int64"
        }, {
          "in" : "body",
          "name" : "body",
          "description" : "Task with updates",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/Task"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "All is good",
            "schema" : {
              "$ref" : "#/definitions/Task"
            }
          }
        }
      },
      "delete" : {
        "tags" : [ "tasks", "destruction" ],
        "summary" : "Delete task",
        "description" : "Deletes task by given identifier",
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "Task identifier to delete",
          "required" : true,
          "type" : "integer",
          "format" : "int64"
        } ],
        "responses" : {
          "204" : {
            "description" : "Task deleted"
          }
        }
      }
    }
  },
  "securityDefinitions" : {
    "username_password" : {
      "type" : "basic"
    }
  },
  "definitions" : {
    "Task" : {
      "type" : "object",
      "properties" : {
        "id" : {
          "type" : "integer",
          "format" : "int64",
          "description" : "Unique task identifier",
          "title" : "Task ID"
        },
        "task" : {
          "type" : "string",
          "description" : "Task line",
          "title" : "The task"
        },
        "completed" : {
          "type" : "integer",
          "description" : "0 - ongoing, 1 - completed",
          "title" : "Task completition status",
          "minimum" : 0,
          "maximum" : 1
        }
      }
    }
  }
}
bfitzpat commented 5 years ago

Created a PR - https://github.com/jboss-fuse/fuse-apicurito-generator/pull/21

bfitzpat commented 5 years ago

@zregvart suggested nulling out the host and schemes, so the resulting openapi file is a bit cleaner:

{
  "swagger" : "2.0",
  "info" : {
    "description" : "Example Todo Application API",
    "version" : "1.0.0",
    "title" : "Todo App API",
    "license" : {
      "name" : "Apache 2.0",
      "url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "basePath" : "/api",
  "paths" : {
    "/" : {
      "get" : {
        "tags" : [ "tasks", "fetching" ],
        "summary" : "List all tasks",
        "description" : "Fetches all tasks from the database",
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "All is good",
            "schema" : {
              "type" : "array",
              "items" : {
                "$ref" : "#/definitions/Task"
              }
            }
          }
        }
      },
      "post" : {
        "tags" : [ "tasks", "creating" ],
        "summary" : "Create new task",
        "description" : "Stores new task in the database",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "description" : "Task to create",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/Task"
          }
        } ],
        "responses" : {
          "201" : {
            "description" : "All is good",
            "schema" : {
              "$ref" : "#/definitions/Task"
            }
          }
        }
      }
    },
    "/{id}" : {
      "get" : {
        "tags" : [ "tasks", "fetching" ],
        "summary" : "Fetch task",
        "description" : "Fetches task by given identifier",
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "Task identifier",
          "required" : true,
          "type" : "integer",
          "format" : "int64"
        } ],
        "responses" : {
          "200" : {
            "description" : "All is good",
            "schema" : {
              "$ref" : "#/definitions/Task"
            }
          },
          "404" : {
            "description" : "No task with provided identifier found"
          }
        }
      },
      "put" : {
        "tags" : [ "tasks", "updating" ],
        "summary" : "Update task",
        "description" : "Updates task by given identifier",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "Task identifier",
          "required" : true,
          "type" : "integer",
          "format" : "int64"
        }, {
          "in" : "body",
          "name" : "body",
          "description" : "Task with updates",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/Task"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "All is good",
            "schema" : {
              "$ref" : "#/definitions/Task"
            }
          }
        }
      },
      "delete" : {
        "tags" : [ "tasks", "destruction" ],
        "summary" : "Delete task",
        "description" : "Deletes task by given identifier",
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "Task identifier to delete",
          "required" : true,
          "type" : "integer",
          "format" : "int64"
        } ],
        "responses" : {
          "204" : {
            "description" : "Task deleted"
          }
        }
      }
    }
  },
  "securityDefinitions" : {
    "username_password" : {
      "type" : "basic"
    }
  },
  "definitions" : {
    "Task" : {
      "type" : "object",
      "properties" : {
        "id" : {
          "type" : "integer",
          "format" : "int64",
          "description" : "Unique task identifier",
          "title" : "Task ID"
        },
        "task" : {
          "type" : "string",
          "description" : "Task line",
          "title" : "The task"
        },
        "completed" : {
          "type" : "integer",
          "description" : "0 - ongoing, 1 - completed",
          "title" : "Task completition status",
          "minimum" : 0,
          "maximum" : 1
        }
      }
    }
  }
}
bfitzpat commented 5 years ago

Merged https://github.com/jboss-fuse/fuse-apicurito-generator/pull/21

@tsedmik can you verify this when you get a chance?

tsedmik commented 5 years ago

verified with master branch + I covered this issue with automated test see: https://github.com/tsedmik/fuse-apicurito-generator-tests/blob/master/src/test/resources/com/github/tsedmik/fuse_apicurito_generator_tests/open_api_file.feature