yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Kastet opened this issue · comments

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.

Thanks for the report @Kastet. I've fixed the unresolved component here #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?

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?

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

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 ?

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?

Yep, using 4.0.0.
image

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

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"
          },
         .....
    }
}

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

  • using swaggers inbuilt format: uuid instead of a custom UUID schema (SwagGen will generate this as an ID typealias, which you can change what that resolves to in the template options).
  • Just use a string type instead of a schema reference
  • Alternatively you can update the template with your Foundation suggestion, but you'll continue to get clashes throughout your codebase.

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