how to write a swagger if two services within same proxy has same resource path but differs with verb

Not applicable

I have built a service which has same resource path but they differ with their verb(both the service are inside same proxy).

I have written the swagger in JSON schema format.

The problem now i am facing is while validating the proxy in apigee studio(swagger editor for apigee),it says its a valid swagger but when i import the swagger one of the service gets automatically removed same is happening in developer portal ,i guess its because of the same resource path.

So,is there any alternate way to handle this issue?Please Help

Thanks in advance

0 7 603
7 REPLIES 7

hi @shubham singh you can use this sample yaml;

swagger: "2.0"
info:
  version: "0.0.1"
  title: Swagger API
host: playground.apistudio.io
basePath: /try/d5d9ad20-841b-4b86-830b-6c9d5bece86b
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/json
paths:
  /hello:
    x-swagger-router-controller: hello_world
    get:
      description: Returns greetings to the caller
      operationId: hello
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/HelloWorldResponse"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    post:
        description: Returns greetings to the caller
        operationId: hello1
        responses:
          "200":
            description: Success
            schema:
              $ref: "#/definitions/HelloWorldResponse"
          default:
            description: Error
            schema:
              $ref: "#/definitions/ErrorResponse"
    
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
      age:
        type: number
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

similar question is answered here - https://community.apigee.com/questions/5333/swagger-editor-post-get-with-same-uri-posts-doesnt.html

@Kuldeep Bhati Thanks for the response

in my case two different flows are there one with different name other with another

For ex

1 service name is abc with verb Post and resource path as /abc

2nd service name is def with verb put and resource path as /abc

{
  "/abc": {
    "post": {
      "description": "Returns all pets from the system that the user has access to",
      "produces": [
        "application/json"
      ],
      "responses": {
        "200": {
          "description": "@!@#@##",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/XXX"
            }
          }
        }
      }
    }
  }
},
{
  "/abc": {
    "put": {
      "description": "Returns all pets from the system that the user has access to",
      "produces": [
        "application/json"
      ],
      "responses": {
        "200": {
          "description": "@$####",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/XXXXX"
            }
          }
        }
      }
    }
  }
},

Both of the above services has its own request and response definition.

In the above example json both the paths is same but differs by their verb.

My question is while importing the swagger for it in swagger editor one of the service gets removed.

So how to handle this scenario?

Thanks

Hi @shubham singh

You need to change your json as following, or use the yaml format as mentioned by @Kuldeep Bhati . Please understand that the resource path "/abc" is the property name and there cannot be duplicate properties in a json.

{
  "/abc": {
    "post": {
      "description": "Returns all pets from the system that the user has access to",
      "produces": [
        "application/json"
      ],
      "responses": {
        "200": {
          "description": "@!@#@##",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/XXX"
            }
          }
        }
      }
    },
    "put": {
      "description": "Returns all pets from the system that the user has access to",
      "produces": [
        "application/json"
      ],
      "responses": {
        "200": {
          "description": "@$####",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/XXXXX"
            }
          }
        }
      }
    }
  }
}

@snehal chakraborty thanks it's working fine

@Kuldeep Bhati @snehal chakraborty one more clarification..

If resource path is not there just the base path is there and the verb ,then how will i write the swagger for that service

If i just give the verb and try to validate that swagger its showing an error stating "Relative path to the individual endpoints.They must be relative to the basepath".

Any help on this?

Thanks in advance

@shubham singh `paths:` is a mandatory property, you can read it here as well - https://swagger.io/specification/#pathsObject

when there's no path, you can use `/` like used in below example;

swagger: "2.0"
info:
  version: "0.0.1"
  title: Swagger API
host: playground.apistudio.io
basePath: /try/d5d9ad20-841b-4b86-830b-6c9d5bece86b
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/json
paths:
  /:
    x-swagger-router-controller: hello_world
    get:
      description: Returns greetings to the caller
      operationId: hello
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/HelloWorldResponse"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    post:
        description: Returns greetings to the caller
        operationId: hello1
        responses:
          "200":
            description: Success
            schema:
              $ref: "#/definitions/HelloWorldResponse"
          default:
            description: Error
            schema:
              $ref: "#/definitions/ErrorResponse"
    
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
      age:
        type: number
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

Note, above example is yaml - you can use apigee swagger editor to check and validate your yaml if in case you need - http://playground.apistudio.io/d5d9ad20-841b-4b86-830b-6c9d5bece86b/#/

@Kuldeep Bhati

yes it can be done..its validating true but the problem here is if my basepath is suppose abc/v1/xyz and there is no resource path then if i hit the service as per swagger it will take abc/v1/xyz/ its saying service is temporarily not available because there is an extra / after basepath

If i hit it as abc/v1/xyz it works fine.

So i think the swagger will validate it as valid swagger but service won't run as its will consider abc/v1/xyz as unknown path

Any help on this scenario how to write swagger for this