swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.88k stars 6.03k forks source link

[Swift3] Array of string appears broken #6238

Open julienfouilhe opened 7 years ago

julienfouilhe commented 7 years ago
Description

Hi guys, Arrays of strings appear broken in the swift 3 client. Using the swagger declaration below, it generates the following file:

public typealias UserInstruments = []

When it should be

public typealias UserInstruments = [String]

And in the Decoders:

        // Decoder for UserInstruments
        Decoders.addDecoder(clazz: UserInstruments.self) { (source: AnyObject, instance: AnyObject?) -> UserInstruments in
            let sourceArray = source as! [AnyObject]
            return sourceArray.map({ Decoders.decode(clazz: .self, source: $0, instance: nil) })
        }

When it should be:

        // Decoder for UserInstruments
        Decoders.addDecoder(clazz: UserInstruments.self) { (source: AnyObject, instance: AnyObject?) -> UserInstruments in
            let sourceArray = source as! [AnyObject]
            return sourceArray.map({ Decoders.decode(clazz: String.self, source: $0, instance: nil) })
        }

Still trying to spot the problem, but if someone that actually coded the client could have a look I would greatly appreciate it!

Thanks!

Swagger-codegen version

2.2.3

Swagger declaration file content or url
  UserInstruments:
    type: array
    description: |
      An array of the instrument identifiers that the user plays.
    items:
      type: string
      pattern: ^[a-z]+\.[a-z-]+$
    example:
    - keyboards.grand-piano
    - brass.trumpet
    - strings.violin
Command line used for generation

swagger-codegen generate -i https://url.com -l swift3 -o ../api-client-swift3 -c conf.json

julienfouilhe commented 7 years ago

cc @Edubits @jaz-ah @jgavris @wing328

Edit: Tried with master, and now it appears it doesn't even create a decoder for UserInstruments

julienfouilhe commented 7 years ago

Ok so this is due to :

m.arrayModelType = fromProperty(name, arrayProperty).complexType;

But when the array is strings, in updatePropertyForArray:

        if (!languageSpecificPrimitives.contains(innerProperty.baseType)) {
            property.complexType = innerProperty.baseType;
        } else {
            property.isPrimitiveType = true;
        }

complexType isn't set.

I don't know why this condition is there but this is in the DefaultCodegen so I don't feel like changing anything! :p

jaz-ah commented 7 years ago

@julienfouilhe do you know if the same issue exists on latest master/2.3.0?

julienfouilhe commented 7 years ago

@jaz-ah It does on master, except that the decoder does not exist for this type anymore. Since it's an array of string I guess it's okay and it will go through the "basic" one (not sure though).

But the typealias error is still there

jaz-ah commented 7 years ago

ok thx @julienfouilhe - did this break recently for you or is this something new - just trying to see if I can track down the history in the code.

julienfouilhe commented 7 years ago

@jaz-ah I wasn't using arrays of strings before, new models and routes in the swagger spec revealed that error. So I don't know when this was introduced or if it was always there.

jaz-ah commented 7 years ago

ok i'll see if i can find some time this weekend to take a look unless @jgavris beats me to it.

julienfouilhe commented 7 years ago

I guess we could just change the template to get the type of the items, but I tried {{items.dataType}} and it didn't appear to work

julienfouilhe commented 7 years ago

Actually, master/2.3.0 breaks a lot of things for me, notably this PR I submitted #5433. I'll see later if I can fix that back.