yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
627 stars 147 forks source link

Custom types in properties #146

Closed StasanTelnov closed 5 years ago

StasanTelnov commented 5 years ago

Hello. I write custom template with using Moya for request and responces and ObjectMapper for mapping. ObjectMapper have very cool mapping of non standart types.

For example, ObjectMapper swift model:

import ObjectMapper

class SiteLinkModel: Mappable {
    var title: String?
    var link: URL?

    required init?(map: Map) {}

    func mapping(map: Map) {
        title <- map["title"]
        link <- (map["link"], URLTransform())
    }
}

I try write this model in my YAML file:

definitions:
  SiteLink:
    type: object
    properties:
      title:
        type: string
      link:
        type: url

But in result I see

import ObjectMapper

class SiteLinkModel: Mappable {
    var title: String?
    var link: UNKNOWN_ANY?

    required init?(map: Map) {}

    func mapping(map: Map) {
        title <- map["title"]
        link <- map["link"]
    }
}

But I dont found "UNKNOWN_ANY" string in files in this repository and in swagger-codegen repository. I found, that file SwagGen/Sources/SwagGenKit/SwiftFormatter.swift generate type and moving result to "properties[N].optionalType" variable, which I can see in .stencil file.

I dont undestand, I can detect in my .stencil file, that raw type is "url" and apply some other template for this property type? Or I have only way for handling non-standart property type - fork SwagGen and modify SwiftFormatter.swift?

Thanks for answer

yonaskolb commented 5 years ago

Hi @StasanTelnov. The way urls are defined in Swagger is with a string and a uri format: https://swagger.io/specification/#dataTypes

definitions:
  SiteLink:
    type: object
    properties:
      title:
        type: string
      link:
        type: string
        format: uri

I hope that helps

StasanTelnov commented 5 years ago

Thanks. I see Data Types, but "uri", dont declared in OAS. I trying

      link:
        type: string
        format: url

but received var link: String? in .swift file and I decided that URL`s is not supported "out of the box".

yonaskolb commented 5 years ago

It mentions the uri format is an unofficial standard. That’s what SwagGen and other generators use as a URL. Try uri instead of url