yonaskolb / SwagGen

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

External schema reference requires #ref #168

Open mitrenegade opened 5 years ago

mitrenegade commented 5 years ago

I am trying to use SwagGen to generate client code from an OpenAPI 3.0 spec. The sample spec is https://github.com/yonaskolb/SwagGen/blob/master/Specs/Petstore/spec.yml (translated to json) and works fine when invoked with:

swaggen generate spec.json

I want to replace the components/schemas section with an external schema representing Pet:

      "schemas": {
         "Pet": {
            "required": [
               "id",
               "name"
            ],
            "properties": {
               "id": {
                  "type": "integer",
                  "format": "int64"
               },
               "name": {
                  "type": "string"
               },
               "tag": {
                  "type": "string"
               }
            }
         },

with

      "schemas": {
         "Pet": {
            "$ref": "pet.json"
         },

where pet.json is in the same directory and looks like:

{
  "$schema": "http://json-schema.org/draft-05/schema#",
  "$id": "https://example.com/schema/pet.json",
  "title": "Pet",
  "description": "Pet",
  "type": "object",
  "properties": {
    "id": {
      "description": "id",
      "type": "integer",
      "format": "int64"
    },
    "name": {
      "description": "name",
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  },
  "required": [
    "id",
    "name"
  ]
}

This gives me an error Fatal error: Reference ./pet.json is unresolved: file /private/tmp/swaggen-20190424-43085-jvmk5p/SwagGen-4.1.0/Sources/Swagger/Component/Reference.swift, line 10 Illegal instruction: 4

For some reason, when I use a #ref instead:

      "schemas": {
         "Pet": {
            "#ref": "./pet.json"
         },

generation succeeds. But, the generated Pet model is empty:

// Generated by SwagGen
// https://github.com/yonaskolb/SwagGen
//

import Foundation

public class Pet: APIModel {

    public init() {
    }

    public required init(from decoder: Decoder) throws {
    }

    public func encode(to encoder: Encoder) throws {
    }

    public func isEqual(to object: Any?) -> Bool {
      guard object is Pet else { return false }
      return true
    }

    public static func == (lhs: Pet, rhs: Pet) -> Bool {
        return lhs.isEqual(to: rhs)
    }
}

I'm trying to understand why $ref results in an unresolved issue, why #ref kind of works, but results in an incorrect Pet model.

yonaskolb commented 5 years ago

Hi @mitrenegade. File references are currently unsupported in SwagGen. There are no immediate plans to support them, but PR's are welcome

mitrenegade commented 5 years ago

thanks for the info. i'm open to looking into it if i have time but i guess not right now. can you give any hint why #ref might parse correctly?

            "#ref": "./pet.json"
         },

doesn't seem to be correct syntax but doesn't result in any errors when generating the client.

yonaskolb commented 5 years ago

#ref wouldn’t be parsed at all so you’d just end up with an empty Pet Schema definition

benrudhart commented 5 years ago

I ran into the same issue and would like to ask if you @mitrenegade started to work on it?

mitrenegade commented 5 years ago

No longer working on this