Repository: cjbooms/fabrikt
Pull request #35 (2021-01-26 23:54:34)

Improve external reference support
Fixes the issue where generation stops when an array element in an externally referenced schema is encountered

averabaq: 👍 
cjbooms: 👍 

Patch:
diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt
index 210ce6c..f602fb4 100644
--- a/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt
+++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt
@@ -179,6 +179,11 @@ class JacksonModelGenerator(
                                     it.name.toModelClassName(enclosingModelName), props
                                 )
                             }
+                            items.isReferenceObjectDefinition() -> items.topLevelProperties(HTTP_SETTINGS, enclosingSchema)
+                                .let { props ->
+                                    buildInLinedModels(props, enclosingSchema) +
+                                        standardDataClass(items.safeName().toModelClassName(), props)
+                                }
                             items.isEnumDefinition() ->
                                 setOf(buildEnumClass(KotlinTypeInfo.from(items, "items", enclosingModelName) as KotlinTypeInfo.Enum))
                             else -> emptySet()


Test patch:
diff --git a/src/test/resources/examples/externalReferences/external-models.yaml b/src/test/resources/examples/externalReferences/external-models.yaml
index d016002..9287d70 100644
--- a/src/test/resources/examples/externalReferences/external-models.yaml
+++ b/src/test/resources/examples/externalReferences/external-models.yaml
@@ -3,5 +3,30 @@ components:
     ExternalObject:
       type: object
       properties:
-        some-string:
+        another:
+          $ref: "#/components/schemas/ExternalObjectTwo"
+          
+    ExternalObjectTwo:
+      type: object
+      required:
+        - errors
+      properties:
+        list-others:
+          type: array
+          items:
+            $ref: '#/components/schemas/ExternalObjectThree'
+   
+    ExternalObjectThree:
+      type: object
+      required:
+        - enum
+        - description
+      properties:
+        enum:
+          type: string
+          enum:
+            - one
+            - two
+            - three
+        description:
           type: string
\ No newline at end of file
diff --git a/src/test/resources/examples/externalReferences/models/Models.kt b/src/test/resources/examples/externalReferences/models/Models.kt
index 3b59611..e542f9f 100644
--- a/src/test/resources/examples/externalReferences/models/Models.kt
+++ b/src/test/resources/examples/externalReferences/models/Models.kt
@@ -1,8 +1,11 @@
 package examples.externalReferences.models
 
 import com.fasterxml.jackson.annotation.JsonProperty
+import com.fasterxml.jackson.annotation.JsonValue
 import javax.validation.Valid
+import javax.validation.constraints.NotNull
 import kotlin.String
+import kotlin.collections.List
 
 data class ContainingExternalReference(
     @param:JsonProperty("some-external-reference")
@@ -11,8 +14,38 @@ data class ContainingExternalReference(
     val someExternalReference: ExternalObject? = null
 )
 
+enum class Enum(
+    @JsonValue
+    val value: String
+) {
+    ONE("one"),
+
+    TWO("two"),
+
+    THREE("three");
+}
+
+data class ExternalObjectThree(
+    @param:JsonProperty("enum")
+    @get:JsonProperty("enum")
+    @get:NotNull
+    val enum: Enum,
+    @param:JsonProperty("description")
+    @get:JsonProperty("description")
+    @get:NotNull
+    val description: String
+)
+
+data class ExternalObjectTwo(
+    @param:JsonProperty("list-others")
+    @get:JsonProperty("list-others")
+    @get:Valid
+    val listOthers: List<ExternalObjectThree>? = null
+)
+
 data class ExternalObject(
-    @param:JsonProperty("some-string")
-    @get:JsonProperty("some-string")
-    val someString: String? = null
+    @param:JsonProperty("another")
+    @get:JsonProperty("another")
+    @get:Valid
+    val another: ExternalObjectTwo? = null
 )
diff --git a/src/test/resources/examples/githubApi/models/Models.kt b/src/test/resources/examples/githubApi/models/Models.kt
index a96ebe7..5e8f639 100644
--- a/src/test/resources/examples/githubApi/models/Models.kt
+++ b/src/test/resources/examples/githubApi/models/Models.kt
@@ -91,50 +91,14 @@ data class ContributorQueryResult(
     val items: List<Contributor>
 )
 
-data class OrganisationQueryResult(
-    @param:JsonProperty("prev")
-    @get:JsonProperty("prev")
-    val prev: String? = null,
-    @param:JsonProperty("next")
-    @get:JsonProperty("next")
-    val next: String? = null,
-    @param:JsonProperty("items")
-    @get:JsonProperty("items")
-    @get:NotNull
-    @get:Size(min = 0)
-    @get:Valid
-    val items: List<Organisation>
-)
-
-data class RepositoryQueryResult(
-    @param:JsonProperty("prev")
-    @get:JsonProperty("prev")
-    val prev: String? = null,
-    @param:JsonProperty("next")
-    @get:JsonProperty("next")
-    val next: String? = null,
-    @param:JsonProperty("items")
-    @get:JsonProperty("items")
-    @get:NotNull
-    @get:Size(min = 0)
-    @get:Valid
-    val items: List<Repository>
-)
+enum class Status(
+    @JsonValue
+    val value: String
+) {
+    ACTIVE("active"),
 
-data class PullRequestQueryResult(
-    @param:JsonProperty("prev")
-    @get:JsonProperty("prev")
-    val prev: String? = null,
-    @param:JsonProperty("next")
-    @get:JsonProperty("next")
-    val next: String? = null,
-    @param:JsonProperty("items")
-    @get:JsonProperty("items")
-    @get:NotNull
-    @get:Size(min = 0)
-    @get:Valid
-    val items: List<PullRequest>
-)
+    INACTIVE("inactive");
+}
 
 data class Contributor(
     @param:JsonProperty("id")
@@ -177,14 +141,30 @@ data class Contributor(
     val name: String? = null
 )
 
-enum class Status(
-    @JsonValue
-    val value: String
-) {
-    ACTIVE("active"),
+data class OrganisationQueryResult(
+    @param:JsonProperty("prev")
+    @get:JsonProperty("prev")
+    val prev: String? = null,
+    @param:JsonProperty("next")
+    @get:JsonProperty("next")
+    val next: String? = null,
+    @param:JsonProperty("items")
+    @get:JsonProperty("items")
+    @get:NotNull
+    @get:Size(min = 0)
+    @get:Valid
+    val items: List<Organisation>
+)
 
-    INACTIVE("inactive");
-}
+data class Webhook(
+    @param:JsonProperty("url")
+    @get:JsonProperty("url")
+    @get:NotNull
+    val url: String,
+    @param:JsonProperty("name")
+    @get:JsonProperty("name")
+    val name: String? = null
+)
 
 data class Organisation(
     @param:JsonProperty("id")
@@ -231,6 +211,30 @@ data class Organisation(
     val hooks: List<Webhook>? = null
 )
 
+data class RepositoryQueryResult(
+    @param:JsonProperty("prev")
+    @get:JsonProperty("prev")
+    val prev: String? = null,
+    @param:JsonProperty("next")
+    @get:JsonProperty("next")
+    val next: String? = null,
+    @param:JsonProperty("items")
+    @get:JsonProperty("items")
+    @get:NotNull
+    @get:Size(min = 0)
+    @get:Valid
+    val items: List<Repository>
+)
+
+enum class Visibility(
+    @JsonValue
+    val value: String
+) {
+    PRIVATE("Private"),
+
+    PUBLIC("Public");
+}
+
 data class Repository(
     @param:JsonProperty("id")
     @get:JsonProperty("id")
@@ -279,14 +283,29 @@ data class Repository(
     val tags: List<String>? = null
 )
 
-enum class Visibility(
-    @JsonValue
-    val value: String
-) {
-    PRIVATE("Private"),
+data class PullRequestQueryResult(
+    @param:JsonProperty("prev")
+    @get:JsonProperty("prev")
+    val prev: String? = null,
+    @param:JsonProperty("next")
+    @get:JsonProperty("next")
+    val next: String? = null,
+    @param:JsonProperty("items")
+    @get:JsonProperty("items")
+    @get:NotNull
+    @get:Size(min = 0)
+    @get:Valid
+    val items: List<PullRequest>
+)
 
-    PUBLIC("Public");
-}
+data class Author(
+    @param:JsonProperty("name")
+    @get:JsonProperty("name")
+    val name: String? = null,
+    @param:JsonProperty("email")
+    @get:JsonProperty("email")
+    val email: String? = null
+)
 
 data class PullRequest(
     @param:JsonProperty("id")
@@ -333,25 +352,6 @@ data class PullRequest(
     val author: Author? = null
 )
 
-data class Author(
-    @param:JsonProperty("name")
-    @get:JsonProperty("name")
-    val name: String? = null,
-    @param:JsonProperty("email")
-    @get:JsonProperty("email")
-    val email: String? = null
-)
-
-data class Webhook(
-    @param:JsonProperty("url")
-    @get:JsonProperty("url")
-    @get:NotNull
-    val url: String,
-    @param:JsonProperty("name")
-    @get:JsonProperty("name")
-    val name: String? = null
-)
-
 enum class StatusQueryParam(
     @JsonValue
     val value: String
diff --git a/src/test/resources/examples/githubApi/resources/reflection-config.json b/src/test/resources/examples/githubApi/resources/reflection-config.json
index 06bb896..d28ef79 100644
--- a/src/test/resources/examples/githubApi/resources/reflection-config.json
+++ b/src/test/resources/examples/githubApi/resources/reflection-config.json
@@ -39,7 +39,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.OrganisationQueryResult",
+  "name" : "examples.githubApi.models.Status",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -47,7 +47,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.RepositoryQueryResult",
+  "name" : "examples.githubApi.models.Contributor",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -55,7 +55,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.PullRequestQueryResult",
+  "name" : "examples.githubApi.models.OrganisationQueryResult",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -63,7 +63,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.Contributor",
+  "name" : "examples.githubApi.models.Webhook",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -71,7 +71,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.Status",
+  "name" : "examples.githubApi.models.Organisation",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -79,7 +79,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.Organisation",
+  "name" : "examples.githubApi.models.RepositoryQueryResult",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -87,7 +87,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.Repository",
+  "name" : "examples.githubApi.models.Visibility",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -95,7 +95,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.Visibility",
+  "name" : "examples.githubApi.models.Repository",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -103,7 +103,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.PullRequest",
+  "name" : "examples.githubApi.models.PullRequestQueryResult",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,
@@ -119,7 +119,7 @@
   "allDeclaredFields" : true,
   "allPublicFields" : true
 }, {
-  "name" : "examples.githubApi.models.Webhook",
+  "name" : "examples.githubApi.models.PullRequest",
   "allDeclaredConstructors" : true,
   "allPublicConstructors" : true,
   "allDeclaredMethods" : true,