-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy and paste for å få på plass github pages
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"/> | ||
<meta name="description" content="Behandlings-API dokumentasjon"/> | ||
<title>Behandling-API dokumentasjon</title> | ||
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css"/> | ||
</head> | ||
<body> | ||
<div id="swagger-ui"></div> | ||
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script> | ||
<script> | ||
window.onload = () => { | ||
window.ui = SwaggerUIBundle({ | ||
url: 'behandling-api.yaml', | ||
dom_id: '#swagger-ui', | ||
}); | ||
}; | ||
</script> | ||
</body> | ||
</html> |
110 changes: 110 additions & 0 deletions
110
openapi/src/main/resources/templates/enum_class.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
{{^multiplatform}} | ||
{{#gson}} | ||
import com.google.gson.annotations.SerializedName | ||
{{/gson}} | ||
{{#moshi}} | ||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
{{/moshi}} | ||
{{#jackson}} | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
{{/jackson}} | ||
{{#kotlinx_serialization}} | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
{{#enumUnknownDefaultCase}} | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializer | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
{{/enumUnknownDefaultCase}} | ||
{{/kotlinx_serialization}} | ||
{{/multiplatform}} | ||
{{#multiplatform}} | ||
import kotlinx.serialization.* | ||
{{/multiplatform}} | ||
|
||
/** | ||
* {{{description}}} | ||
* | ||
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} | ||
*/ | ||
{{#multiplatform}}@Serializable{{/multiplatform}}{{#kotlinx_serialization}}@Serializable{{#enumUnknownDefaultCase}}(with = {{classname}}Serializer::class){{/enumUnknownDefaultCase}}{{/kotlinx_serialization}} | ||
{{^multiplatform}} | ||
{{#moshi}} | ||
@JsonClass(generateAdapter = false) | ||
{{/moshi}} | ||
{{/multiplatform}} | ||
{{#nonPublicApi}}internal {{/nonPublicApi}}enum class {{classname}}(val value: {{{dataType}}}) { | ||
{{#allowableValues}}{{#enumVars}} | ||
{{^multiplatform}} | ||
{{#moshi}} | ||
@Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) | ||
{{/moshi}} | ||
{{#gson}} | ||
@SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) | ||
{{/gson}} | ||
{{#jackson}} | ||
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) | ||
{{/jackson}} | ||
{{#kotlinx_serialization}} | ||
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) | ||
{{/kotlinx_serialization}} | ||
{{/multiplatform}} | ||
{{#multiplatform}} | ||
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) | ||
{{/multiplatform}} | ||
{{#isArray}} | ||
{{#isList}} | ||
{{&name}}(listOf({{{value}}})){{^-last}},{{/-last}}{{#-last}};{{/-last}} | ||
{{/isList}} | ||
{{^isList}} | ||
{{&name}}(arrayOf({{{value}}})){{^-last}},{{/-last}}{{#-last}};{{/-last}} | ||
{{/isList}} | ||
{{/isArray}} | ||
{{^isArray}} | ||
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} | ||
{{/isArray}} | ||
{{/enumVars}}{{/allowableValues}} | ||
/** | ||
* Override [toString()] to avoid using the enum variable name as the value, and instead use | ||
* the actual value defined in the API spec file. | ||
* | ||
* This solves a problem when the variable name and its value are different, and ensures that | ||
* the client sends the correct enum values to the server always. | ||
*/ | ||
override fun toString(): kotlin.String = value{{^isString}}.toString(){{/isString}} | ||
|
||
companion object { | ||
/** | ||
* Converts the provided [data] to a [String] on success, null otherwise. | ||
*/ | ||
fun encode(data: kotlin.Any?): kotlin.String? = if (data is {{classname}}) "$data" else null | ||
|
||
/** | ||
* Returns a valid [{{classname}}] for [data], null otherwise. | ||
*/ | ||
fun decode(data: kotlin.Any?): {{classname}}? = data?.let { | ||
val normalizedData = "$it".lowercase() | ||
values().firstOrNull { value -> | ||
it == value || normalizedData == "$value".lowercase() | ||
} | ||
} | ||
} | ||
}{{#kotlinx_serialization}}{{#enumUnknownDefaultCase}} | ||
|
||
@Serializer(forClass = {{classname}}::class) | ||
internal object {{classname}}Serializer : KSerializer<{{classname}}> { | ||
override val descriptor = {{{dataType}}}.serializer().descriptor | ||
|
||
override fun deserialize(decoder: Decoder): {{classname}} { | ||
val value = decoder.decodeSerializableValue({{{dataType}}}.serializer()) | ||
return {{classname}}.values().firstOrNull { it.value == value } | ||
?: {{classname}}.{{#allowableValues}}{{#enumVars}}{{#-last}}{{&name}}{{/-last}}{{/enumVars}}{{/allowableValues}} | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: {{classname}}) { | ||
encoder.encodeSerializableValue({{{dataType}}}.serializer(), value.value) | ||
} | ||
}{{/enumUnknownDefaultCase}}{{/kotlinx_serialization}} |