From 866fde18d8ad3386f01d848625370f08b78bbbd3 Mon Sep 17 00:00:00 2001 From: Kevin Wooten Date: Thu, 14 Nov 2024 14:31:59 -0700 Subject: [PATCH] Fixes for hierarchies in Kotlin, Swift & Typescript ## All Lookup inherited/inheriting by non-nullable type ## Swift Use non-optional `AnyRef` type for `ifPresent` decoding ## Kotlin Remove inherited properties from declared properties to support constraining properties in sub-types --- .../generator/common/ResolutionContext.kt | 16 +-- .../generator/kotlin/KotlinTypeRegistry.kt | 4 +- .../generator/swift/SwiftTypeRegistry.kt | 2 +- .../sunday/generator/utils/WebApiExts.kt | 1 + .../generator/kotlin/RamlObjectTypesTest.kt | 98 ++++++++++++++++++- .../generator/swift/RamlObjectTypesTest.kt | 35 ++++++- .../typescript/RamlObjectTypesTest.kt | 56 ++++++++++- .../tools/LocalTypeScriptCompiler.kt | 9 ++ .../types/obj-property-nullability.raml | 23 +++++ 9 files changed, 224 insertions(+), 20 deletions(-) diff --git a/generator/src/main/kotlin/io/outfoxx/sunday/generator/common/ResolutionContext.kt b/generator/src/main/kotlin/io/outfoxx/sunday/generator/common/ResolutionContext.kt index b1c2908fc..2c4502b99 100644 --- a/generator/src/main/kotlin/io/outfoxx/sunday/generator/common/ResolutionContext.kt +++ b/generator/src/main/kotlin/io/outfoxx/sunday/generator/common/ResolutionContext.kt @@ -25,13 +25,7 @@ import amf.core.client.platform.model.domain.PropertyShape import amf.core.client.platform.model.domain.Shape import amf.core.internal.annotations.Aliases import amf.shapes.client.platform.model.domain.NodeShape -import io.outfoxx.sunday.generator.utils.allUnits -import io.outfoxx.sunday.generator.utils.annotations -import io.outfoxx.sunday.generator.utils.declares -import io.outfoxx.sunday.generator.utils.id -import io.outfoxx.sunday.generator.utils.location -import io.outfoxx.sunday.generator.utils.name -import io.outfoxx.sunday.generator.utils.nonPatternProperties +import io.outfoxx.sunday.generator.utils.* import scala.collection.JavaConverters interface ResolutionContext { @@ -39,13 +33,13 @@ interface ResolutionContext { val unit: BaseUnit val shapeIndex: ShapeIndex - fun hasInherited(shape: Shape): Boolean = shapeIndex.hasInherited(shape) + fun hasInherited(shape: Shape): Boolean = shapeIndex.hasInherited(shape.nonNullableType) - fun hasNoInherited(shape: Shape): Boolean = shapeIndex.hasNoInherited(shape) + fun hasNoInherited(shape: Shape): Boolean = shapeIndex.hasNoInherited(shape.nonNullableType) - fun hasNoInheriting(shape: Shape): Boolean = shapeIndex.hasNoInheriting(shape) + fun hasNoInheriting(shape: Shape): Boolean = shapeIndex.hasNoInheriting(shape.nonNullableType) - fun findRootShape(shape: Shape): Shape = findSuperShapeOrNull(shape)?.let(this::findRootShape) ?: shape + fun findRootShape(shape: Shape): Shape = findSuperShapeOrNull(shape.nonNullableType)?.let(this::findRootShape) ?: shape fun findSuperShapeOrNull(shape: Shape): Shape? { val superShapeId = shapeIndex.findSuperShapeIdOrNull(shape) ?: return null diff --git a/generator/src/main/kotlin/io/outfoxx/sunday/generator/kotlin/KotlinTypeRegistry.kt b/generator/src/main/kotlin/io/outfoxx/sunday/generator/kotlin/KotlinTypeRegistry.kt index 99478de4e..7a6f3d69c 100644 --- a/generator/src/main/kotlin/io/outfoxx/sunday/generator/kotlin/KotlinTypeRegistry.kt +++ b/generator/src/main/kotlin/io/outfoxx/sunday/generator/kotlin/KotlinTypeRegistry.kt @@ -518,7 +518,7 @@ class KotlinTypeRegistry( resolveReferencedTypeName(itemsShape, context) } ?: ANY - + val collectionType = if (shape.uniqueItems == true) { SET @@ -612,7 +612,7 @@ class KotlinTypeRegistry( } var inheritedProperties = superShape?.let(context::findAllProperties) ?: emptyList() - var declaredProperties = context.findProperties(shape) + var declaredProperties = context.findProperties(shape).filter { dec -> dec.name !in inheritedProperties.map { it.name } } val inheritingTypes = context.findInheritingShapes(shape) diff --git a/generator/src/main/kotlin/io/outfoxx/sunday/generator/swift/SwiftTypeRegistry.kt b/generator/src/main/kotlin/io/outfoxx/sunday/generator/swift/SwiftTypeRegistry.kt index cedd84330..053b73ca4 100644 --- a/generator/src/main/kotlin/io/outfoxx/sunday/generator/swift/SwiftTypeRegistry.kt +++ b/generator/src/main/kotlin/io/outfoxx/sunday/generator/swift/SwiftTypeRegistry.kt @@ -933,7 +933,7 @@ class SwiftTypeRegistry( when { !isLeaf -> { propertyTypeName = if (isOptional) { - (propertyTypeName.makeNonOptional() as DeclaredTypeName).nestedType(ANY_REF_NAME).makeOptional() + (propertyTypeName.makeNonOptional() as DeclaredTypeName).nestedType(ANY_REF_NAME) } else { (propertyTypeName as DeclaredTypeName).nestedType(ANY_REF_NAME) } diff --git a/generator/src/main/kotlin/io/outfoxx/sunday/generator/utils/WebApiExts.kt b/generator/src/main/kotlin/io/outfoxx/sunday/generator/utils/WebApiExts.kt index 8248d9a6c..034b8e45a 100644 --- a/generator/src/main/kotlin/io/outfoxx/sunday/generator/utils/WebApiExts.kt +++ b/generator/src/main/kotlin/io/outfoxx/sunday/generator/utils/WebApiExts.kt @@ -326,6 +326,7 @@ val Shape.ifShape: Shape? get() = this.ifShape() val Shape.thenShape: Shape? get() = this.thenShape() val Shape.elseShape: Shape? get() = this.elseShape() val Shape.inlined: Boolean get() = this.annotations.inlinedElement() +val Shape.nonNullableType: Shape get() = if (this is UnionShape) this.nullableType else this // val ShapeExtension.definedBy: PropertyShape get() = this.definedBy() diff --git a/generator/src/test/kotlin/io/outfoxx/sunday/generator/kotlin/RamlObjectTypesTest.kt b/generator/src/test/kotlin/io/outfoxx/sunday/generator/kotlin/RamlObjectTypesTest.kt index 8f71e330c..e6b679345 100644 --- a/generator/src/test/kotlin/io/outfoxx/sunday/generator/kotlin/RamlObjectTypesTest.kt +++ b/generator/src/test/kotlin/io/outfoxx/sunday/generator/kotlin/RamlObjectTypesTest.kt @@ -77,7 +77,8 @@ class RamlObjectTypesTest { val typeRegistryOptions = setOf