Skip to content

Commit

Permalink
Do not leak implementation details in generated Javadoc links
Browse files Browse the repository at this point in the history
Fixes #2803
  • Loading branch information
IgnatBeresnev committed Jan 13, 2023
1 parent ac932d4 commit 45e7e59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
Expand Up @@ -2,14 +2,20 @@ package org.jetbrains.dokka.javadoc.pages

import org.jetbrains.dokka.model.*

internal fun JavadocFunctionNode.getAnchor(): String =
"$name(${parameters.joinToString(",") {
when (val bound = if (it.typeBound is Nullable) it.typeBound.inner else it.typeBound) {
is TypeConstructor -> listOf(bound.dri.packageName, bound.dri.classNames).joinToString(".")
is TypeParameter -> bound.name
is PrimitiveJavaType -> bound.name
is UnresolvedBound -> bound.name
is JavaObject -> "Object"
else -> bound.toString()
}
}})"
internal fun JavadocFunctionNode.getAnchor(): String {
val parameters = parameters.joinToString(",") { it.typeBound.asString() }
return "$name($parameters)"
}

private fun Bound.asString(): String = when (this) {
is Nullable -> this.inner.asString()
is DefinitelyNonNullable -> this.inner.asString()
is TypeConstructor -> listOf(this.dri.packageName, this.dri.classNames).joinToString(".")
is TypeParameter -> this.name
is PrimitiveJavaType -> this.name
is UnresolvedBound -> this.name
is TypeAliased -> this.typeAlias.asString()
is JavaObject -> "Object"
Dynamic -> "dynamic"
Void -> "void"
}
Expand Up @@ -47,6 +47,15 @@ class JavadocLocationTest : BaseAbstractTest() {
| * Referencing element from another package: [javadoc.test.Test]
| */
|class Referenced {}
|
|/jvmSrc/javadoc/test/FunctionParameters.kt
|package javadoc.test.functionparams
|
|typealias FunctionParamTypealias = String
|
|class FunctionParameters {
| fun foo(param: FunctionParamTypealias) {}
|}
""".trimIndent(),
config,
cleanupOutput = false,
Expand Down Expand Up @@ -151,6 +160,25 @@ class JavadocLocationTest : BaseAbstractTest() {
}
}

@Test
fun `should resolve typealias function parameter`() {
locationTestInline { rootPageNode, dokkaContext ->
val transformer = htmlTranslator(rootPageNode, dokkaContext)
val containingClass = rootPageNode
.firstChildOfType<JavadocPackagePageNode> { it.name == "javadoc.test.functionparams" }
.firstChildOfType<JavadocClasslikePageNode> { it.name == "FunctionParameters" }

val methodWithTypealiasParam = containingClass.methods.single { it.name == "foo" }
val methodSignatureHtml = transformer.htmlForContentNode(methodWithTypealiasParam.signature, null)

val expectedSignatureHtml = "final <a href=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html>Unit</a> " +
"<a href=javadoc/test/functionparams/FunctionParameters.html#foo(javadoc.test.functionparams.FunctionParamTypealias)>foo</a>" +
"(<a href=https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>String</a> param)"

assertEquals(expectedSignatureHtml, methodSignatureHtml)
}
}

private fun htmlTranslator(rootPageNode: RootPageNode, dokkaContext: DokkaContext): JavadocContentToHtmlTranslator {
val locationProvider = dokkaContext.plugin<JavadocPlugin>().querySingle { locationProviderFactory }
.getLocationProvider(rootPageNode) as JavadocLocationProvider
Expand Down

0 comments on commit 45e7e59

Please sign in to comment.