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

Keep generated serializer #68

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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
dokka = "1.9.20"
kotlin = "2.0.0"
kotlin = "2.0.20-Beta1"
kover = "0.8.2"
knit = "0.5.0"
spotless="6.25.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.Example.Companion.Serializer as ExampleSerializer
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KeepGeneratedSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
Expand All @@ -14,14 +15,14 @@ import kotlinx.serialization.json.JsonPrimitive
* properties outside the components object.
*/
// TODO, need `KeepGeneratedSerializer` or manually define instance...
@Serializable
@Serializable(Components.Companion.Serializer::class)
@OptIn(InternalSerializationApi::class)
@KeepGeneratedSerializer
public data class Components(
public val schemas: Map<String, ReferenceOr<Schema>> = emptyMap(),
public val responses: Map<String, ReferenceOr<Response>> = emptyMap(),
public val parameters: Map<String, ReferenceOr<Parameter>> = emptyMap(),
public val examples:
Map<String, ReferenceOr<@Serializable(with = ExampleSerializer::class) Example>> =
emptyMap(),
public val examples: Map<String, ReferenceOr<Example>> = emptyMap(),
public val requestBodies: Map<String, ReferenceOr<RequestBody>> = emptyMap(),
public val headers: Map<String, ReferenceOr<Header>> = emptyMap(),
// val securitySchemes: Definitions<SecurityScheme>,
Expand All @@ -38,7 +39,7 @@ public data class Components(
public companion object {
internal object Serializer :
KSerializerWithExtensions<Components>(
serializer(),
generatedSerializer(),
Components::extensions,
{ op, extensions -> op.copy(extensions = extensions) }
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.nomisrev.openapi

import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KeepGeneratedSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
Expand All @@ -8,7 +10,9 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

/** A single encoding definition applied to a single schema property. */
@Serializable
@Serializable(Encoding.Companion.Serializer::class)
@OptIn(InternalSerializationApi::class)
@KeepGeneratedSerializer
public data class Encoding(
/**
* The Content-Type for encoding a specific property. Default value depends on the property type:
Expand Down Expand Up @@ -58,7 +62,7 @@ public data class Encoding(
public companion object {
internal object Serializer :
KSerializerWithExtensions<Encoding>(
serializer(),
generatedSerializer(),
Encoding::extensions,
{ op, extensions -> op.copy(extensions = extensions) }
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.github.nomisrev.openapi

import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KeepGeneratedSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

@Serializable
@Serializable(Example.Companion.Serializer::class)
@OptIn(InternalSerializationApi::class)
@KeepGeneratedSerializer
public data class Example(
/** Short description for the example. */
public val summary: String? = null,
Expand Down Expand Up @@ -37,7 +41,7 @@ public data class Example(
public companion object {
internal object Serializer :
KSerializerWithExtensions<Example>(
serializer(),
generatedSerializer(),
Example::extensions,
{ op, extensions -> op.copy(extensions = extensions) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public data class Header(
public val allowEmptyValue: Boolean? = null,
public val explode: Boolean? = null,
public val example: ExampleValue? = null,
public val examples:
Map<String, ReferenceOr<@Serializable(with = Example.Companion.Serializer::class) Example>>? =
null,
public val examples: Map<String, ReferenceOr<Example>>? = null,
public val schema: ReferenceOr<Schema>? = null
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.Encoding.Companion.Serializer as EncodingSerializer
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KeepGeneratedSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
Expand All @@ -9,7 +10,9 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

/** Each Media Type Object provides schema and examples for the media type identified by its key. */
@Serializable
@Serializable(MediaType.Companion.Serializer::class)
@OptIn(InternalSerializationApi::class)
@KeepGeneratedSerializer
public data class MediaType(
/** The schema defining the content of the request, response, or parameter. */
public val schema: ReferenceOr<Schema>? = null,
Expand All @@ -26,16 +29,13 @@ public data class MediaType(
* if referencing a schema which contains an example, the examples value SHALL override the
* example provided by the schema.
*/
public val examples:
Map<String, ReferenceOr<@Serializable(with = Example.Companion.Serializer::class) Example>> =
emptyMap(),
public val examples: Map<String, ReferenceOr<Example>> = emptyMap(),
/**
* A map between a property name and its encoding information. The key, being the property name,
* MUST exist in the schema as a property. The encoding object SHALL only apply to requestBody
* objects when the media type is multipart or application/x-www-form-urlencoded.
*/
public val encoding: Map<String, @Serializable(with = EncodingSerializer::class) Encoding> =
emptyMap(),
public val encoding: Map<String, Encoding> = emptyMap(),
/**
* Any additional external documentation for this OpenAPI document. The key is the name of the
* extension (beginning with x-), and the value is the data. The value can be a [JsonNull],
Expand All @@ -46,7 +46,7 @@ public data class MediaType(
public companion object {
internal object Serializer :
KSerializerWithExtensions<MediaType>(
serializer(),
generatedSerializer(),
MediaType::extensions,
{ op, extensions -> op.copy(extensions = extensions) }
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.Components.Companion.Serializer as ComponentsSerializer
import kotlin.jvm.JvmStatic
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.EncodeDefault.Mode.ALWAYS
Expand Down Expand Up @@ -37,8 +36,7 @@ public data class OpenAPI(
*/
public val webhooks: Map<String, ReferenceOr<PathItem>> = emptyMap(),
/** An element to hold various schemas for the specification. */
public val components: @Serializable(with = ComponentsSerializer::class) Components =
Components(),
public val components: Components = Components(),
/**
* A declaration of which security mechanisms can be used across the API. The list of values
* includes alternative security requirement objects that can be used. Only one of the security
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.github.nomisrev.openapi

import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KeepGeneratedSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

@Serializable
@Serializable(Operation.Companion.Serializer::class)
@OptIn(InternalSerializationApi::class)
@KeepGeneratedSerializer
public data class Operation(
/**
* A list of tags for API documentation control. Tags can be used for logical grouping of
Expand Down Expand Up @@ -81,7 +81,7 @@ public data class Operation(
public companion object {
internal object Serializer :
KSerializerWithExtensions<Operation>(
serializer(),
generatedSerializer(),
Operation::extensions,
{ op, extensions -> op.copy(extensions = extensions) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public data class Parameter(
* of the _paramExample field. Furthermore, if referencing a schema that contains an example, the
* examples value SHALL override the example provided by the schema.
*/
public val examples:
Map<String, ReferenceOr<@Serializable(with = Example.Companion.Serializer::class) Example>>? =
emptyMap()
public val examples: Map<String, ReferenceOr<Example>>? = emptyMap()
) {
init {
if (input == Input.Path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ public data class PathItem(
*/
public val description: String? = null,
/** A definition of a GET operation on this path. */
public val get: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val get: Operation? = null,
/** A definition of a PUT operation on this path. */
public val put: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val put: Operation? = null,
/** A definition of a POST operation on this path. */
public val post: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val post: Operation? = null,
/** A definition of a DELETE operation on this path. */
public val delete: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val delete: Operation? = null,
/** A definition of a OPTIONS operation on this path. */
public val options: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val options: Operation? = null,
/** A definition of a HEAD operation on this path. */
public val head: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val head: Operation? = null,
/** A definition of a PATCH operation on this path. */
public val patch: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val patch: Operation? = null,
/** A definition of a TRACE operation on this path. */
public val trace: @Serializable(with = Operation.Companion.Serializer::class) Operation? = null,
public val trace: Operation? = null,
/** An alternative server array to service all operations in this path. */
public val servers: List<Server>? = null,
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.MediaType.Companion.Serializer as MediaTypeSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
Expand All @@ -20,7 +19,7 @@ public data class RequestBody(
* describes it. For requests that match multiple keys, only the most specific key is applicable.
* e.g. text/plain overrides text
*/
public val content: Map<String, @Serializable(with = MediaTypeSerializer::class) MediaType>,
public val content: Map<String, MediaType>,
/** Determines if the request body is required in the request. Defaults to false. */
public val required: Boolean = false,
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.MediaType.Companion.Serializer as MediaTypeSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
Expand All @@ -22,8 +21,7 @@ public data class Response(
* type range and the value describes it. For responses that match multiple keys, only the most
* specific key is applicable. i.e. text/plain overrides text
*/
public val content: Map<String, @Serializable(with = MediaTypeSerializer::class) MediaType> =
emptyMap(),
public val content: Map<String, MediaType> = emptyMap(),
/**
* A map of operations links that can be followed from the response. The key of the map is a short
* name for the link, following the naming constraints of the names for Component Objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.serialization.json.JsonPrimitive
public data class Schema(
val title: String? = null,
val description: String? = null,
/** required is an object-level attribute, not a property attribute. */
val required: List<String>? = null,
val nullable: Boolean? = null,
val allOf: List<ReferenceOr<Schema>>? = null,
Expand Down Expand Up @@ -63,6 +64,12 @@ public data class Schema(
@SerialName("\$id") val id: String? = null,
@SerialName("\$anchor") val anchor: String? = null
) {
init {
require(required?.isEmpty() != true) {
"An empty list required: [] is not valid. If all properties are optional, do not specify the required keyword."
}
}

@Serializable
public data class Discriminator(
val propertyName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ data class CollectionConstraint(
}

// TODO `not` is not supported yet
/**
* minProperties and maxProperties let you restrict the number of properties allowed in an object.
* This can be useful when using additionalProperties, or free-form objects.
*/
data class ObjectConstraint(
val minProperties: Int,
val maxProperties: Int,
Expand Down