Я столкнулся с проблемой в API Swagger?

Swagger.yml

swagger: "2.0"
info:
  version: "0.0.1"
  title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /movies:
    # binds a127 app logic to a route
    x-swagger-router-controller: movies
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: index
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/MovieListBody"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
# complex objects have schema definitions

    post:
      description: Creates a new movie entry
      operationId: create
      parameters:
        - name: movie
          required: true
          in: body
          description: a new movie details
          schema:
            $ref: "#/definitions/MovieBody"
      responses:
        "200":
          description: a successfully stored movie details
          schema:
            $ref: "#/definitions/MovieBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse" 

definitions:
  MovieListBody:
    required:
      - movies
    properties:
      movies:
        type: array
        items:
          $ref: "#/definitions/Movie"

  Movie:
    required:
      - title
      - gener
      - year
    properties:
      title:
        type: string
      gener:
        type: string
      year:
        type: integer

  MovieBody:
    required:
      - movie
    properties:
      movie:
        $ref: "#/definitions/Movie"

  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

Я получаю эту ошибку:

Маршрут определен в спецификации Swagger (/movies), но не определена пост-операция

Я новичок в этой концепции Swagger API. Я попробовал грубую операцию в Swagger API. Метод get работает нормально, но я пробовал post, показывая этот тип проблемы. Я пытался шаг за шагом смотреть видео Swagger API. Я попытался получить функцию, данные были успешно получены в db. Но я попытался отправить данные в mongodb с помощью Swagger API, он выдает ошибку такого типа....

Как это исправить? Может ли кто-нибудь предложить какое-либо решение?


person smith hari    schedule 13.03.2019    source источник


Ответы (1)


Вам не нужен узел /swagger, достаточно узла post на том же уровне, что и узел get в пути /movies. POST и GET — это операции, которые можно выполнять на конечной точке «фильмы».

В настоящее время ваш swagger поддерживает GET /movies/ и POST /swagger/, так как у вас есть путь под названием 'swagger'.

Структура должна стать:

paths:
  /movies:
    get:
      # All the get properties
    post:
      # All the post properties
definitions:
  # All the definitions you need

А вот обновленная копия твоего чванства:

swagger: "2.0"
info:
  version: "0.0.1"
  title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /movies:
    # binds a127 app logic to a route
    x-swagger-router-controller: movies
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: index
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/MovieListBody"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    post:
      description: Creates a new movie entry
      operationId: create
      parameters:
        - name: movie
          required: true
          in: body
          description: a new movie details
          schema:
            $ref: "#/definitions/MovieBody"
      responses:
        "200":
          description: a successfully stored movie details
          schema:
            $ref: "#/definitions/MovieBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse" 
definitions:
  MovieListBody:
    required:
      - movies
    properties:
      movies:
        type: array
        items:
          $ref: "#/definitions/Movie"

  Movie:
    required:
      - title
      - gener
      - year
    properties:
      title:
        type: string
      gener:
        type: string
      year:
        type: integer

  MovieBody:
    required:
      - movie
    properties:
      movie:
        $ref: "#/definitions/Movie"

  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string
person Andy Lamb    schedule 13.03.2019
comment
Я обновил ответ с правильной иерархией. - person Andy Lamb; 13.03.2019
comment
Я попробовал эту структуру, но это выдает это другое сообщение об ошибке: Проверка запроса не удалась: параметр (фильм) не прошел проверку схемы, - person smith hari; 13.03.2019
comment
может дать мне код примера операции Swagger api crud или видео... plz - person smith hari; 13.03.2019
comment
Я обновил ответ обновленной версией вашего чванства, которая синтаксически действительна. - person Andy Lamb; 13.03.2019
comment
Я попробовал этот шаблон, но выдал ошибку этого типа. Ошибка: проверка ответа не удалась: проверка схемы не удалась. как решить эту ошибку. и можете ли вы объяснить, что это за ошибка - person smith hari; 14.03.2019
comment
Может ли кто-нибудь проверить мой файл yaml, который я пытался выполнить методом post. Его ошибка проверки схемы ошибки не удалась. Как с этим справиться. Дайте мне какое-либо решение .. plz - person smith hari; 14.03.2019