Ошибка в клиенте Swagger для сериализации и десериализации входных данных - PullRequest
0 голосов
/ 03 июня 2019

Я сделал клиент C # с swagger 2.0, и мой yamel в editor.swagger.io выглядит так:

swagger: "2.0"
info:
  description: "This is a web service we call it XZ Company for getting Milage of the Customers "
  version: "1.0.0"
  title: "XZ"
  termsOfService: "http://ws.smtallk.org/XZ/terms/"
  contact:
    email: "m@gmail.com"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "ws.smtallk.org"
basePath: "/"

schemes:
- "http"
- "https"
paths:
  /customers:
    post:
      tags:
      - "Customers"
      summary: "Create Customer"
      description: "IdentificationType => 1: National ID 2: Passport 3:Other Identification Types"
      operationId: "createCustomer"
      consumes:
        - 'text/plain; charset=utf-8'
        - 'application/json'
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Create Customer object"
        required: true
        schema:
          $ref: "#/definitions/Customer_Body"
      responses:
        200:
          description: ''
          schema:
            $ref: '#/definitions/Customer_Response'
        400:
          description: ''
          schema:
            $ref: '#/definitions/Error400'
definitions:
  Customer_Body:
    type: "object"
    properties:
      identificationNo:
        type: "string"
        description: "National ID or Passport Number or any other Identification Number"
      identificationType:
        type: "integer"
        format: "int32"
        description: "1:National ID , 2: passport , 3:others"
      firstName:
        type: "string"
        description: ""
      lastName:
        type: "string"
        description: ""
      province:
        type: "string"
        description: ""
      city:
        type: "string"
        description: ""
      address:
        type: "string"
        description: ""
      phone:
        type: "string"
        description: ""
  Customer_Response:
    type: "object"
    properties:
      message:
        type: "string"
        description: ""
      result:
        $ref: '#/definitions/ResultInCustomer'
      error:
        $ref: '#/definitions/error'
  ResultInCustomer:
    type: "object"
    properties:
      _key:
        type: "string"
        description: ""
      _id:
        type: "string"
        description: ""
      _rev:
        type: "string"
        description: ""
      identificationNo:
        type: "string"
        description: ""  
      identificationType:
        type: "integer"
        format: "int32"
        description: ""  
      firstName:
        type: "string"
        description: ""  
      lastName:
        type: "string"
        description: ""  
      province:
        type: "string"
        description: ""  
      city:
        type: "string"
        description: ""   
      address:
        type: "string"
        description: ""   
      phone:
        type: "string"
        description: ""   
      isDeleted:
        type: "boolean"
        description: "" 
      vehicles:
            type: array
            items:
              $ref: '#/definitions/VehicleInResult'
  VehicleInResult:
    type: "object"
    properties:
      _id:
        type: "string"
        description: ""
      vin:
        type: "string"
        description: ""
      chassis:
        type: "string"
        description: ""
      engine:
        type: "string"
        description: ""
      plate:
        type: "string"
        description: ""
      identificationNo:
        type: "string"
        description: ""
      customer:
        $ref: '#/definitions/customer'

И у меня есть проект asp.net mvc, и я ссылался на клиента swagger на этот mvc. Я называю этот чванливый клиент следующим действием:

using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;

namespace Panel.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult RegisterCustomer()
         {
           return View();
         }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult RegisterCustomer([Bind(Include = "identificationNo,identificationType,firstName,lastName,province,city,address,phone")] RegisterCustomerViewModel register)
        {
            if (ModelState.IsValid) 
            {
                CustomersApi a = new CustomersApi();
                IO.Swagger.Model.CustomerBody customerBody = new IO.Swagger.Model.CustomerBody
                {
                    IdentificationNo = register.identificationNo,
                    IdentificationType = register.identificationType,
                    FirstName = register.firstName,
                    LastName = register.lastName,
                    Province = register.province,
                    City = register.city,
                    Address = register.address,
                    Phone = register.phone
                };
                Debug.WriteLine(customerBody.ToJson());

                Tuple<object, int> CreateCustomerResponse = a.CreateCustomer(customerBody);
                if (CreateCustomerResponse .Item2 == 200)
                {
                    CustomerResponse result1 = (CustomerResponse)b.Item1;
                    SamanTel_DBEntities db = new SamanTel_DBEntities();
                    Customer customer = new Customer
                    {
                        PublicId = Guid.NewGuid(),
                        CreateDate = DateTime.Now,
                        ChangeDate = DateTime.Now,
                        RegistererUser = db.Users.Where(u => u.Username == User.Identity.Name).Select(u => u.Id).SingleOrDefault(),
                        ModifierUser = db.Users.Where(u => u.Username == User.Identity.Name).Select(u => u.Id).SingleOrDefault(),
                        IsDeleted = result1.Result.IsDeleted.Value,
                        C_key = result1.Result.Key,
                        C_id = result1.Result.Id,
                        C_rev = result1.Result.Rev,
                        identificationNo = result1.Result.IdentificationNo,
                        identificationType = result1.Result.IdentificationType.Value,
                        FirstName = result1.Result.FirstName,
                        LastName = result1.Result.LastName,
                        Province = result1.Result.Province,
                        City = result1.Result.City,
                        Address = result1.Result.Address,
                        Phone = result1.Result.Phone,
                    };
                    try
                    {
                        db.Customer.Add(customer);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        throw;
                    }


                }
                else
                {

                }
            }

            return View(register);
        }
       }
    }

В действии mvc post в переменной «CreateCustomerResponse» я вижу ошибку, которая в сообщении об ошибке говорит, что «идентификационный номер нужен», как показано на рисунке ниже, но при вводе действия «RegisterCustomer» значение «идентификационный номер» имеет значение, но я не знаю в чем проблема?!

enter image description here

Я проверяю эти входные данные с помощью "editor.swagger.io", а остальное проверяю с почтальоном, и я получаю ответ Ok без проблем. Как я могу решить мою проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...