From 197b3f82a72f2b7b8c522bfecb2ee653c02a92d0 Mon Sep 17 00:00:00 2001 From: Niels Simonides <5821618+nsmnds@users.noreply.github.com> Date: Wed, 4 Dec 2024 21:24:45 +0100 Subject: [PATCH] Support for OpenAPIV2 and OpenAPIV3 language in plugins (#304) Co-authored-by: nsimonides --- .../maven/custom/emit/CustomEmitter.java | 6 +++ .../flock/wirespec/compiler/core/Utils.kt | 4 +- .../compiler/core/emit/JavaEmitter.kt | 5 +-- .../compiler/core/emit/KotlinEmitter.kt | 5 +-- .../compiler/core/emit/ScalaEmitter.kt | 5 +-- .../compiler/core/emit/TypeScriptEmitter.kt | 6 +-- .../compiler/core/emit/WirespecEmitter.kt | 4 +- .../compiler/core/emit/common/Emitter.kt | 6 +-- .../compiler/core/emit/common/Emitters.kt | 8 +++- .../wirespec/compiler/core/parse/Node.kt | 7 +++- .../flock/wirespec/compiler/core/UtilsTest.kt | 13 +++++++ .../wirespec/compiler/core/parse/ParseTest.kt | 12 +++--- .../flock/wirespec/openapi/Common.kt | 5 +++ .../wirespec/openapi/v2/OpenApiV2Emitter.kt | 34 +++++++++++++++-- .../wirespec/openapi/v3/OpenApiV3Emitter.kt | 38 ++++++++++++++++--- .../openapi/v2/OpenApiV2EmitterTest.kt | 2 +- .../openapi/v3/OpenApiV3EmitterTest.kt | 2 +- src/plugin/arguments/build.gradle.kts | 1 + .../flock/wirespec/plugin/CompilerUtil.kt | 6 +++ .../flock/wirespec/plugin/Language.kt | 2 +- .../flock/wirespec/plugin/LanguageTest.kt | 2 +- .../flock/wirespec/plugin/cli/Main.kt | 6 +++ .../maven/src/main/kotlin/CustomMojo.kt | 2 + .../flock/wirespec/plugin/npm/Main.kt | 4 +- 24 files changed, 140 insertions(+), 45 deletions(-) diff --git a/examples/maven-spring-custom/emitter/src/main/java/community/flock/wirespec/example/maven/custom/emit/CustomEmitter.java b/examples/maven-spring-custom/emitter/src/main/java/community/flock/wirespec/example/maven/custom/emit/CustomEmitter.java index cb8bef9b..50512b33 100644 --- a/examples/maven-spring-custom/emitter/src/main/java/community/flock/wirespec/example/maven/custom/emit/CustomEmitter.java +++ b/examples/maven-spring-custom/emitter/src/main/java/community/flock/wirespec/example/maven/custom/emit/CustomEmitter.java @@ -25,6 +25,12 @@ public CustomEmitter(@NotNull Logger logger, boolean split) { super(logger, split); } + @NotNull + @Override + public String getSingleLineComment() { + return "//"; + } + @NotNull @Override public String emitName(@NotNull Definition definition) { diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/Utils.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/Utils.kt index bab62e2e..11674374 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/Utils.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/Utils.kt @@ -16,4 +16,6 @@ fun String.removeJavaPrefix() = removePrefix("java.util.") fun String.removeAngularBrackets() = filterNot { it == '<' || it == '>' } -fun String.removeCommasAndSpaces() = filterNot { it == ',' || it == ' '} +fun String.removeCommasAndSpaces() = filterNot { it == ',' || it == ' ' } + +fun String.removeCommentMarkers(): String = removePrefix("/*").removeSuffix("*/").trim() diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt index 4d2d3306..9b56f5c3 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt @@ -45,10 +45,7 @@ open class JavaEmitter( is Union -> emit(identifier) } - override fun notYetImplemented() = - """// TODO("Not yet implemented") - | - """.trimMargin() + override val singleLineComment = "//" override fun emit(ast: AST): List = super.emit(ast).map { (typeName, result) -> diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt index e74219c3..2eb6dd44 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/KotlinEmitter.kt @@ -47,10 +47,7 @@ open class KotlinEmitter( is Union -> emit(identifier) } - override fun notYetImplemented() = - """// TODO("Not yet implemented") - | - """.trimMargin() + override val singleLineComment = "//" override fun emit(ast: AST): List = super.emit(ast).map { (typeName, result) -> diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt index e4e5edd9..2095c67f 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/ScalaEmitter.kt @@ -42,10 +42,7 @@ open class ScalaEmitter( is Union -> emit(identifier) } - override fun notYetImplemented() = - """// TODO("Not yet implemented") - | - """.trimMargin() + override val singleLineComment = "//" override fun emit(ast: AST): List = super.emit(ast).map { (typeName, result) -> diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/TypeScriptEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/TypeScriptEmitter.kt index 6863d524..06b8a501 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/TypeScriptEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/TypeScriptEmitter.kt @@ -3,7 +3,6 @@ package community.flock.wirespec.compiler.core.emit import community.flock.wirespec.compiler.core.emit.common.DefinitionModelEmitter import community.flock.wirespec.compiler.core.emit.common.Emitted import community.flock.wirespec.compiler.core.emit.common.Emitter -import community.flock.wirespec.compiler.core.emit.common.Emitter.Companion.firstToLower import community.flock.wirespec.compiler.core.emit.common.Spacer import community.flock.wirespec.compiler.core.emit.shared.TypeScriptShared import community.flock.wirespec.compiler.core.parse.AST @@ -31,10 +30,7 @@ open class TypeScriptEmitter(logger: Logger = noLogger) : DefinitionModelEmitter is Channel -> emit(identifier) } - override fun notYetImplemented() = - """// TODO("Not yet implemented") - | - """.trimMargin() + override val singleLineComment = "//" override fun emit(ast: AST): List = super.emit(ast).map { diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt index 2832c26c..b0fb482c 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/WirespecEmitter.kt @@ -30,7 +30,9 @@ open class WirespecEmitter(logger: Logger = noLogger) : DefinitionModelEmitter, is Channel -> emit(identifier) } - override fun notYetImplemented() = "\n" + override val singleLineComment = "\n" + + override fun notYetImplemented() = singleLineComment override fun emit(type: Type, ast: AST) = """ |type ${emit(type.identifier)} { diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitter.kt index 37321981..3d37b63d 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitter.kt @@ -17,7 +17,7 @@ import community.flock.wirespec.compiler.utils.Logger abstract class Emitter( val logger: Logger, - val split: Boolean = false + val split: Boolean = false, ) : Emitters { data class Param( @@ -31,9 +31,7 @@ abstract class Emitter( } } - abstract fun Definition.emitName(): String - - abstract fun notYetImplemented(): String + open fun Definition.emitName(): String = notYetImplemented() open fun emit(ast: AST): List = ast .map { diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitters.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitters.kt index dd4f5200..0233aea3 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitters.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/common/Emitters.kt @@ -16,7 +16,8 @@ interface Emitters : EndpointDefinitionEmitter, UnionDefinitionEmitter, IdentifierEmitter, - ChannelDefinitionEmitter + ChannelDefinitionEmitter, + NotYetImplemented interface TypeDefinitionEmitter { fun emit(type: Type, ast: AST): String @@ -45,3 +46,8 @@ interface ChannelDefinitionEmitter { interface IdentifierEmitter { fun emit(identifier: Identifier): String } + +interface NotYetImplemented { + val singleLineComment: String + fun notYetImplemented() = "$singleLineComment TODO(\"Not yet implemented\")\n" +} diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/Node.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/Node.kt index 94b386bc..6b5e2e33 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/Node.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/parse/Node.kt @@ -2,6 +2,7 @@ package community.flock.wirespec.compiler.core.parse import community.flock.wirespec.compiler.core.Value import community.flock.wirespec.compiler.core.removeBackticks +import community.flock.wirespec.compiler.core.removeCommentMarkers import kotlin.jvm.JvmInline sealed interface Node @@ -112,7 +113,11 @@ data class Channel( ) : Definition @JvmInline -value class Comment(override val value: String) : Value +value class Comment private constructor(override val value: String) : Value { + companion object { + operator fun invoke(comment: String) = Comment(comment.removeCommentMarkers()) + } +} sealed class Identifier(name: String) : Value { override val value = name.removeBackticks() diff --git a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/UtilsTest.kt b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/UtilsTest.kt index bd9e08ea..ae7ee050 100644 --- a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/UtilsTest.kt +++ b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/UtilsTest.kt @@ -33,4 +33,17 @@ class UtilsTest { "java.util.List".concatGenerics() shouldBe "ListString" } + @Test + fun testRemoveCommentMarkers() { + "/* Simple comment */".removeCommentMarkers() shouldBe "Simple comment" + "/* Padded */".removeCommentMarkers() shouldBe "Padded" + "/**/".removeCommentMarkers() shouldBe "" + + """/* Multiple + | Lines + | Here */""".trimMargin().removeCommentMarkers() shouldBe """Multiple + | Lines + | Here""".trimMargin() + } + } diff --git a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTest.kt b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTest.kt index 0301c681..59b3c0a7 100644 --- a/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTest.kt +++ b/src/compiler/core/src/commonTest/kotlin/community/flock/wirespec/compiler/core/parse/ParseTest.kt @@ -84,15 +84,15 @@ class ParseTest { @Test fun testParserWithDoubleComment() { val source = """ - |/** - | * This is comment 1 + |/* + | This is comment 1 | */ |type Foo { | foo: String |} | - |/** - | * This is comment 2 + |/* + | This is comment 2 | */ |type Bar { | bar: String @@ -102,8 +102,8 @@ class ParseTest { WirespecSpec.tokenize(source) .let(parser()::parse) .shouldBeRight().filterIsInstance().map { it.comment?.value } shouldBe listOf( - "/**\n * This is comment 1\n */", - "/**\n * This is comment 2\n */", + "This is comment 1", + "This is comment 2", ) } } diff --git a/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/Common.kt b/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/Common.kt index 7c1a30ad..cdaf8ebf 100644 --- a/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/Common.kt +++ b/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/Common.kt @@ -1,6 +1,7 @@ package community.flock.wirespec.openapi import community.flock.wirespec.compiler.core.emit.common.Emitter.Companion.firstToUpper +import kotlinx.serialization.json.Json object Common { fun className(vararg arg: String) = arg @@ -9,4 +10,8 @@ object Common { fun Map.filterNotNullValues(): Map = mapNotNull { (key, value) -> value?.let { key to it } }.toMap() + + val json = Json { + prettyPrint = true + } } diff --git a/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2Emitter.kt b/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2Emitter.kt index 524afffb..686d8242 100644 --- a/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2Emitter.kt +++ b/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2Emitter.kt @@ -14,19 +14,46 @@ import community.flock.kotlinx.openapi.bindings.v2.SchemaObject import community.flock.kotlinx.openapi.bindings.v2.SchemaOrReferenceObject import community.flock.kotlinx.openapi.bindings.v2.StatusCode import community.flock.kotlinx.openapi.bindings.v2.SwaggerObject +import community.flock.wirespec.compiler.core.emit.common.Emitted +import community.flock.wirespec.compiler.core.emit.common.Emitter import community.flock.wirespec.compiler.core.parse.AST +import community.flock.wirespec.compiler.core.parse.Channel import community.flock.wirespec.compiler.core.parse.Endpoint import community.flock.wirespec.compiler.core.parse.Enum import community.flock.wirespec.compiler.core.parse.Field +import community.flock.wirespec.compiler.core.parse.Identifier import community.flock.wirespec.compiler.core.parse.Reference import community.flock.wirespec.compiler.core.parse.Refined import community.flock.wirespec.compiler.core.parse.Type +import community.flock.wirespec.compiler.core.parse.Union +import community.flock.wirespec.compiler.utils.noLogger +import community.flock.wirespec.openapi.Common.json +import kotlinx.serialization.encodeToString import kotlinx.serialization.json.JsonPrimitive import community.flock.kotlinx.openapi.bindings.v2.Type as OpenApiType -object OpenApiV2Emitter { +object OpenApiV2Emitter: Emitter(noLogger) { - fun emit(ast: AST): SwaggerObject = + override val singleLineComment = "" + + override fun emit(ast: AST): List = + listOf(Emitted("SwaggerObject", json.encodeToString(emitSwaggerObject(ast)))) + + override fun emit(type: Type, ast: AST) = notYetImplemented() + + override fun emit(enum: Enum) = notYetImplemented() + + override fun emit(refined: Refined) = notYetImplemented() + + override fun emit(endpoint: Endpoint) = notYetImplemented() + + override fun emit(union: Union) = notYetImplemented() + + override fun emit(identifier: Identifier) = notYetImplemented() + + override fun emit(channel: Channel) = notYetImplemented() + + fun emitSwaggerObject(ast: AST): SwaggerObject = SwaggerObject( swagger = "2.0", info = InfoObject( @@ -83,6 +110,7 @@ object OpenApiV2Emitter { private fun Endpoint.emit() = OperationObject( operationId = identifier.value, + description = comment?.value, consumes = requests.mapNotNull { it.content?.type }.distinct().ifEmpty { null }, produces = responses.mapNotNull { it.content?.type }.distinct().ifEmpty { null }, parameters = requests @@ -103,7 +131,7 @@ object OpenApiV2Emitter { responses = responses .associate { response -> StatusCode(response.status) to ResponseObject( - description = "$identifier ${response.status} response", + description = comment?.value ?: "${identifier.value} ${response.status} response", headers = response.headers.associate { it.identifier.value to HeaderObject( type = it.reference.value, diff --git a/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3Emitter.kt b/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3Emitter.kt index 8e916877..e1c0c75d 100644 --- a/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3Emitter.kt +++ b/src/converter/openapi/src/commonMain/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3Emitter.kt @@ -19,26 +19,51 @@ import community.flock.kotlinx.openapi.bindings.v3.ResponseObject import community.flock.kotlinx.openapi.bindings.v3.SchemaObject import community.flock.kotlinx.openapi.bindings.v3.SchemaOrReferenceObject import community.flock.kotlinx.openapi.bindings.v3.StatusCode +import community.flock.wirespec.compiler.core.emit.common.Emitted +import community.flock.wirespec.compiler.core.emit.common.Emitter import community.flock.wirespec.compiler.core.parse.AST import community.flock.wirespec.compiler.core.parse.Channel import community.flock.wirespec.compiler.core.parse.Definition import community.flock.wirespec.compiler.core.parse.Endpoint import community.flock.wirespec.compiler.core.parse.Enum import community.flock.wirespec.compiler.core.parse.Field +import community.flock.wirespec.compiler.core.parse.Identifier import community.flock.wirespec.compiler.core.parse.Reference import community.flock.wirespec.compiler.core.parse.Refined import community.flock.wirespec.compiler.core.parse.Type import community.flock.wirespec.compiler.core.parse.Union +import community.flock.wirespec.compiler.utils.noLogger +import community.flock.wirespec.openapi.Common.json +import kotlinx.serialization.encodeToString import kotlinx.serialization.json.JsonPrimitive import community.flock.kotlinx.openapi.bindings.v3.Type as OpenApiType -object OpenApiV3Emitter { +object OpenApiV3Emitter : Emitter(noLogger) { data class Options( val title: String, - val version: String + val version: String, ) - fun emit(ast: AST, options: Options? = null) = OpenAPIObject( + override val singleLineComment = "" + + override fun emit(ast: AST): List = + listOf(Emitted("OpenAPIObject", json.encodeToString(emitOpenAPIObject(ast, null)))) + + override fun emit(type: Type, ast: AST) = notYetImplemented() + + override fun emit(enum: Enum) = notYetImplemented() + + override fun emit(refined: Refined) = notYetImplemented() + + override fun emit(endpoint: Endpoint) = notYetImplemented() + + override fun emit(union: Union) = notYetImplemented() + + override fun emit(identifier: Identifier) = notYetImplemented() + + override fun emit(channel: Channel) = notYetImplemented() + + fun emitOpenAPIObject(ast: AST, options: Options? = null) = OpenAPIObject( openapi = "3.0.0", info = InfoObject( title = options?.title ?: "Wirespec", @@ -91,7 +116,7 @@ object OpenApiV3Emitter { private fun Type.emit(): SchemaObject = SchemaObject( - + description = comment?.value, properties = shape.value.associate { it.emitSchema() }, required = shape.value .filter { !it.isNullable } @@ -101,12 +126,14 @@ object OpenApiV3Emitter { private fun Enum.emit(): SchemaObject = SchemaObject( + description = comment?.value, type = OpenApiType.STRING, enum = entries.map { JsonPrimitive(it) } ) private fun Union.emit(): SchemaObject = SchemaObject( + description = comment?.value, type = OpenApiType.STRING, oneOf = entries.map { it.emitSchema() } ) @@ -116,6 +143,7 @@ object OpenApiV3Emitter { private fun Endpoint.emit(): OperationObject = OperationObject( operationId = identifier.value, + description = comment?.value, parameters = path.filterIsInstance() .map { it.emitParameter() } + queries.map { it.emitParameter(ParameterLocation.QUERY) } + headers.map { it.emitParameter( @@ -131,7 +159,7 @@ object OpenApiV3Emitter { .map { (statusCode, res) -> StatusCode(statusCode) to ResponseObject( headers = res.flatMap { it.headers }.associate { it.emitHeader() }, - description = "$identifier $statusCode response", + description = "${identifier.value} $statusCode response", content = res .mapNotNull { it.content } .associate { it.emit() } diff --git a/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2EmitterTest.kt b/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2EmitterTest.kt index 524ffac0..faea2d85 100644 --- a/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2EmitterTest.kt +++ b/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v2/OpenApiV2EmitterTest.kt @@ -19,7 +19,7 @@ class OpenApiV2EmitterTest { val petstoreOpenAPi = OpenAPI.decodeFromString(petstoreJson) val petstoreAst = petstoreOpenAPi.parse() - val petstoreConvertedOpenApi = OpenApiV2Emitter.emit(petstoreAst) + val petstoreConvertedOpenApi = OpenApiV2Emitter.emitSwaggerObject(petstoreAst) val petstoreConvertedOpenAPiAst = petstoreConvertedOpenApi.parse() assertEquals( diff --git a/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3EmitterTest.kt b/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3EmitterTest.kt index 87e05910..db2d1bb9 100644 --- a/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3EmitterTest.kt +++ b/src/converter/openapi/src/commonTest/kotlin/community/flock/wirespec/openapi/v3/OpenApiV3EmitterTest.kt @@ -18,7 +18,7 @@ class OpenApiV3EmitterTest { val petstoreOpenAPi = OpenAPI.decodeFromString(petstoreJson) val petstoreAst = petstoreOpenAPi.parse() - val petstoreConvertedOpenAPi = OpenApiV3Emitter.emit(petstoreAst) + val petstoreConvertedOpenAPi = OpenApiV3Emitter.emitOpenAPIObject(petstoreAst, null) val petstoreConvertedOpenAPiAst = petstoreConvertedOpenAPi.parse() assertEquals(petstoreAst, petstoreConvertedOpenAPiAst) diff --git a/src/plugin/arguments/build.gradle.kts b/src/plugin/arguments/build.gradle.kts index 88ad4c2b..e0620953 100644 --- a/src/plugin/arguments/build.gradle.kts +++ b/src/plugin/arguments/build.gradle.kts @@ -24,6 +24,7 @@ kotlin { val commonMain by getting { dependencies { implementation(project(":src:compiler:core")) + implementation(project(":src:converter:openapi")) } } val commonTest by getting { diff --git a/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/CompilerUtil.kt b/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/CompilerUtil.kt index a77af3d2..12525ede 100644 --- a/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/CompilerUtil.kt +++ b/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/CompilerUtil.kt @@ -17,8 +17,12 @@ import community.flock.wirespec.compiler.core.parse import community.flock.wirespec.compiler.core.parse.Node import community.flock.wirespec.compiler.core.validate.validate import community.flock.wirespec.compiler.utils.Logger +import community.flock.wirespec.openapi.v2.OpenApiV2Emitter +import community.flock.wirespec.openapi.v3.OpenApiV3Emitter import community.flock.wirespec.plugin.Language.Java import community.flock.wirespec.plugin.Language.Kotlin +import community.flock.wirespec.plugin.Language.OpenAPIV2 +import community.flock.wirespec.plugin.Language.OpenAPIV3 import community.flock.wirespec.plugin.Language.Scala import community.flock.wirespec.plugin.Language.TypeScript import community.flock.wirespec.plugin.Language.Wirespec @@ -51,4 +55,6 @@ fun Language.mapEmitter(packageName: PackageName, logger: Logger) = Scala -> LanguageEmitter(ScalaEmitter(packageName.value, logger), FileExtension.Scala, ScalaShared) TypeScript -> LanguageEmitter(TypeScriptEmitter(logger), FileExtension.TypeScript) Wirespec -> LanguageEmitter(WirespecEmitter(logger), FileExtension.Wirespec) + OpenAPIV2 -> LanguageEmitter(OpenApiV2Emitter, FileExtension.Json) + OpenAPIV3 -> LanguageEmitter(OpenApiV3Emitter, FileExtension.Json) } diff --git a/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/Language.kt b/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/Language.kt index f68f84a7..3260f2f8 100644 --- a/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/Language.kt +++ b/src/plugin/arguments/src/commonMain/kotlin/community/flock/wirespec/plugin/Language.kt @@ -3,7 +3,7 @@ package community.flock.wirespec.plugin import community.flock.wirespec.compiler.core.Value enum class Language { - Java, Kotlin, Scala, TypeScript, Wirespec; + Java, Kotlin, Scala, TypeScript, Wirespec, OpenAPIV2, OpenAPIV3; companion object { fun toMap() = entries.associateBy { it.name } diff --git a/src/plugin/arguments/src/commonTest/kotlin/community/flock/wirespec/plugin/LanguageTest.kt b/src/plugin/arguments/src/commonTest/kotlin/community/flock/wirespec/plugin/LanguageTest.kt index 7ca68dda..c119813f 100644 --- a/src/plugin/arguments/src/commonTest/kotlin/community/flock/wirespec/plugin/LanguageTest.kt +++ b/src/plugin/arguments/src/commonTest/kotlin/community/flock/wirespec/plugin/LanguageTest.kt @@ -6,6 +6,6 @@ import kotlin.test.Test class LanguageTest { @Test fun testLanguages() { - Language.toString() shouldBe "Java, Kotlin, Scala, TypeScript, Wirespec" + Language.toString() shouldBe "Java, Kotlin, Scala, TypeScript, Wirespec, OpenAPIV2, OpenAPIV3" } } diff --git a/src/plugin/cli/src/commonMain/kotlin/community/flock/wirespec/plugin/cli/Main.kt b/src/plugin/cli/src/commonMain/kotlin/community/flock/wirespec/plugin/cli/Main.kt index 5069174c..48a1ed0a 100644 --- a/src/plugin/cli/src/commonMain/kotlin/community/flock/wirespec/plugin/cli/Main.kt +++ b/src/plugin/cli/src/commonMain/kotlin/community/flock/wirespec/plugin/cli/Main.kt @@ -13,6 +13,8 @@ import community.flock.wirespec.compiler.core.emit.common.Emitted import community.flock.wirespec.compiler.core.emit.common.Emitter import community.flock.wirespec.compiler.core.emit.common.Emitter.Companion.firstToUpper import community.flock.wirespec.compiler.utils.Logger +import community.flock.wirespec.openapi.v2.OpenApiV2Emitter +import community.flock.wirespec.openapi.v3.OpenApiV3Emitter import community.flock.wirespec.plugin.CompilerArguments import community.flock.wirespec.plugin.Console import community.flock.wirespec.plugin.FileExtension @@ -23,6 +25,8 @@ import community.flock.wirespec.plugin.FullFilePath import community.flock.wirespec.plugin.Language import community.flock.wirespec.plugin.Language.Java import community.flock.wirespec.plugin.Language.Kotlin +import community.flock.wirespec.plugin.Language.OpenAPIV2 +import community.flock.wirespec.plugin.Language.OpenAPIV3 import community.flock.wirespec.plugin.Language.Scala import community.flock.wirespec.plugin.Language.TypeScript import community.flock.wirespec.plugin.Language.Wirespec @@ -140,6 +144,8 @@ private fun Set.emitters(packageName: PackageName, path: ((FileExtensi Scala -> ScalaEmitter(packageString, logger) to path?.let { ScalaFile(it(FileExtension.Scala)) } TypeScript -> TypeScriptEmitter(logger) to path?.let { TypeScriptFile(it(FileExtension.TypeScript)) } Wirespec -> WirespecEmitter(logger) to path?.let { WirespecFile(it(FileExtension.Wirespec)) } + OpenAPIV2 -> OpenApiV2Emitter to path?.let { JsonFile(it(FileExtension.Json)) } + OpenAPIV3 -> OpenApiV3Emitter to path?.let { JsonFile(it(FileExtension.Json)) } } } diff --git a/src/plugin/maven/src/main/kotlin/CustomMojo.kt b/src/plugin/maven/src/main/kotlin/CustomMojo.kt index c3df1057..22f90843 100644 --- a/src/plugin/maven/src/main/kotlin/CustomMojo.kt +++ b/src/plugin/maven/src/main/kotlin/CustomMojo.kt @@ -64,6 +64,8 @@ class CustomMojo : BaseMojo() { Language.Scala -> ScalaShared Language.TypeScript -> TypeScriptShared Language.Wirespec -> null + Language.OpenAPIV2 -> null + Language.OpenAPIV3 -> null } }?.also { File(output).resolve(PackageName(it.packageString).toDirectory()).apply { diff --git a/src/plugin/npm/src/jsMain/kotlin/community/flock/wirespec/plugin/npm/Main.kt b/src/plugin/npm/src/jsMain/kotlin/community/flock/wirespec/plugin/npm/Main.kt index 02f65004..761e69db 100644 --- a/src/plugin/npm/src/jsMain/kotlin/community/flock/wirespec/plugin/npm/Main.kt +++ b/src/plugin/npm/src/jsMain/kotlin/community/flock/wirespec/plugin/npm/Main.kt @@ -84,12 +84,12 @@ fun emit(ast: Array, emitter: Emitters, packageName: String) = ast Emitters.KOTLIN -> KotlinEmitter(packageName).emit(it) Emitters.SCALA -> ScalaEmitter(packageName).emit(it) Emitters.OPENAPI_V2 -> listOf(it) - .map(OpenApiV2Emitter::emit) + .map(OpenApiV2Emitter::emitSwaggerObject) .map(encode(SwaggerObject.serializer())) .map(::Emitted.curried()("openapi")::invoke) Emitters.OPENAPI_V3 -> listOf(it) - .map(OpenApiV3Emitter::emit) + .map{ast -> OpenApiV3Emitter.emitOpenAPIObject(ast, null)} .map(encode(OpenAPIObject.serializer())) .map(::Emitted.curried()("openapi")::invoke) }