-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathResourceModel.kt
134 lines (118 loc) · 3.5 KB
/
ResourceModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.epages.restdocs.apispec.model
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import java.util.Comparator
data class ResourceModel(
val operationId: String,
val summary: String? = null,
val description: String? = null,
val privateResource: Boolean,
val deprecated: Boolean,
val tags: Set<String> = emptySet(),
val request: RequestModel,
val response: ResponseModel
)
fun List<ResourceModel>.groupByPath(): Map<String, List<ResourceModel>> {
return this.sortedWith(
// by first path segment, then path length, then path
Comparator.comparing<ResourceModel, String> {
it.request.path.split("/").firstOrNull { s -> s.isNotEmpty() }.orEmpty()
}
.thenComparing(Comparator.comparingInt<ResourceModel> { it.request.path.count { c -> c == '/' } })
.thenComparing(Comparator.comparing<ResourceModel, String> { it.request.path })
)
.groupBy { it.request.path }
}
data class Schema @JsonCreator constructor(
val name: String
)
data class RequestModel(
val path: String,
val method: HTTPMethod,
val contentType: String? = null,
val securityRequirements: SecurityRequirements?,
val headers: List<HeaderDescriptor>,
val pathParameters: List<ParameterDescriptor>,
val queryParameters: List<ParameterDescriptor>,
val formParameters: List<ParameterDescriptor>,
val requestFields: List<FieldDescriptor>,
val example: String? = null,
val schema: Schema? = null
)
data class ResponseModel(
val status: Int,
val contentType: String?,
val headers: List<HeaderDescriptor>,
val responseFields: List<FieldDescriptor>,
val example: String? = null,
val schema: Schema? = null
)
enum class SimpleType {
STRING,
INTEGER,
NUMBER,
BOOLEAN
}
interface AbstractParameterDescriptor {
val name: String
val description: String
val type: String
val defaultValue: Any?
val optional: Boolean
val attributes: Attributes
}
data class HeaderDescriptor(
override val name: String,
override val description: String,
override val type: String,
@JsonProperty("default") override val defaultValue: Any? = null,
override val optional: Boolean,
val example: String? = null,
override val attributes: Attributes = Attributes()
) : AbstractParameterDescriptor
open class FieldDescriptor(
val path: String,
val description: String,
val type: String,
val optional: Boolean = false,
val ignored: Boolean = false,
val attributes: Attributes = Attributes()
)
data class Attributes(
val validationConstraints: List<Constraint> = emptyList(),
val enumValues: List<Any> = emptyList(),
val itemsType: String? = null,
val schemaName: String? = null,
)
data class Constraint(
val name: String,
val configuration: Map<String, Any>
)
data class ParameterDescriptor(
override val name: String,
override val description: String,
override val type: String,
@JsonProperty("default") override val defaultValue: Any? = null,
override val optional: Boolean,
val ignored: Boolean,
override val attributes: Attributes = Attributes()
) : AbstractParameterDescriptor
data class SecurityRequirements(
val type: SecurityType,
val requiredScopes: List<String>? = null
)
enum class SecurityType {
OAUTH2,
BASIC,
API_KEY,
JWT_BEARER
}
enum class HTTPMethod {
GET,
POST,
PUT,
DELETE,
PATCH,
HEAD,
OPTIONS
}