nielsfalk/ktor-swagger


Download


Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}
	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.nielsfalk:ktor-swagger:0.7.0'
	}
	<dependency>
	    <groupId>com.github.nielsfalk</groupId>
	    <artifactId>ktor-swagger</artifactId>
	    <version>0.7.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.nielsfalk" % "ktor-swagger" % "0.7.0"
        
        

                            
    :dependencies [[com.github.nielsfalk/ktor-swagger "0.7.0"]]
        
        

Readme


ktor with swaggerUi

Build Status

This project provides a library that allows you you to integrate the swaggerUi with ktor

An example efrom the ktor-sample-swagger is deployed on heroku.

When using this with Jackson

By default, Jackson includes fields with null values in the JSON output that it generates. This results in swagger.json and openapi.json files that cannot be processed by Swagger UI properly, leading to error messages while parsing the type and format info of parameters. To prevent this, install Jackson content negotiation as follows:

install(ContentNegotiation) {
    jackson {
        setSerializationInclusion(JsonInclude.Include.NON_NULL)
        // (You can add additional Jackson config stuff here, such as registerModules(JavaTimeModule()), etc.)
    }
}

Example Usage

This library adds some extension function that build on the ktor routing feature to provide an API that allows this feature to automatically generate a swagger.json file for your webserver.

routing {
    get<pets>("all".responds(ok<PetsModel>())) {
        call.respond(data)
    }
    post<pets, PetModel>("create".responds(created<PetModel>())) { _, entity ->
        call.respond(Created, entity.copy(id = newId()).apply {
            data.pets.add(this)
        })
    }
    get<pet>("find".responds(ok<PetModel>(), notFound())) { params ->
        data.pets.find { it.id == params.id }
            ?.let {
                call.respond(it)
            }
    }
}

Project Status

This project is a proof of concept built on a library to support this functionality.

There is an open proposal to include this project as an official Ktor feature here.

This project is looking for maintainers. Please open an issue if you're interested in taking over maintaining this project.

Other Similar Projects

Ktor-OpenAPI-Generator