Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.restdocs.headers.RequestHeadersSnippet
import org.springframework.restdocs.headers.ResponseHeadersSnippet
import org.springframework.restdocs.hypermedia.HypermediaDocumentation
import org.springframework.restdocs.hypermedia.LinkDescriptor
import org.springframework.restdocs.hypermedia.LinkExtractor
import org.springframework.restdocs.hypermedia.LinksSnippet
import org.springframework.restdocs.operation.Operation
import org.springframework.restdocs.payload.FieldDescriptor
Expand All @@ -29,7 +30,7 @@ internal object DescriptorValidator {
validateIfDescriptorsPresent(
links,
operation
) { LinksSnippetWrapper(links) }
) { LinksSnippetWrapper(links, linkExtractor) }

validateIfDescriptorsPresent(
responseFieldsWithLinks,
Expand Down Expand Up @@ -183,8 +184,8 @@ internal object DescriptorValidator {
}
}

private class LinksSnippetWrapper(descriptors: List<LinkDescriptor>) :
LinksSnippet(HypermediaDocumentation.halLinks(), descriptors),
private class LinksSnippetWrapper(descriptors: List<LinkDescriptor>, linkExtractor: LinkExtractor) :
LinksSnippet(linkExtractor, descriptors),
ValidateableSnippet {
override fun validate(operation: Operation) {
this.createModel(operation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.epages.restdocs.apispec

import com.epages.restdocs.apispec.SimpleType.STRING
import org.springframework.restdocs.headers.HeaderDescriptor
import org.springframework.restdocs.hypermedia.HypermediaDocumentation
import org.springframework.restdocs.hypermedia.LinkDescriptor
import org.springframework.restdocs.hypermedia.LinkExtractor
import org.springframework.restdocs.payload.FieldDescriptor
import org.springframework.restdocs.payload.JsonFieldType
import org.springframework.restdocs.payload.PayloadDocumentation
Expand All @@ -28,9 +30,16 @@ data class ResourceSnippetParameters @JvmOverloads constructor(
val requestParameters: List<ParameterDescriptorWithType> = emptyList(),
val requestHeaders: List<HeaderDescriptorWithType> = emptyList(),
val responseHeaders: List<HeaderDescriptorWithType> = emptyList(),
val tags: Set<String> = emptySet()
val tags: Set<String> = emptySet(),
val linkExtractor: LinkExtractor = HypermediaDocumentation.halLinks()
) {
val responseFieldsWithLinks by lazy { responseFields + links.map(Companion::toFieldDescriptor) }
val responseFieldsWithLinks by lazy {
if (linkExtractor::class == HypermediaDocumentation.halLinks()::class) {
responseFields + links.map(Companion::toFieldDescriptor)
} else {
responseFields
}
}

companion object {
@JvmStatic
Expand Down Expand Up @@ -65,7 +74,7 @@ data class ResourceSnippetParameters @JvmOverloads constructor(
* @return
*/
private fun createLinkFieldDescriptor(rel: String): FieldDescriptor {
val path = "_links.$rel"
val path = "_links.$rel" // HAL specific
return Optional.ofNullable(
ReflectionUtils.findMethod(
PayloadDocumentation::class.java,
Expand Down Expand Up @@ -193,6 +202,8 @@ class ResourceSnippetParametersBuilder : ResourceSnippetDetails() {
private set
var responseHeaders: List<HeaderDescriptorWithType> = emptyList()
private set
var linkExtractor: LinkExtractor = HypermediaDocumentation.halLinks()
private set

override fun summary(summary: String?) = apply { this.summary = summary }
override fun description(description: String?) = apply { this.description = description }
Expand Down Expand Up @@ -246,6 +257,8 @@ class ResourceSnippetParametersBuilder : ResourceSnippetDetails() {
override fun tag(tag: String) = tags(tag)
override fun tags(vararg tags: String) = apply { this.tags += tags }

fun linkExtractor(linkExtractor: LinkExtractor) = apply { this.linkExtractor = linkExtractor }

fun build() = ResourceSnippetParameters(
summary,
description,
Expand All @@ -260,6 +273,7 @@ class ResourceSnippetParametersBuilder : ResourceSnippetDetails() {
requestParameters,
requestHeaders,
responseHeaders,
tags
tags,
linkExtractor
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import org.springframework.http.HttpStatus.OK
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.restdocs.generate.RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE
import org.springframework.restdocs.headers.HeaderDocumentation
import org.springframework.restdocs.hypermedia.HypermediaDocumentation
import org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel
import org.springframework.restdocs.operation.Operation
import org.springframework.restdocs.payload.JsonFieldType
import org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath
import org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath
import java.io.File
import java.io.IOException
import java.nio.file.Path
Expand Down Expand Up @@ -182,6 +185,17 @@ class ResourceSnippetTest {
then(resourceSnippetJson.read<String>("response.contentType")).isEqualTo("application/json;format=format-1")
}

@Test
fun should_generate_resourcemodel_for_links_not_in_HAL_format() {
givenOperationWithAtomLinksInResponse()
givenLinkDescriptorAndAtomLinkExtractor()

whenResourceSnippetInvoked()

thenSnippetFileExists()
then(resourceSnippetJson.read<List<*>>("response.responseFields")).hasSize(1)
}

private fun givenTag() {
parametersBuilder.tag("some")
parametersBuilder.tags("someOther", "somethingElse")
Expand Down Expand Up @@ -377,6 +391,40 @@ class ResourceSnippetTest {
operation = operationBuilder.build()
}

private fun givenOperationWithAtomLinksInResponse() {
val operationBuilder = OperationBuilder("test", rootOutputDirectory)
val responseContent = """
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/some/123"
}
]
}
""".trimIndent()

operationBuilder
.attribute(ATTRIBUTE_NAME_URL_TEMPLATE, "http://localhost:8080/some/{id}")
.request("http://localhost:8080/some/123")
.method("GET")

operationBuilder
.response()
.status(200)
.content(responseContent)

operation = operationBuilder.build()
}

private fun givenLinkDescriptorAndAtomLinkExtractor() {
parametersBuilder
.links(
linkWithRel("self").description("Link to this resource")
)
.linkExtractor(HypermediaDocumentation.atomLinks())
}

@Throws(IOException::class)
private fun whenResourceSnippetInvoked() {
resource(
Expand Down