Синтаксис вложенных атрибутов Rswag

#ruby-on-rails #ruby #rspec #swagger #rswag

#ruby-on-rails #ruby #rspec #развязность #rswag

Вопрос:

у меня есть проект, в котором я должен написать вложенный атрибут в rswag

 {
  "question_bank": {
    "question_exams_attributes": {
      "0": {
        "question_bank_id": "",
        "exam_id": "12",
        "sub_category_id": "23",
        "_destroy": "false"
      }
    },
    "question_type": "Single best answer",
    "difficulty": "easy",
    "status": "active",
    "tag_list": [
      ""
    ],
    "question": "testing api 2"
  },
  "commit": "Submit"
}
  

я пытаюсь написать это тело json в синтаксисе вложенных атрибутов в rswag:
я пробовал это:

  parameter name: :question_bank, in: :body, schema: {
        type: :object,
        properties: {
          question_exams_attributes: {
            type: :object,
            properties: {
              '0': {
              properties: {
                question_bank_id: { type: :string },
                exam_id: { type: :string },
                sub_category_id: { type: :string }
                }
              }
            }    
          }    
        }

  }
  

Ответ №1:

Я тоже столкнулся с подобной проблемой.

Вот как я решил это.

 schema type: :object,
            properties: {
              title: { type: :string },
              content: { type: :string },
              comments: {
                type: :array,
                items: {
                  type: :object,
                  properties: {
                    content: { type: :string }
                  }
                }
              }
            }
  

В вашем случае, я думаю, вам не хватает типа в элементе «0».

 properties: {
          question_exams_attributes: {
            type: :object,
            properties: {
              '0': {
              type: :object,
              properties: {
                question_bank_id: { type: :string },
                exam_id: { type: :string },
                sub_category_id: { type: :string }
                }
              }
            }    
          }    
        }