Skip to content

Commit

Permalink
GH-193 Rename OpenApiExampleObject to OpenApiExampleProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Aug 24, 2023
1 parent f6d3baa commit 213512f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import io.javalin.openapi.OpenApiContentProperty;
import io.javalin.openapi.OpenApiDescription;
import io.javalin.openapi.OpenApiExample;
import io.javalin.openapi.OpenApiExampleObject;
import io.javalin.openapi.OpenApiExampleProperty;
import io.javalin.openapi.OpenApiIgnore;
import io.javalin.openapi.OpenApiLicense;
import io.javalin.openapi.OpenApiName;
Expand Down Expand Up @@ -331,18 +331,18 @@ public String getFormattedMessage() {

// should contain examples
@OpenApiExample(objects = {
@OpenApiExampleObject(value = "2022-08-14T21:13:03.546Z"),
@OpenApiExampleObject(value = "2022-08-14T21:13:03.546Z")
@OpenApiExampleProperty(value = "2022-08-14T21:13:03.546Z"),
@OpenApiExampleProperty(value = "2022-08-14T21:13:03.546Z")
})
public @NotNull String[] getTimestamps() {
return new String[] { timestamp };
}

// should contain object example
@OpenApiExample(objects = {
@OpenApiExampleObject(name = "Barbie", objects = {
@OpenApiExampleObject(name = "name", value = "Margot Robbie"),
@OpenApiExampleObject(name = "link", value = "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
@OpenApiExampleProperty(name = "Barbie", objects = {
@OpenApiExampleProperty(name = "name", value = "Margot Robbie"),
@OpenApiExampleProperty(name = "link", value = "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
}),
})
public @NotNull Object[] getExampleObjects() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ annotation class OpenApiName(
@Retention(RUNTIME)
annotation class OpenApiExample(
val value: String = NULL_STRING,
val objects: Array<OpenApiExampleObject> = []
val objects: Array<OpenApiExampleProperty> = []
)

@Target(ANNOTATION_CLASS)
@Retention(RUNTIME)
annotation class OpenApiExampleObject(
annotation class OpenApiExampleProperty(
val name: String = NULL_STRING,
val value: String = NULL_STRING,
val objects: Array<OpenApiExampleObject> = []
val objects: Array<OpenApiExampleProperty> = []
)

@Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import io.javalin.openapi.NULL_STRING
import io.javalin.openapi.OpenApiExampleObject
import io.javalin.openapi.OpenApiExampleProperty

object ExampleGenerator {

Expand All @@ -19,7 +19,7 @@ object ExampleGenerator {
}
}

fun generateFromExamples(examples: Array<OpenApiExampleObject>): GeneratorResult {
fun generateFromExamples(examples: Array<OpenApiExampleProperty>): GeneratorResult {
if (examples.isRawList()) {
val jsonArray = JsonArray()
examples.forEach { jsonArray.add(it.value) }
Expand All @@ -35,14 +35,14 @@ object ExampleGenerator {
return GeneratorResult(null, examples.toJsonObject())
}

private fun OpenApiExampleObject.toSimpleExampleValue(): GeneratorResult =
private fun OpenApiExampleProperty.toSimpleExampleValue(): GeneratorResult =
when {
this.value != NULL_STRING -> GeneratorResult(this.value, null)
this.objects.isNotEmpty() -> GeneratorResult(null, objects.toJsonObject())
else -> throw IllegalArgumentException("Example object must have either value or objects ($this)")
}

private fun Array<OpenApiExampleObject>.toJsonObject(): JsonObject {
private fun Array<OpenApiExampleProperty>.toJsonObject(): JsonObject {
val jsonObject = JsonObject()
this.forEach {
val result = it.toSimpleExampleValue()
Expand All @@ -57,10 +57,10 @@ object ExampleGenerator {
return jsonObject
}

private fun Array<OpenApiExampleObject>.isObjectList(): Boolean =
private fun Array<OpenApiExampleProperty>.isObjectList(): Boolean =
this.isNotEmpty() && this.all { it.name == NULL_STRING && it.value == NULL_STRING && it.objects.isNotEmpty() }

private fun Array<OpenApiExampleObject>.isRawList(): Boolean =
private fun Array<OpenApiExampleProperty>.isRawList(): Boolean =
this.isNotEmpty() && this.all { it.name == NULL_STRING && it.value != NULL_STRING && it.objects.isEmpty() }

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.javalin.openapi.Nullability
import io.javalin.openapi.OpenApiByFields
import io.javalin.openapi.OpenApiDescription
import io.javalin.openapi.OpenApiExample
import io.javalin.openapi.OpenApiExampleObject
import io.javalin.openapi.OpenApiIgnore
import io.javalin.openapi.OpenApiName
import io.javalin.openapi.OpenApiPropertyType
Expand Down

0 comments on commit 213512f

Please sign in to comment.