yonaskolb / SwagGen

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

Getting `Reference ... is unresolved` when generating from OAS3 #131

Closed Kastet closed 5 years ago

Kastet commented 5 years ago

Hi,

I was wondering if OAS3 was supported and gave it a go.

I used code from master, passed this spec as a launch argument. Getting Thread 1: Fatal error: Reference #/components/schemas/User is unresolved

Not sure if it's a bug or something isn't supported yet.

yonaskolb commented 5 years ago

Thanks for the report @Kastet. I've fixed the unresolved component here https://github.com/yonaskolb/SwagGen/pull/132

That spec still won't generate properly though, as an object schema can't be used as a path parameter. Would does the api expect to send? Just a simple string, or the whole user object encoded in some form?

Kastet commented 5 years ago

Hi @yonaskolb ,

Thanks for looking into this. Agree that passing a complex object as a path param doesn't make sense.

However, $ref isn't necessarily an object schema, it can be just a type alias, for example

"UUID": {
        "type": "string"
      }

Would it make sense to allow $ref as path param provided its type is a primitive?

yonaskolb commented 5 years ago

Yeah that should work fine. The reference isn’t the problem (anymore) it’s that the schema type is an object

Kastet commented 5 years ago

Awesome!

Just tested and found one minor problem. The UUID schema resolves into a type alias public typealias UUID = String which exactly what I'd expect, but it collides with Foundation's UUID extension in Coding.swift

extension UUID {
    func encode() -> Any {
        return uuidString
    }
}

A simple fix would be changing the template to

extension Foundation.UUID {
    func encode() -> Any {
        return uuidString
    }
}

Thoughts @yonaskolb ?

yonaskolb commented 5 years ago

Hmm, I'm not seeing this. There is a template option to map what ID is generated as (typeAliases.ID). It defaults to UUID In Coding.swift this is then generated as public typealias ID = UUID. Are you using the latest release, and the included template?

Kastet commented 5 years ago

Yep, using 4.0.0. image

yonaskolb commented 5 years ago

And where are you getting the public typealias UUID = String from?

Kastet commented 5 years ago

It's a model that gets generated from a schema, to illustrate

"components": {
    "schemas": {
      "UUID": {
           "type": "string"
      },
      "User": {
        "type": "object",
        "properties": {
          "id": {
            "$ref": "#/components/schemas/UUID"
          },
         .....
    }
}
yonaskolb commented 5 years ago

Oh I see, you have a custom schema that conflicts with Foundation. I would suggest either:

Either way happy for you to create a PR to add the Foundation.UUID