swagger定义生成的模型不正确

时间:2018-03-29 10:32:26

标签: swagger swagger-2.0 swagger-editor swagger-codegen

我使用editor.swagger.io(V2.0)定义了以下路径和模型:

swagger: '2.0'
info:
  version: V1
  title: x
  description: x
  termsOfService: x
  contact:
    name: x
    email: x
  license:
    name: x
    url: 'x'

paths:
  '/api/v1/consumer/profile':
    get:
      security:
        - Bearer: []
      tags:
        - consumer
      summary: Gets a consumer profile
      description: Retrieves a users profile
      operationId: getConsumerProfileById
      produces:
        - application/json
        - application/xml

      responses:
        '200':
          description: Retrieved Consumer Profile
          schema:
            $ref: '#/definitions/consumer'
        '401':
          description: Unauthorized

definitions:
  consumer:
    properties:
      firstName:
        type: string
      lastName:
        type: string
      gender:
        type: string
      dateOfBirth:
        type: string
        format: date
      profilePicUrl:
        type: string
        format: uri
      location:
        allOf:
          - $ref: "#/definitions/location"
          - type: object
      contactInfo:
        allOf:
          - $ref: "#/definitions/contactInfo"
          - type: object

  contactInfo:
    type: object
    properties:
      mobile:
        type: string
      landline:
        type: string
      email:
        type: string
      website:
        type: string
        format: url

  location:
    properties:
      address:
        allOf:
          - $ref: "#/definitions/address"
          - type: object
      gps:
        allOf:
          - $ref: "#/definitions/gps"
          - type: object

  gps:
    properties:  
      latitude:
        type: string
        format: gps_decimal_degrees
      longitude:
        type: string
        format: gps_decimal_degrees

  address:
    properties:
      addressOne:
        type: string
      addressTwo:
        type: string
      addressThree:
        type: string
      postcode:
        type: string
      city:
        type: string
      county:
        type: string
      country:
        type: string

这导致JSON看起来像这样:

{
  "firstName": "string",
  "lastName": "string",
  "gender": "string",
  "dateOfBirth": "string",
  "profilePicUrl": "string",
  "location": {
    "address": {
      "addressOne": "string",
      "addressTwo": "string",
      "addressThree": "string",
      "postcode": "string",
      "city": "string",
      "county": "string",
      "country": "string"
    },
    "gps": {
      "latitude": "string",
      "longitude": "string"
    }
  },
  "contactInfo": {
    "mobile": "string",
    "landline": "string",
    "email": "string",
    "website": "string"
  }
}

问题是,当我尝试为它生成一个aspnetcore服务器存根时,模型出错了:

using System;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace IO.Swagger.Models
{
    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    public partial class Consumer : IEquatable<Consumer>
    { 
        /// <summary>
        /// Gets or Sets FirstName
        /// </summary>
        [DataMember(Name="firstName")]
        public string FirstName { get; set; }

        /// <summary>
        /// Gets or Sets LastName
        /// </summary>
        [DataMember(Name="lastName")]
        public string LastName { get; set; }

        /// <summary>
        /// Gets or Sets Gender
        /// </summary>
        [DataMember(Name="gender")]
        public string Gender { get; set; }

        /// <summary>
        /// Gets or Sets DateOfBirth
        /// </summary>
        [DataMember(Name="dateOfBirth")]
        public DateTime? DateOfBirth { get; set; }

        /// <summary>
        /// Gets or Sets ProfilePicUrl
        /// </summary>
        [DataMember(Name="profilePicUrl")]
        public string ProfilePicUrl { get; set; }

        /// <summary>
        /// Returns the string presentation of the object
        /// </summary>
        /// <returns>String presentation of the object</returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append("class Consumer {\n");
            sb.Append("  FirstName: ").Append(FirstName).Append("\n");
            sb.Append("  LastName: ").Append(LastName).Append("\n");
            sb.Append("  Gender: ").Append(Gender).Append("\n");
            sb.Append("  DateOfBirth: ").Append(DateOfBirth).Append("\n");
            sb.Append("  ProfilePicUrl: ").Append(ProfilePicUrl).Append("\n");
            sb.Append("}\n");
            return sb.ToString();
        }
    }

这是一个错误还是我在定义中遗漏了什么?

0 个答案:

没有答案