Skip to content

Commit

Permalink
feat: constraints (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
brizzbuzz committed Jan 5, 2023
1 parent e34bea1 commit 377a606
Show file tree
Hide file tree
Showing 21 changed files with 916 additions and 92 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

## Released

## [3.11.0] - January 5th, 2023

### Added

- Support for type constraints.

## [3.10.0] - January 4th, 2023

### Added
Expand Down
31 changes: 28 additions & 3 deletions core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package io.bkbn.kompendium.core

import dev.forst.ktor.apikey.apiKey
import io.bkbn.kompendium.core.fixtures.TestHelpers.openApiTestAllSerializers
import io.bkbn.kompendium.core.util.arrayConstraints
import io.bkbn.kompendium.core.util.complexRequest
import io.bkbn.kompendium.core.util.customAuthConfig
import io.bkbn.kompendium.core.util.customFieldNameResponse
import io.bkbn.kompendium.core.util.dateTimeString
import io.bkbn.kompendium.core.util.defaultAuthConfig
import io.bkbn.kompendium.core.util.defaultField
import io.bkbn.kompendium.core.util.defaultParameter
import io.bkbn.kompendium.core.util.doubleConstraints
import io.bkbn.kompendium.core.util.enrichedComplexGenericType
import io.bkbn.kompendium.core.util.enrichedNestedCollection
import io.bkbn.kompendium.core.util.enrichedSimpleRequest
Expand All @@ -20,6 +22,7 @@ import io.bkbn.kompendium.core.util.genericPolymorphicResponseMultipleImpls
import io.bkbn.kompendium.core.util.gnarlyGenericResponse
import io.bkbn.kompendium.core.util.headerParameter
import io.bkbn.kompendium.core.util.ignoredFieldsResponse
import io.bkbn.kompendium.core.util.intConstraints
import io.bkbn.kompendium.core.util.multipleAuthStrategies
import io.bkbn.kompendium.core.util.multipleExceptions
import io.bkbn.kompendium.core.util.nestedGenericCollection
Expand Down Expand Up @@ -56,6 +59,9 @@ import io.bkbn.kompendium.core.util.simpleGenericResponse
import io.bkbn.kompendium.core.util.simplePathParsing
import io.bkbn.kompendium.core.util.simpleRecursive
import io.bkbn.kompendium.core.util.singleException
import io.bkbn.kompendium.core.util.stringConstraints
import io.bkbn.kompendium.core.util.stringContentEncodingConstraints
import io.bkbn.kompendium.core.util.stringPatternConstraints
import io.bkbn.kompendium.core.util.topLevelNullable
import io.bkbn.kompendium.core.util.trailingSlash
import io.bkbn.kompendium.core.util.unbackedFieldsResponse
Expand Down Expand Up @@ -318,9 +324,6 @@ class KompendiumTest : DescribeSpec({
exception.message should startWith("A route has already been registered for path: /test/{a} and method: GET")
}
}
describe("Constraints") {
// TODO Assess strategies here
}
describe("Formats") {
it("Can set a format for a simple type schema") {
openApiTestAllSerializers(
Expand Down Expand Up @@ -442,4 +445,26 @@ class KompendiumTest : DescribeSpec({
openApiTestAllSerializers("T0057__enriched_complex_generic_type.json") { enrichedComplexGenericType() }
}
}
describe("Constraints") {
it("Can apply constraints to an int field") {
openApiTestAllSerializers("T0059__int_constraints.json") { intConstraints() }
}
it("Can apply constraints to a double field") {
openApiTestAllSerializers("T0060__double_constraints.json") { doubleConstraints() }
}
it("Can apply a min and max length to a string field") {
openApiTestAllSerializers("T0061__string_min_max_constraints.json") { stringConstraints() }
}
it("Can apply a pattern to a string field") {
openApiTestAllSerializers("T0062__string_pattern_constraints.json") { stringPatternConstraints() }
}
it("Can apply a content encoding and media type to a string field") {
openApiTestAllSerializers("T0063__string_content_encoding_constraints.json") {
stringContentEncodingConstraints()
}
}
it("Can apply constraints to an array field") {
openApiTestAllSerializers("T0064__array_constraints.json") { arrayConstraints() }
}
}
})
160 changes: 160 additions & 0 deletions core/src/test/kotlin/io/bkbn/kompendium/core/util/Constraints.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package io.bkbn.kompendium.core.util

import io.bkbn.kompendium.core.fixtures.DoubleResponse
import io.bkbn.kompendium.core.fixtures.Page
import io.bkbn.kompendium.core.fixtures.TestCreatedResponse
import io.bkbn.kompendium.core.fixtures.TestNested
import io.bkbn.kompendium.core.metadata.GetInfo
import io.bkbn.kompendium.core.plugin.NotarizedRoute
import io.bkbn.kompendium.core.util.TestModules.defaultPath
import io.bkbn.kompendium.enrichment.TypeEnrichment
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.install
import io.ktor.server.routing.Routing
import io.ktor.server.routing.route

fun Routing.intConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Get an int")
description("Get an int")
response {
responseCode(HttpStatusCode.OK)
description("An int")
responseType(
enrichment = TypeEnrichment("example") {
TestCreatedResponse::id {
minimum = 2
maximum = 100
multipleOf = 2
}
}
)
responseCode(HttpStatusCode.OK)
}
}
}
}
}

fun Routing.doubleConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Get a double")
description("Get a double")
response {
responseCode(HttpStatusCode.OK)
description("A double")
responseType(
enrichment = TypeEnrichment("example") {
DoubleResponse::payload {
minimum = 2.0
maximum = 100.0
multipleOf = 2.0
}
}
)
responseCode(HttpStatusCode.OK)
}
}
}
}
}

fun Routing.stringConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Get a string")
description("Get a string with constraints")
response {
responseCode(HttpStatusCode.OK)
description("A string")
responseType(
enrichment = TypeEnrichment("example") {
TestNested::nesty {
maxLength = 10
minLength = 2
}
}
)
responseCode(HttpStatusCode.OK)
}
}
}
}
}

fun Routing.stringPatternConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Get a string")
description("This is a description")
response {
responseCode(HttpStatusCode.OK)
description("A string")
responseType(
enrichment = TypeEnrichment("example") {
TestNested::nesty {
pattern = "[a-z]+"
}
}
)
responseCode(HttpStatusCode.OK)
}
}
}
}
}

fun Routing.stringContentEncodingConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Get a string")
description("This is a description")
response {
responseCode(HttpStatusCode.OK)
description("A string")
responseType(
enrichment = TypeEnrichment("example") {
TestNested::nesty {
contentEncoding = "base64"
contentMediaType = "image/png"
}
}
)
responseCode(HttpStatusCode.OK)
}
}
}
}
}

fun Routing.arrayConstraints() {
route(defaultPath) {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Get an array")
description("Get an array of strings")
response {
responseCode(HttpStatusCode.OK)
description("An array")
responseType(
enrichment = TypeEnrichment("example") {
Page<String>::content {
minItems = 2
maxItems = 10
uniqueItems = true
}
}
)
responseCode(HttpStatusCode.OK)
}
}
}
}
}
80 changes: 80 additions & 0 deletions core/src/test/resources/T0059__int_constraints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"openapi": "3.1.0",
"jsonSchemaDialect": "https://json-schema.org/draft/2020-12/schema",
"info": {
"title": "Test API",
"version": "1.33.7",
"description": "An amazing, fully-ish 😉 generated API spec",
"termsOfService": "https://example.com",
"contact": {
"name": "Homer Simpson",
"url": "https://gph.is/1NPUDiM",
"email": "[email protected]"
},
"license": {
"name": "MIT",
"url": "https://github.com/bkbnio/kompendium/blob/main/LICENSE"
}
},
"servers": [
{
"url": "https://myawesomeapi.com",
"description": "Production instance of my API"
},
{
"url": "https://staging.myawesomeapi.com",
"description": "Where the fun stuff happens"
}
],
"paths": {
"/test/{a}": {
"get": {
"tags": [],
"summary": "Get an int",
"description": "Get an int",
"parameters": [],
"responses": {
"200": {
"description": "An int",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestCreatedResponse-example"
}
}
}
}
},
"deprecated": false
},
"parameters": []
}
},
"webhooks": {},
"components": {
"schemas": {
"TestCreatedResponse-example": {
"type": "object",
"properties": {
"c": {
"type": "string"
},
"id": {
"type": "number",
"format": "int32",
"multipleOf": 2,
"maximum": 100,
"minimum": 2
}
},
"required": [
"c",
"id"
]
}
},
"securitySchemes": {}
},
"security": [],
"tags": []
}
Loading

0 comments on commit 377a606

Please sign in to comment.