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

Typed: add constraints to models #57

Merged
merged 2 commits into from
Jul 10, 2024
Merged
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
4 changes: 0 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ plugins {
val assertId = libs.plugins.assert.get().pluginId
val spotlessId = libs.plugins.spotless.get().pluginId
val publishId = libs.plugins.publish.get().pluginId
val koverId = libs.plugins.kover.get().pluginId


dependencies {
kover(projects.parser)
Expand All @@ -27,8 +25,6 @@ dependencies {
}

subprojects {
apply(plugin = koverId)

apply(plugin = assertId)
@Suppress("OPT_IN_USAGE")
configure<PowerAssertGradleExtension> {
Expand Down
1 change: 1 addition & 0 deletions generation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
alias(libs.plugins.serialization)
id(libs.plugins.publish.get().pluginId)
alias(libs.plugins.dokka)
id(libs.plugins.kover.get().pluginId)
}

kotlin { compilerOptions.freeCompilerArgs.add("-Xcontext-receivers") }
Expand Down
2,000 changes: 0 additions & 2,000 deletions kotlin-js-store/yarn.lock

This file was deleted.

1 change: 1 addition & 0 deletions parser/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
alias(libs.plugins.serialization)
id(libs.plugins.publish.get().pluginId)
alias(libs.plugins.dokka)
id(libs.plugins.kover.get().pluginId)
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public data class Schema(
val deprecated: Boolean? = null,
val maxProperties: Int? = null,
val minProperties: Int? = null,
/**
* Unlike JSON Schema this value MUST conform to the defined type for this parameter. Note: is
* ignored for required parameters.
*/
/** Unlike JSON Schema this value MUST conform to the defined type for this parameter. */
val default: DefaultValue? = null,
val type: Type? = null,
val format: String? = null,
Expand Down
1 change: 1 addition & 0 deletions typed/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
alias(libs.plugins.serialization)
id(libs.plugins.publish.get().pluginId)
alias(libs.plugins.dokka)
id(libs.plugins.kover.get().pluginId)
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.nomisrev.openapi

data class NumberConstraint(
val exclusiveMinimum: Boolean,
val minimum: Double,
val exclusiveMaximum: Boolean,
val maximum: Double,
val multipleOf: Double?
) {
constructor(
schema: Schema
) : this(
schema.exclusiveMinimum ?: false,
schema.minimum ?: Double.NEGATIVE_INFINITY,
schema.exclusiveMaximum ?: false,
schema.maximum ?: Double.POSITIVE_INFINITY,
schema.multipleOf
)
}

data class TextConstraint(val maxLength: Int, val minLength: Int, val pattern: String?) {
constructor(
schema: Schema
) : this(schema.maxLength ?: Int.MAX_VALUE, schema.minLength ?: 0, schema.pattern)
}

data class CollectionConstraint(
val minItems: Int,
val maxItems: Int,
) {
constructor(
schema: Schema
) : this(
schema.minItems ?: 0,
schema.maxItems ?: Int.MAX_VALUE,
)
}

// TODO `not` is not supported yet
data class ObjectConstraint(
val minProperties: Int,
val maxProperties: Int,
) {
constructor(
schema: Schema
) : this(
schema.minProperties ?: 0,
schema.maxProperties ?: Int.MAX_VALUE,
)
}
51 changes: 37 additions & 14 deletions typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.ktor.http.ContentType
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

data class Route(
Expand Down Expand Up @@ -129,16 +128,26 @@
val description: String?

sealed interface Primitive : Model {
data class Int(val default: kotlin.Int?, override val description: kotlin.String?) : Primitive

data class Double(val default: kotlin.Double?, override val description: kotlin.String?) :
Primitive
data class Int(
val default: kotlin.Int?,
override val description: kotlin.String?,
val bounds: NumberConstraint
) : Primitive

data class Double(
val default: kotlin.Double?,
override val description: kotlin.String?,
val bounds: NumberConstraint
) : Primitive

data class Boolean(val default: kotlin.Boolean?, override val description: kotlin.String?) :
Primitive

data class String(val default: kotlin.String?, override val description: kotlin.String?) :
Primitive
data class String(
val default: kotlin.String?,
override val description: kotlin.String?,
val bounds: TextConstraint
) : Primitive

data class Unit(override val description: kotlin.String?) : Primitive

Expand All @@ -150,6 +159,8 @@
is String -> default?.let { "\"$it\"" }
is Unit -> null
}

companion object
}

data class OctetStream(override val description: String?) : Model
Expand All @@ -158,32 +169,40 @@

sealed interface Collection : Model {
val inner: Model
val bounds: CollectionConstraint

data class List(
override val inner: Model,
val default: kotlin.collections.List<String>?,
override val description: String?
override val description: String?,
override val bounds: CollectionConstraint
) : Collection

data class Set(
override val inner: Model,
val default: kotlin.collections.List<String>?,
override val description: String?
override val description: String?,
override val bounds: CollectionConstraint
) : Collection

data class Map(override val inner: Model, override val description: String?) : Collection {
val key = Primitive.String(null, null)
data class Map(
override val inner: Model,
override val description: String?,
override val bounds: CollectionConstraint

Check warning on line 191 in typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Model.kt

View check run for this annotation

Codecov / codecov/patch

typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Model.kt#L188-L191

Added lines #L188 - L191 were not covered by tests
) : Collection {
val key = Primitive.String(null, null, TextConstraint(Int.MAX_VALUE, 0, null))

Check warning on line 193 in typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Model.kt

View check run for this annotation

Codecov / codecov/patch

typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Model.kt#L193

Added line #L193 was not covered by tests
}

companion object
}

@Serializable
data class Object(
val context: NamingContext,
override val description: String?,
val properties: List<Property>,
val inline: List<Model>
val inline: List<Model>,
val constraint: ObjectConstraint?
) : Model {
@Serializable
data class Property(
val baseName: String,
val model: Model,
Expand All @@ -195,6 +214,8 @@
val isNullable: Boolean,
val description: String?
)

companion object
}

data class Union(
Expand Down Expand Up @@ -228,4 +249,6 @@
override val description: String?
) : Enum
}

companion object
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,25 @@
Type.Basic.Array -> collection(context)
Type.Basic.Boolean ->
Primitive.Boolean(default("Boolean", String::toBooleanStrictOrNull), description)
Type.Basic.Integer -> Primitive.Int(default("Integer", String::toIntOrNull), description)
Type.Basic.Integer ->
Primitive.Int(
default("Integer", String::toIntOrNull),
description,
NumberConstraint(this)
)
Type.Basic.Number ->
Primitive.Double(default("Number", String::toDoubleOrNull), description)
Primitive.Double(
default("Number", String::toDoubleOrNull),
description,
NumberConstraint(this)
)
Type.Basic.String ->
if (format == "binary") Model.OctetStream(description)
else
Primitive.String(
default("String", String::toString) { it.joinToString() },
description
description,
TextConstraint(this)
)
Type.Basic.Object -> toObject(context)
Type.Basic.Null -> TODO("Schema.Type.Basic.Null")
Expand Down Expand Up @@ -398,7 +408,8 @@
is Resolved.Value -> NamingContext.Nested(Named(name), context)
}
nestedModel(resolved, pContext)
}
},
ObjectConstraint(this)
)
}

Expand Down Expand Up @@ -429,8 +440,9 @@
}
null -> null
}
return if (uniqueItems == true) Collection.Set(inner.value, default, description)
else Collection.List(inner.value, default, description)
return if (uniqueItems == true)
Collection.Set(inner.value, default, description, CollectionConstraint(this))
else Collection.List(inner.value, default, description, CollectionConstraint(this))
}

fun Schema.toEnum(context: NamingContext, enums: List<String>): Enum.Closed {
Expand Down Expand Up @@ -673,7 +685,14 @@
response.isEmpty() ->
Pair(
statusCode,
Route.ReturnType(Primitive.String(null, response.description), response.extensions)
Route.ReturnType(
Primitive.String(
null,
response.description,
TextConstraint(Int.MAX_VALUE, 0, null)

Check warning on line 692 in typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt

View check run for this annotation

Codecov / codecov/patch

typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt#L688-L692

Added lines #L688 - L692 were not covered by tests
),
response.extensions

Check warning on line 694 in typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt

View check run for this annotation

Codecov / codecov/patch

typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt#L694

Added line #L694 was not covered by tests
)
)
else ->
throw IllegalStateException("OpenAPI requires at least 1 valid response. $response")
Expand Down
Loading