From 5fce37f26904fdbc87896cc21ed9add8a9a4b25a Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 17 Mar 2021 00:28:01 +0100 Subject: [PATCH] Test openapi schema of /citizens/{id} --- .../component/citizen/routes/GetOneCitizen.kt | 23 ++- src/main/resources/openapi2.yaml | 179 +++++++++++------- 2 files changed, 130 insertions(+), 72 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/component/citizen/routes/GetOneCitizen.kt b/src/main/kotlin/fr/dcproject/component/citizen/routes/GetOneCitizen.kt index 6b108ed..b8366c2 100644 --- a/src/main/kotlin/fr/dcproject/component/citizen/routes/GetOneCitizen.kt +++ b/src/main/kotlin/fr/dcproject/component/citizen/routes/GetOneCitizen.kt @@ -1,6 +1,7 @@ package fr.dcproject.component.citizen.routes import fr.dcproject.common.security.assert +import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.citizen.CitizenAccessControl import fr.dcproject.component.citizen.database.CitizenRef @@ -12,6 +13,8 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route +import org.joda.time.DateTime +import org.joda.time.format.ISODateTimeFormat import java.util.UUID @KtorExperimentalLocationsAPI @@ -26,7 +29,25 @@ object GetOneCitizen { val citizen = citizenRepository.findById(it.citizen.id) ?: throw NotFoundException("Citizen not found ${it.citizen.id}") ac.assert { canView(citizen, citizenOrNull) } - call.respond(it.citizen) + call.respond( + object { + val id: UUID = citizen.id + val name: Any = citizen.name.let { n -> + object { + val firstName: String = n.firstName + val lastName: String = n.lastName + } + } + val email: String = citizen.email + val birthday: String = citizen.birthday.run { + toString(ISODateTimeFormat.date()) + } + val createdAt: DateTime = citizen.createdAt + val user: Any = object { + val username: String = citizen.user.username + } + } + ) } } } diff --git a/src/main/resources/openapi2.yaml b/src/main/resources/openapi2.yaml index 6f2d692..60ed3f9 100644 --- a/src/main/resources/openapi2.yaml +++ b/src/main/resources/openapi2.yaml @@ -389,32 +389,7 @@ paths: result: type: array items: - type: object - required: - - id - - name - - email - properties: - id: - $ref: '#/components/schemas/UUID' - name: - type: object - required: - - firstName - - lastName - properties: - firstName: - type: string - example: - john - lastName: - type: string - example: - Doe - email: - type: string - format: email - example: my.email@dc-project.fr + $ref: '#/components/schemas/CitizenListResponse' 401: $ref: '#/components/responses/401' @@ -432,52 +407,29 @@ paths: content: application/json: schema: - type: object - required: - - id - - name - - email - - createdAt - properties: - id: - $ref: '#/components/schemas/UUID' - name: - type: object - required: - - firstName - - lastName - properties: - firstName: - type: string - example: - john - lastName: - type: string - example: - Doe - email: - type: string - format: email - example: my.email@dc-project.fr - birthday: - type: string - format: 'date' - example: '1984-12-25' - createdAt: - type: string - format: 'date-time' - user: - type: object - required: - - username - properties: - id: - $ref: '#/components/schemas/UUID' - username: - type: string - example: - john-doe + $ref: '#/components/schemas/CitizenResponse' + /citizens/{citizen}: + parameters: + - $ref: '#/components/parameters/citizen' + get: + security: + - JWTAuth: [] + summary: Get Citizen + tags: + - citizen + operationId: getCitizen + responses: + 200: + description: The Citizen object + content: + application/json: + schema: + $ref: '#/components/schemas/CitizenResponse' + 404: + description: Citizen not found + 401: + $ref: '#/components/responses/401' components: parameters: page: @@ -560,6 +512,16 @@ components: type: string format: uuid + citizen: + name: citizen + in: path + description: ID of citizen + example: 6434f4f9-f570-f22a-c134-8668350651ff + required: true + schema: + type: string + format: uuid + responses: 401: description: Unautorized @@ -723,6 +685,81 @@ components: score: type: integer + CitizenListResponse: + additionalProperties: false + type: object + required: + - id + - name + - email + properties: + id: + $ref: '#/components/schemas/UUID' + name: + type: object + required: + - firstName + - lastName + properties: + firstName: + type: string + example: + john + lastName: + type: string + example: + Doe + email: + type: string + format: email + example: my.email@dc-project.fr + CitizenResponse: + additionalProperties: false + type: object + required: + - id + - name + - email + - createdAt + properties: + id: + $ref: '#/components/schemas/UUID' + name: + type: object + required: + - firstName + - lastName + properties: + firstName: + type: string + example: + john + lastName: + type: string + example: + Doe + email: + type: string + format: email + example: my.email@dc-project.fr + birthday: + type: string + format: 'date' + example: '1984-12-25' + createdAt: + type: string + format: 'date-time' + user: + type: object + required: + - username + properties: + id: + $ref: '#/components/schemas/UUID' + username: + type: string + example: + john-doe securitySchemes: JWTAuth: type: http