Skip to content

Commit

Permalink
feat: add support for contact object (#208)
Browse files Browse the repository at this point in the history
* feat: add support for contact object

Closes #88

* docs: add documentation on how to define contacts
  • Loading branch information
mpbalmeida authored Apr 14, 2023
1 parent 26cd0dd commit 2842c43
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ openapi3 {
title = 'My API title'
version = '1.0.1'
format = 'yaml'
contact = {
name = 'John Doe'
email = '[email protected]'
}
separatePublicApi = true
outputFileNamePrefix = 'my-api-spec'
oauth2SecuritySchemeDefinition = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.epages.restdocs.apispec.gradle

import com.epages.restdocs.apispec.model.ResourceModel
import com.epages.restdocs.apispec.openapi3.OpenApi3Generator
import io.swagger.v3.oas.models.info.Contact
import io.swagger.v3.oas.models.servers.Server
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
Expand All @@ -12,9 +13,14 @@ open class OpenApi3Task : OpenApiBaseTask() {
@Optional
var servers: List<Server> = listOf()

@Input
@Optional
var contact: Contact? = null

fun applyExtension(extension: OpenApi3Extension) {
super.applyExtension(extension)
servers = extension.servers
contact = extension.contact
}

override fun generateSpecification(resourceModels: List<ResourceModel>): String {
Expand All @@ -26,7 +32,8 @@ open class OpenApi3Task : OpenApiBaseTask() {
tagDescriptions = tagDescriptions,
version = apiVersion,
oauth2SecuritySchemeDefinition = oauth2SecuritySchemeDefinition,
format = format
format = format,
contact = contact
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.readValue
import groovy.lang.Closure
import io.swagger.v3.oas.models.info.Contact
import io.swagger.v3.oas.models.servers.Server
import org.gradle.api.Project
import java.io.File
Expand Down Expand Up @@ -63,6 +64,7 @@ open class OpenApi3Extension(project: Project) : OpenApiBaseExtension(project) {
override var outputFileNamePrefix = "openapi3"

private var _servers: List<Server> = mutableListOf(Server().apply { url = "http://localhost" })
private var _contact: Contact? = null

val servers
get() = _servers
Expand All @@ -79,6 +81,13 @@ open class OpenApi3Extension(project: Project) : OpenApiBaseExtension(project) {
_servers = serversActions.map { project.configure(Server(), it) as Server }
}

val contact
get() = _contact

fun setContact(contact: Closure<Contact>) {
_contact = project.configure(Contact(), contact) as Contact
}

companion object {
const val name = "openapi3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class RestdocsOpenApi3TaskTest : RestdocsOpenApiTaskTestBase() {
thenSingleServerContainedInOutput()
}

@Test
fun `should run openapi task with contact`() {
givenBuildFileWithOpenApiClosureWithContact()
givenResourceSnippet()

whenPluginExecuted()

thenApiSpecTaskSuccessful()
thenOutputFileFound()
thenContactContainedInOutput()
}

@Test
fun `should run openapi task with single server string`() {
givenBuildFileWithOpenApiClosureWithSingleServerString()
Expand Down Expand Up @@ -67,6 +79,12 @@ class RestdocsOpenApi3TaskTest : RestdocsOpenApiTaskTestBase() {
}
}

private fun thenContactContainedInOutput() {
with(outputFileContext()) {
then(read<String>("info.contact.name")).isEqualTo("Test Contact")
}
}

private fun thenHeaderWithDefaultValuesContainedInOutput() {
with(outputFileContext()) {
then(read<String>("paths./products/{id}.get.parameters[1].name")).isEqualTo("one")
Expand All @@ -86,6 +104,10 @@ class RestdocsOpenApi3TaskTest : RestdocsOpenApiTaskTestBase() {
givenBuildFileWithOpenApiClosure("server", """{ url = 'http://some.api' }""")
}

fun givenBuildFileWithOpenApiClosureWithContact() {
givenBuildFileWithOpenApiClosure("contact", """{ name = 'Test Contact' }""")
}

override fun givenBuildFileWithOpenApiClosure() {
givenBuildFileWithOpenApiClosure(
"servers",
Expand Down Expand Up @@ -136,6 +158,7 @@ class RestdocsOpenApi3TaskTest : RestdocsOpenApiTaskTestBase() {
baseBuildFile() + """
openapi3 {
servers = [ { url = "http://some.api" } ]
contact = { name = "Test Contact" }
title = '$title'
description = '$description'
tagDescriptionsPropertiesFile = "tagDescriptions.yaml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.Paths
import io.swagger.v3.oas.models.examples.Example
import io.swagger.v3.oas.models.headers.Header
import io.swagger.v3.oas.models.info.Contact
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.media.BooleanSchema
import io.swagger.v3.oas.models.media.Content
Expand Down Expand Up @@ -51,7 +52,8 @@ object OpenApi3Generator {
description: String? = null,
tagDescriptions: Map<String, String> = emptyMap(),
version: String = "1.0.0",
oauth2SecuritySchemeDefinition: Oauth2Configuration? = null
oauth2SecuritySchemeDefinition: Oauth2Configuration? = null,
contact: Contact? = null
): OpenAPI {
return OpenAPI().apply {

Expand All @@ -60,6 +62,7 @@ object OpenApi3Generator {
this.title = title
this.description = description
this.version = version
this.contact = contact
}
this.tags(
tagDescriptions.map {
Expand All @@ -86,7 +89,8 @@ object OpenApi3Generator {
tagDescriptions: Map<String, String> = emptyMap(),
version: String = "1.0.0",
oauth2SecuritySchemeDefinition: Oauth2Configuration? = null,
format: String
format: String,
contact: Contact? = null
) =
ApiSpecificationWriter.serialize(
format,
Expand All @@ -97,7 +101,8 @@ object OpenApi3Generator {
description = description,
tagDescriptions = tagDescriptions,
version = version,
oauth2SecuritySchemeDefinition = oauth2SecuritySchemeDefinition
oauth2SecuritySchemeDefinition = oauth2SecuritySchemeDefinition,
contact = contact
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option
import io.swagger.parser.OpenAPIParser
import io.swagger.parser.models.ParseOptions
import io.swagger.v3.oas.models.info.Contact
import io.swagger.v3.oas.models.servers.Server
import org.assertj.core.api.BDDAssertions.then
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -518,6 +519,7 @@ class OpenApi3GeneratorTest {
then(openApiJsonPathContext.read<String>("info.title")).isEqualTo("API")
then(openApiJsonPathContext.read<String>("info.description")).isEqualTo("API Description")
then(openApiJsonPathContext.read<String>("info.version")).isEqualTo("1.0.0")
then(openApiJsonPathContext.read<String>("info.contact.name")).isEqualTo("Test Contact")
}

private fun thenTagFieldsPresent() {
Expand Down Expand Up @@ -578,7 +580,8 @@ class OpenApi3GeneratorTest {
),
format = "json",
description = "API Description",
tagDescriptions = mapOf("tag1" to "tag1 description", "tag2" to "tag2 description")
tagDescriptions = mapOf("tag1" to "tag1 description", "tag2" to "tag2 description"),
contact = Contact().apply { name = "Test Contact" }
)

println(openApiSpecJsonString)
Expand Down

0 comments on commit 2842c43

Please sign in to comment.