Skip to content

Commit

Permalink
Add missing tests for generating arrays of any
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Oct 29, 2024
1 parent c5870d7 commit 06c1851
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,13 @@ class KotlinTypeRegistry(

private fun processArrayShape(shape: ArrayShape, context: KotlinResolutionContext): TypeName {

val elementType = resolveReferencedTypeName(shape.items!!, context)

val elementType =
shape.items
?.let { itemsShape ->
resolveReferencedTypeName(itemsShape, context)
}
?: ANY

val collectionType =
if (shape.uniqueItems == true) {
SET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,12 @@ class SwiftTypeRegistry(

private fun processArrayShape(shape: ArrayShape, context: SwiftResolutionContext): TypeName {

val elementType = resolveReferencedTypeName(shape.items!!, context)
val elementType =
shape.items
?.let { itemsShape ->
resolveReferencedTypeName(itemsShape, context)
}
?: ANY

val collectionType =
if (shape.uniqueItems == true) {
Expand Down Expand Up @@ -948,7 +953,7 @@ class SwiftTypeRegistry(
propertyTypeName == DICTIONARY_STRING_ANY || propertyTypeName == DICTIONARY_STRING_ANY_OPTIONAL -> {

propertyTypeName = DICTIONARY.parameterizedBy(STRING, ANY_VALUE)
decoderPost.add("${if (isOptional) "?" else ""}.mapValues { $0.unwrapped as Any }")
decoderPost.add("${if (isOptional) "?" else ""}.mapValues { $0.unwrapped }")
encoderPre.add("${if (isOptional) "?" else ""}.mapValues { try %T.wrapped($0) }", ANY_VALUE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,12 @@ class TypeScriptTypeRegistry(

private fun processArrayShape(shape: ArrayShape, context: TypeScriptResolutionContext): TypeName {

val elementType = resolveReferencedTypeName(shape.items!!, context)
val elementType =
shape.items
?.let { itemsShape ->
resolveReferencedTypeName(itemsShape, context)
}
?: UNKNOWN

val collectionType =
if (shape.uniqueItems == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ class RamlObjectTypesTest {
import kotlin.Any
import kotlin.String
import kotlin.collections.List
import kotlin.collections.Map
public interface Test {
public val map: Map<String, Any>
public val array: Array<Any>
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,43 @@ class RamlObjectTypesTest {
public class Test : Codable, CustomDebugStringConvertible {
public var map: [String : Any]
public var array: [Any]
public var debugDescription: String {
return DescriptionBuilder(Test.self)
.add(map, named: "map")
.add(array, named: "array")
.build()
}
public init(map: [String : Any]) {
public init(map: [String : Any], array: [Any]) {
self.map = map
self.array = array
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.map = try container.decode([String : AnyValue].self, forKey: .map).mapValues { ${'$'}0.unwrapped as Any }
self.map = try container.decode([String : AnyValue].self, forKey: .map).mapValues { ${'$'}0.unwrapped }
self.array = try container.decode([AnyValue].self, forKey: .array).map { ${'$'}0.unwrapped }
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.map.mapValues { try AnyValue.wrapped(${'$'}0) }, forKey: .map)
try container.encode(self.array.map { try AnyValue.wrapped(${'$'}0) }, forKey: .array)
}
public func withMap(map: [String : Any]) -> Test {
return Test(map: map)
return Test(map: map, array: array)
}
public func withArray(array: [Any]) -> Test {
return Test(map: map, array: array)
}
fileprivate enum CodingKeys : String, CodingKey {
case map = "map"
case array = "array"
}
Expand Down Expand Up @@ -184,8 +194,8 @@ class RamlObjectTypesTest {
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.optionalObject = try container.decodeIfPresent([String : AnyValue].self, forKey: .optionalObject)?.mapValues { ${'$'}0.unwrapped as Any }
self.nillableObject = try container.decodeIfPresent([String : AnyValue].self, forKey: .nillableObject)?.mapValues { ${'$'}0.unwrapped as Any }
self.optionalObject = try container.decodeIfPresent([String : AnyValue].self, forKey: .optionalObject)?.mapValues { ${'$'}0.unwrapped }
self.nillableObject = try container.decodeIfPresent([String : AnyValue].self, forKey: .nillableObject)?.mapValues { ${'$'}0.unwrapped }
}
public func encode(to encoder: Encoder) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,27 @@ class RamlObjectTypesTest {
map: Record<string, unknown>;
array: Array<unknown>;
}
export class Test implements TestSpec {
map: Record<string, unknown>;
array: Array<unknown>;
constructor(init: TestSpec) {
this.map = init.map;
this.array = init.array;
}
copy(changes: Partial<TestSpec>): Test {
return new Test(Object.assign({}, this, changes));
}
toString(): string {
return `Test(map='${'$'}{this.map}')`;
return `Test(map='${'$'}{this.map}', array='${'$'}{this.array}')`;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ types:
type: object
additionalProperties: true

Array:
type: array

Test:
type: object
properties:
map: Map
array: Array

/tests/{id}:

Expand Down

0 comments on commit 06c1851

Please sign in to comment.