Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kdoc to models #384

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions end2end-tests/models-jackson/openapi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ components:
$ref: "#/components/schemas/RootDiscriminator"
rootField1:
type: string
description: this is a rootField1
rootField2:
type: boolean
description: this is a rootField2

FirstLevelChild:
allOf:
Expand Down Expand Up @@ -152,10 +154,12 @@ components:
type: string
required: true
nullable: false
description: commonField1
field2:
type: string
required: true
nullable: false
description: commonField2

# Polymorphism
PolymorphicTypeOneRef:
Expand Down Expand Up @@ -427,6 +431,3 @@ components:
properties:
bar_prop:
type: string



Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,18 @@ object GeneratorUtils {
val kdoc = CodeBlock.builder().add("${this.summary.orEmpty()}\n${this.description.orEmpty()}\n")

parameters.forEach {
kdoc.add("@param %L %L\n", it.name.toKCodeName(), it.description.orEmpty()).build()
kdoc.add("@param %L %L\n", it.name.toKCodeName(), it.description.orEmpty())
}

return kdoc.build()
}

fun Schema.toKDoc(): CodeBlock {
val kdoc = CodeBlock.builder().add("${this.description.orEmpty()}\n")

return kdoc.build()
}

fun TypeSpec.Builder.primaryPropertiesConstructor(vararg properties: PropertySpec): TypeSpec.Builder {
val propertySpecs = properties.map { it.toBuilder().initializer(it.name).build() }
val parameters = propertySpecs.map { ParameterSpec.builder(it.name, it.type).build() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.cjbooms.fabrikt.generators

import com.cjbooms.fabrikt.generators.GeneratorUtils.toKDoc
import com.cjbooms.fabrikt.generators.TypeFactory.maybeMakeMapValueNullable
import com.cjbooms.fabrikt.generators.model.JacksonMetadata
import com.cjbooms.fabrikt.model.SerializationAnnotations
import com.cjbooms.fabrikt.model.JacksonAnnotations
import com.cjbooms.fabrikt.model.KotlinTypeInfo
import com.cjbooms.fabrikt.model.PropertyInfo
import com.cjbooms.fabrikt.model.SerializationAnnotations
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
Expand Down Expand Up @@ -57,6 +58,7 @@ object PropertyUtils {
type
}
val property = PropertySpec.builder(name, wrappedType)
.addKdoc(this.schema.toKDoc())

if (this is PropertyInfo.AdditionalProperties) {
if (!serializationAnnotations.supportsAdditionalProperties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.cjbooms.fabrikt.cli.ModelCodeGenOptionType.SEALED_INTERFACES_FOR_ONE_
import com.cjbooms.fabrikt.configurations.Packages
import com.cjbooms.fabrikt.generators.ClassSettings
import com.cjbooms.fabrikt.generators.GeneratorUtils.toClassName
import com.cjbooms.fabrikt.generators.GeneratorUtils.toKDoc
import com.cjbooms.fabrikt.generators.GeneratorUtils.toObjectTypeSpec
import com.cjbooms.fabrikt.generators.MutableSettings
import com.cjbooms.fabrikt.generators.PropertyUtils.addToClass
Expand Down Expand Up @@ -175,22 +176,22 @@ class ModelGenerator(
private fun createModels(api: OpenApi3, schemas: List<SchemaInfo>) = schemas
.filterNot { it.schema.isSimpleType() }
.filterNot { it.schema.isOneOfPolymorphicTypes() }
.flatMap {
val properties = it.schema.topLevelProperties(HTTP_SETTINGS, api, it.schema)
.flatMap { schemaInfo ->
val properties = schemaInfo.schema.topLevelProperties(HTTP_SETTINGS, api, schemaInfo.schema)
when {
properties.isNotEmpty() ||
it.typeInfo is KotlinTypeInfo.Enum ||
it.schema.findOneOfSuperInterface(schemas.map { it.schema }).isNotEmpty() -> {
val primaryModel = buildPrimaryModel(api, it, properties, schemas)
val inlinedModels = buildInLinedModels(properties, it.schema, it.schema.getDocumentUrl())
schemaInfo.typeInfo is KotlinTypeInfo.Enum ||
schemaInfo.schema.findOneOfSuperInterface(schemas.map { it.schema }).isNotEmpty() -> {
val primaryModel = buildPrimaryModel(api, schemaInfo, properties, schemas)
val inlinedModels = buildInLinedModels(properties, schemaInfo.schema, schemaInfo.schema.getDocumentUrl())
listOf(primaryModel) + inlinedModels
}
it.typeInfo is KotlinTypeInfo.Array -> {
schemaInfo.typeInfo is KotlinTypeInfo.Array -> {
buildInlinedListDefinition(
schema = it.schema,
schemaName = it.schema.safeName(),
enclosingSchema = it.schema,
apiDocUrl = it.schema.getDocumentUrl(),
schema = schemaInfo.schema,
schemaName = schemaInfo.schema.safeName(),
enclosingSchema = schemaInfo.schema,
apiDocUrl = schemaInfo.schema.getDocumentUrl(),
)
}
else -> {
Expand Down Expand Up @@ -252,7 +253,7 @@ class ModelGenerator(
modelName = modelName,
schemaName = schemaName,
properties = properties,
extensions = schemaInfo.schema.extensions,
schema = schemaInfo.schema,
oneOfInterfaces = schemaInfo.schema.findOneOfSuperInterface(allSchemas.map { it.schema }),
)
}
Expand All @@ -279,7 +280,7 @@ class ModelGenerator(
ModelNameRegistry.getOrRegister(it.schema, enclosingSchema),
it.name,
props,
it.schema.extensions,
it.schema,
oneOfInterfaces = emptySet(),
)
val inlinedModels = buildInLinedModels(props, enclosingSchema, apiDocUrl)
Expand All @@ -298,7 +299,7 @@ class ModelGenerator(
modelName = ModelNameRegistry.getOrRegister(it.schema, valueSuffix = it.schema.isInlinedTypedAdditionalProperties()),
schemaName = it.name,
properties = it.schema.topLevelProperties(HTTP_SETTINGS, sourceApi.openApi3, enclosingSchema),
extensions = it.schema.extensions,
schema = it.schema,
oneOfInterfaces = emptySet(),
),
)
Expand Down Expand Up @@ -342,7 +343,7 @@ class ModelGenerator(
modelName = ModelNameRegistry.getOrRegister(schema, enclosingSchema),
schemaName = schemaName,
properties = props,
extensions = schema.extensions,
schema = schema,
oneOfInterfaces = emptySet(),
)
}
Expand Down Expand Up @@ -443,7 +444,7 @@ class ModelGenerator(
modelName = ModelNameRegistry.getOrRegister(schema, valueSuffix = schema.isInlinedTypedAdditionalProperties()),
schemaName = schema.safeName(),
properties = mapField.schema.additionalPropertiesSchema.topLevelProperties(HTTP_SETTINGS, sourceApi.openApi3),
extensions = mapField.schema.extensions,
schema = schema,
oneOfInterfaces = emptySet(),
)
} else {
Expand All @@ -454,7 +455,7 @@ class ModelGenerator(
modelName: String,
schemaName: String,
properties: Collection<PropertyInfo>,
extensions: Map<String, Any>,
schema: Schema,
oneOfInterfaces: Set<Schema>,
): TypeSpec {
val name = generatedType(packages.base, modelName)
Expand All @@ -466,6 +467,7 @@ class ModelGenerator(
TypeSpec.classBuilder(name)
}
val classBuilder = builder
.addKdoc(schema.toKDoc())
.addSerializableInterface()
.addQuarkusReflectionAnnotation()
.addMicronautIntrospectedAnnotation()
Expand All @@ -487,13 +489,13 @@ class ModelGenerator(
properties.addToClass(
schemaName = schemaName,
classBuilder = classBuilder,
classType = ClassSettings(ClassSettings.PolymorphyType.ONE_OF, extensions),
classType = ClassSettings(ClassSettings.PolymorphyType.ONE_OF, schema.extensions),
)
} else {
properties.addToClass(
schemaName = schemaName,
classBuilder = classBuilder,
classType = ClassSettings(ClassSettings.PolymorphyType.NONE, extensions),
classType = ClassSettings(ClassSettings.PolymorphyType.NONE, schema.extensions),
)
}
}
Expand Down
Loading