Skip to content

Commit

Permalink
Merge pull request #217 from nicholascross/feature/flatten_single_gro…
Browse files Browse the repository at this point in the history
…up_schema

Use first group schema type when there is only one group schema
  • Loading branch information
yonaskolb authored Feb 29, 2020
2 parents 9d6103a + c08f83d commit bbb1ad7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
- Fixed inline allOf group generation
- Fixed property generation when there is only one group schema; the first group schema type will be used as the type #217

## 4.3.1

Expand Down
7 changes: 1 addition & 6 deletions Sources/SwagGenKit/SwaggerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,7 @@ extension Schema {
break
}
case let .group(group):
switch group.type {
case .any, .one:
if group.discriminator != nil {
return self
}
case .all:
if group.discriminator != nil {
return self
}
default: break
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwagGenKit/SwiftFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ public class SwiftFormatter: CodeFormatter {
}
case let .reference(reference):
return getSchemaTypeName(reference.component)
case .group:
case let .group(groupSchema):
if groupSchema.schemas.count == 1, let singleGroupSchema = groupSchema.schemas.first {
//flatten group schemas with only one schema
return getSchemaType(name: name, schema: singleGroupSchema)
}

return escapeType(name.upperCamelCased())
case .any:
return "Any"
Expand Down
40 changes: 19 additions & 21 deletions Specs/TestSpec/generated/Swift/Sources/Models/Zoo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,17 @@ import Foundation

public class Zoo: APIModel {

public var inlineAnimal: InlineAnimal?
public var allOfDog: Dog?

public var inlineAnimals: [InlineAnimals]?

public var schemaAnimals: [SingleAnimal]?
public var anyOfDog: Dog?

public class InlineAnimal: Animal {
public var inlineAnimal: Animal?

public override init(animal: String? = nil) {
super.init(animal: animal)
}

public required init(from decoder: Decoder) throws {
try super.init(from: decoder)
}
public var inlineAnimals: [InlineAnimals]?

public override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
}
public var oneOfDog: Dog?

override public func isEqual(to object: Any?) -> Bool {
guard object is InlineAnimal else { return false }
return super.isEqual(to: object)
}
}
public var schemaAnimals: [SingleAnimal]?

public enum InlineAnimals: Codable, Equatable {
case cat(Cat)
Expand Down Expand Up @@ -61,32 +47,44 @@ public class Zoo: APIModel {
}
}

public init(inlineAnimal: InlineAnimal? = nil, inlineAnimals: [InlineAnimals]? = nil, schemaAnimals: [SingleAnimal]? = nil) {
public init(allOfDog: Dog? = nil, anyOfDog: Dog? = nil, inlineAnimal: Animal? = nil, inlineAnimals: [InlineAnimals]? = nil, oneOfDog: Dog? = nil, schemaAnimals: [SingleAnimal]? = nil) {
self.allOfDog = allOfDog
self.anyOfDog = anyOfDog
self.inlineAnimal = inlineAnimal
self.inlineAnimals = inlineAnimals
self.oneOfDog = oneOfDog
self.schemaAnimals = schemaAnimals
}

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)

allOfDog = try container.decodeIfPresent("allOfDog")
anyOfDog = try container.decodeIfPresent("anyOfDog")
inlineAnimal = try container.decodeIfPresent("inlineAnimal")
inlineAnimals = try container.decodeArrayIfPresent("inlineAnimals")
oneOfDog = try container.decodeIfPresent("oneOfDog")
schemaAnimals = try container.decodeArrayIfPresent("schemaAnimals")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)

try container.encodeIfPresent(allOfDog, forKey: "allOfDog")
try container.encodeIfPresent(anyOfDog, forKey: "anyOfDog")
try container.encodeIfPresent(inlineAnimal, forKey: "inlineAnimal")
try container.encodeIfPresent(inlineAnimals, forKey: "inlineAnimals")
try container.encodeIfPresent(oneOfDog, forKey: "oneOfDog")
try container.encodeIfPresent(schemaAnimals, forKey: "schemaAnimals")
}

public func isEqual(to object: Any?) -> Bool {
guard let object = object as? Zoo else { return false }
guard self.allOfDog == object.allOfDog else { return false }
guard self.anyOfDog == object.anyOfDog else { return false }
guard self.inlineAnimal == object.inlineAnimal else { return false }
guard self.inlineAnimals == object.inlineAnimals else { return false }
guard self.oneOfDog == object.oneOfDog else { return false }
guard self.schemaAnimals == object.schemaAnimals else { return false }
return true
}
Expand Down
9 changes: 9 additions & 0 deletions Specs/TestSpec/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ components:
inlineAnimal:
allOf:
- $ref: '#/components/schemas/Animal'
oneOfDog:
oneOf:
- $ref: '#/components/schemas/Dog'
anyOfDog:
anyOf:
- $ref: '#/components/schemas/Dog'
allOfDog:
allOf:
- $ref: '#/components/schemas/Dog'
SingleAnimal:
oneOf:
- $ref: "#/components/schemas/Cat"
Expand Down

0 comments on commit bbb1ad7

Please sign in to comment.