ehyche a3d0f1d4bd Swift4: make generated models structs instead of classes (#7345)
* Split up model template into partials

* Change models from class to struct.

This fixes issue https://github.com/swagger-api/swagger-codegen/issues/6941.

In this change, we make our Swift4 generated model objects struct instead of class. However, in order to do this, we needed to handle the following edge cases:

* Inheritance and polymorphism (allOf)
  * With classes, we use inheritance. So therefore, the parent properties are ONLY on the parent generated class, and the model object which derives from the parent class picks up those properties through inheritance.
  * However, structs do not support inheritance. So we simply duplicate the parent allOf properties in the child struct.
* We have to handle the case where the property name on the struct may be different than the property name in the JSON. By default, the Codable protocol assumes that the JSON property name is the same as the struct property name. If they need to be different, then we generate a CodingKeys string enum, which contains the mapping between struct property name and JSON property name.
* additionalProperties. We cannot use the default Codable implementation for the additionalProperties, since it will look for an actual dictionary called "additionalProperties" in the JSON. Therefore, for model objects which have additionalProperties, we must generate our own implementation for the Decodable and Encodable protocols.

I have run ./bin/swift4-all.sh and ./bin/swift4-test.sh to re-generate all of the sources, and I have verified that the generated code in samples/clients/test/swift4/default builds and the unit tests pass.

* Update VERSION in .swagger-codegen

* Update generated code for swift4-test schema
2018-01-25 21:33:11 +08:00

44 lines
1.5 KiB
Swift

//
// TestClientAppTests.swift
// TestClientAppTests
//
// Created by Eric Hyche on 7/18/17.
// Copyright © 2017 Swagger Codegen. All rights reserved.
//
import XCTest
import TestClient
@testable import TestClientApp
class TestClientAppTests: XCTestCase {
func testWhenVariableNameIsDifferentFromPropertyName() {
// This tests to make sure that the swift4 language can handle when
// we have property names which map to variable names that are not the same.
// This can happen when we have things like snake_case property names,
// or when we have property names which may be Swift 4 reserved words.
let jsonData = """
{
"example_name": "Test example name",
"for": "Some reason",
"normalName": "Some normal name value"
}
""".data(using: .utf8)!
let decodedResult = CodableHelper.decode(VariableNameTest.self, from: jsonData)
XCTAssertNil(decodedResult.error, "Got an error decoding VariableNameTest, error=\(String(describing: decodedResult.error))")
guard let variableNameTest = decodedResult.decodableObj else {
XCTFail("Did not get an VariableNameTest decoded object")
return
}
XCTAssertTrue(variableNameTest.exampleName == "Test example name", "Did not decode snake_case property correctly.")
XCTAssertTrue(variableNameTest._for == "Some reason", "Did not decode property name that is a reserved word correctly.")
}
}