Skip to content

Commit

Permalink
Nullable arrays (#301)
Browse files Browse the repository at this point in the history
* test: Added nullableArrayItems model generation test

* Fix nullable array parameterized type.
Also combining the two latest test cases around nullable true into existing test cases to reduce example set

---------

Co-authored-by: DavidMazarro <[email protected]>
  • Loading branch information
cjbooms and DavidMazarro authored Jul 14, 2024
1 parent 9600df3 commit 8caa7c4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class JacksonModelGenerator(
toModelType(
basePackage,
typeInfo.parameterizedType,
typeInfo.isParameterizedTypeNullable
),
)

Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/cjbooms/fabrikt/model/KotlinTypeInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ sealed class KotlinTypeInfo(val modelKClass: KClass<*>, val generatedModelClassN
object UntypedObject : KotlinTypeInfo(Any::class)
object AnyType : KotlinTypeInfo(Any::class)
data class Object(val simpleClassName: String) : KotlinTypeInfo(GeneratedType::class, simpleClassName)
data class Array(val parameterizedType: KotlinTypeInfo) : KotlinTypeInfo(List::class)
data class Array(
val parameterizedType: KotlinTypeInfo,
val isParameterizedTypeNullable: kotlin.Boolean = false
) : KotlinTypeInfo(List::class)

data class Map(val parameterizedType: KotlinTypeInfo) : KotlinTypeInfo(Map::class)
object UnknownAdditionalProperties : KotlinTypeInfo(Any::class)
object UntypedObjectAdditionalProperties : KotlinTypeInfo(Any::class)
Expand Down Expand Up @@ -77,7 +81,7 @@ sealed class KotlinTypeInfo(val modelKClass: KClass<*>, val generatedModelClassN
OasType.Array ->
if (schema.itemsSchema.isNotDefined())
throw IllegalArgumentException("Property ${schema.name} cannot be parsed to a Schema. Check your input")
else Array(from(schema.itemsSchema, oasKey, enclosingSchema))
else Array(from(schema.itemsSchema, oasKey, enclosingSchema), schema.itemsSchema.isNullable)

OasType.Object -> Object(ModelNameRegistry.getOrRegister(schema, enclosingSchema))
OasType.Map ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class ModelGeneratorTest {
"openapi310",
"binary",
"oneOfMarkerInterface",
"requiredButNullableObjectProperty",
)

@BeforeEach
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/examples/arrays/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ components:
properties:
some-array-prop:
type: string
a-nullable-array:
type: array
nullable: true
items:
type: string
nullable: true

ArrayOfSomething:
type: array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples.arrays.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import javax.validation.Valid
import kotlin.String
import kotlin.collections.List

public data class ContainsArrayOfArrays(
Expand All @@ -13,4 +14,7 @@ public data class ContainsArrayOfArrays(
@get:JsonProperty("absent-object-type-in-array")
@get:Valid
public val absentObjectTypeInArray: List<ContainsArrayOfArraysAbsentObjectTypeInArray>? = null,
@param:JsonProperty("a-nullable-array")
@get:JsonProperty("a-nullable-array")
public val aNullableArray: List<String?>? = null,
)
10 changes: 7 additions & 3 deletions src/test/resources/examples/optionalVsRequired/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ components:
type: object
required:
- name
- requiredNullable
- requiredNullableString
- requiredNullableUntypedObject
properties:
name:
type: string
gender:
type: string
format: uuid
requiredNullable:
requiredNullableString:
type: string
nullable: true
nullable: true
requiredNullableUntypedObject:
type: object
nullable: true
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package examples.optionalVsRequired.models
import com.fasterxml.jackson.`annotation`.JsonProperty
import java.util.UUID
import javax.validation.constraints.NotNull
import kotlin.Any
import kotlin.String
import kotlin.collections.Map

public data class OptionalVsRequired(
@param:JsonProperty("name")
Expand All @@ -13,7 +15,10 @@ public data class OptionalVsRequired(
@param:JsonProperty("gender")
@get:JsonProperty("gender")
public val gender: UUID? = null,
@param:JsonProperty("requiredNullable")
@get:JsonProperty("requiredNullable")
public val requiredNullable: String?,
@param:JsonProperty("requiredNullableString")
@get:JsonProperty("requiredNullableString")
public val requiredNullableString: String?,
@param:JsonProperty("requiredNullableUntypedObject")
@get:JsonProperty("requiredNullableUntypedObject")
public val requiredNullableUntypedObject: Map<String, Any?>?,
)

This file was deleted.

This file was deleted.

0 comments on commit 8caa7c4

Please sign in to comment.