3

Following this post : Get Data JSON in Flask

I was able to write a simple API where i post a json objet (a first name & last name & a generate ID) to insert it into a database.

After that, i want to use swagger/flasgger to model it. It's already working for the get or get_by_id, but impossible for the POST to work.

In my python code i have this :

from flask import Flask, jsonify, request
@app.route('/api/v1/list', methods=['POST'])
@swag_from('index_post.yml')
def add_entry():
    request_json = request.get_json()
    value1 = request_json.get('First_Name')
    value2 = request_json.get('Last_Name')
    if value1 is not None and value2 is not None:
        cursor.execute("INSERT INTO person (first_name,last_name) VALUES 
        (%s,%s)", (value1, value2))
    data = conn.commit()
    return jsonify(data)

And in the YAML file, i have :

paths:
/api/v1/list:
post:
description: testpost
operationId: PostNewName
consumes:
  - application/json
parameters:
  - name: body
    in: body
    required: true
    schema:
      id : toto
      required:
        - first
        - last
      properties:
        first:
          type: string
          description: Unique identifier representing a First Name
        last:
          type: string
          description: Unique identifier representing a Last Name
responses:
  200:
    description: creation OK

But the parameters doesnt appear on the swagger html page. And i dont know what the issue is on this...

Thanks for your help.

Community
  • 1
  • 1
BrunoCX92
  • 101
  • 1
  • 2
  • 6

2 Answers2

3

seems like your indentation is wrong, you can edit and validate your schema at https://editor.swagger.io/. I started fixing it, but I think you should be able to do the rest now:

swagger: '2.0'
info:
  title: Demo App
  version: "1"
paths:
  /api/v1/list:
    post:
      description: testpost
      operationId: PostNewName
      consumes:
        - application/json
      parameters:
        - name: body
          in: body
          required: true
          schema:
            id : toto
            required:
              - first
              - last
            properties:
              first:
                type: string
                description: Unique identifier representing a First Name
              last:
                type: string
                description: Unique identifier representing a Last Name
      responses:
        200:
          description: creation OK
Tohmaxxx
  • 423
  • 2
  • 9
0

Put operation id: add_entry or remove the tag operation id