Skip to content

Commit

Permalink
generate typealias for models that only have additionalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
yonaskolb committed Sep 26, 2017
1 parent 3b53bd9 commit ca04f2f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 41 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

## [1.1.0]
### Added
- Generate typealias models for non schema definitions #42 thanks @Liquidsoul
- references to other types
- arrays
- dictionaries
- Generate typealias for models with reference and array types #42 thanks @Liquidsoul
- generate typealias for models that only have additional properties

### Fixed
- fixed SPM installation issue
Expand Down
1 change: 0 additions & 1 deletion Sources/SwagGenKit/CodeFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public class CodeFormatter {
context["referenceType"] = schemaType
context["aliasType"] = schemaType
case .array:
let schemaType = getSchemaType(name: schema.name, schema: schema.value)
context["arrayType"] = schemaType
context["aliasType"] = schemaType
default: break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,4 @@ import Foundation
import JSONUtilities

/** definition with only additional properties */
public class ModelWithOnlyAdditionalProperties: JSONDecodable, JSONEncodable, PrettyPrintable {

public var additionalProperties: [String: Any] = [:]

public init() {
}

public required init(jsonDictionary: JSONDictionary) throws {

var additionalProperties = jsonDictionary
self.additionalProperties = additionalProperties
}

public func encode() -> JSONDictionary {
var dictionary: JSONDictionary = [:]

for (key, value) in additionalProperties {
dictionary[key] = value
}
return dictionary
}

public subscript(key: String) -> Any? {
get {
return additionalProperties[key]
}
set {
additionalProperties[key] = newValue
}
}

/// pretty prints all properties including nested models
public var prettyPrinted: String {
return "\(Swift.type(of: self)):\n\(encode().recursivePrint(indentIndex: 1))"
}
}
public typealias ModelWithOnlyAdditionalProperties = [String: Any]
2 changes: 2 additions & 0 deletions Templates/Swift/Includes/Model.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{% include "Includes/Enum.stencil" using enum %}
{% elif aliasType %}
public typealias {{ type }} = {{ aliasType }}
{% elif additionalPropertiesType and allProperties.count == 0 %}
public typealias {{ type }} = [String: {{ additionalPropertiesType }}]
{% else %}
public class {{ type }}: {% if parent %}{{ parent.type }}{% else %}JSONDecodable, JSONEncodable, PrettyPrintable{% endif %} {
{% for enum in enums %}
Expand Down

0 comments on commit ca04f2f

Please sign in to comment.