Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
feat: update swagger to openapi and define model for generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Oct 16, 2020
1 parent a80ca9c commit e51a2b7
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 149 deletions.
143 changes: 90 additions & 53 deletions api/agencyservice.yaml
Original file line number Diff line number Diff line change
@@ -1,76 +1,113 @@
swagger: "2.0"
openapi: 3.0.1
info:
description: "This information will be replaced by the SpringFox config information"
version: "0.0.1"
title: "will be replaced"
securityDefinitions:
Bearer:
type: "apiKey"
name: "Authorization"
in: "header"

# REST API path definitions #
paths:
title: will be replaced
description: This information will be replaced by the SpringFox config information
version: 0.0.1
servers:
- url: /
paths:
/agencies:
get:
tags:
- "agency-controller"
summary: "Returns the list of agencies. If no agency is found the white spot agency id is being returned. When no white spot agency is defined No Content is being returned. [Authorization: none]"
operationId: "getAgencies"
- agency-controller
summary: 'Returns the list of agencies. If no agency is found the white spot
agency id is being returned. When no white spot agency is defined No Content
is being returned. [Authorization: none]'
operationId: getAgencies
parameters:
- in: query
name: postcode
type: string
- name: postcode
in: query
description: The postcode the user entered
- in: query
name: consultingType
type: integer
format: int32
schema:
type: string
- name: consultingType
in: query
description: The consulting type of the registration form
schema:
type: integer
format: int32
responses:
200:
description: "OK - successfull operation"
schema:
type: "array"
items:
$ref: "#/definitions/AgencyResponseDTO"
description: OK - successfull operation
content:
'*/*':
schema:
type: array
items:
$ref: '#/components/schemas/AgencyResponseDTO'
400:
description: "BAD REQUEST - invalid/incomplete request or body object"
description: BAD REQUEST - invalid/incomplete request or body object
500:
description: "INTERNAL SERVER ERROR - server encountered unexpected condition"
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/agencies/{agencyIds}:
get:
tags:
- "agency-controller"
summary: "Returns the information of provided agencies (Ids). [Authorization: none]"
operationId: "getAgencies"
security:
- Bearer: []
- agency-controller
summary: 'Returns the information of provided agencies (Ids). [Authorization:
none]'
operationId: getAgenciesByIds
parameters:
- in: "path"
- name: agencyIds
in: path
description: List of comma-seperated Agency IDs
required: true
description: "List of comma-seperated Agency IDs"
name: "agencyIds"
type: "array"
items:
type: integer
format: int64
responses:
200:
description: "OK - successfull operation"
style: simple
explode: false
schema:
type: "array"
type: array
items:
$ref: "#/definitions/AgencyResponseDTO"
type: integer
format: int64
responses:
200:
description: OK - successfull operation
content:
'*/*':
schema:
type: array
items:
$ref: '#/components/schemas/AgencyResponseDTO'
400:
description: "BAD REQUEST - invalid/incomplete request or body object"
description: BAD REQUEST - invalid/incomplete request or body object
401:
description: "UNAUTHORIZED - no/invalid CSRF token"
description: UNAUTHORIZED - no/invalid CSRF token
500:
description: "INTERNAL SERVER ERROR - server encountered unexpected condition"
description: INTERNAL SERVER ERROR - server encountered unexpected condition
security:
- Bearer: []

components:
schemas:
AgencyResponseDTO:
type: object
properties:
id:
type: long
example: "684"
name:
type: string
example: "Suchtberatung Freiburg"
postcode:
type: string
example: "79106"
city:
type: string
example: "Bonn"
description:
type: string
example: "Our agency provides help for the following topics: Lorem ipsum.."
teamAgency:
type: boolean
example: "false"
offline:
type: boolean
example: "false"
consultingType:
type: integer
example: "0"

# Model/object definitions #
definitions:
AgencyResponseDTO:
type: "object"
securitySchemes:
Bearer:
type: apiKey
name: Authorization
in: header
40 changes: 22 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Swagger dependencies -->

<!-- Swagger annotations: needed for swagger-codegen-maven-plugin -->
<!-- OpenApi/Swagger dependencies -->
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.0</version>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.1</version>
</dependency>
<!-- SpringFox: generate YAML file from POJOs and generate documentation -->
<dependency>
Expand Down Expand Up @@ -139,7 +143,9 @@
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
<scope>test</scope><exclusions>
</exclusions>

</dependency>
<dependency>
<artifactId>powermock-module-junit4</artifactId>
Expand Down Expand Up @@ -176,28 +182,26 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<!-- Swagger codegen maven plugin: generates api stubs -->
<!-- OpenApi codegen maven plugin: generates api stubs -->
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/api/agencyservice.yaml</inputSpec>
<language>spring</language>
<apiPackage>${project.groupId}.${project.artifactId}.generated.api.controller
</apiPackage>
<modelPackage>${project.groupId}.${project.artifactId}.api.model</modelPackage>
<generateModels>false</generateModels>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<sourceFolder>/</sourceFolder>
</configOptions>
<inputSpec>${project.basedir}/api/agencyservice.yaml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>${project.groupId}.${project.artifactId}.generated.api.controller</apiPackage>
<modelPackage>${project.groupId}.${project.artifactId}.api.model</modelPackage>
<generateSupportingFiles>true</generateSupportingFiles>
<output>target/generated-sources</output>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package de.caritas.cob.agencyservice.api.controller;

import static java.util.Objects.requireNonNull;

import de.caritas.cob.agencyservice.api.exception.BadRequestException;
import de.caritas.cob.agencyservice.api.model.AgencyResponseDTO;
import de.caritas.cob.agencyservice.api.repository.agency.ConsultingType;
import de.caritas.cob.agencyservice.api.service.AgencyService;
import de.caritas.cob.agencyservice.generated.api.controller.AgenciesApi;
import io.swagger.annotations.Api;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import de.caritas.cob.agencyservice.api.exception.BadRequestException;
import de.caritas.cob.agencyservice.api.model.AgencyResponseDTO;
import de.caritas.cob.agencyservice.api.repository.agency.ConsultingType;
import de.caritas.cob.agencyservice.api.service.AgencyService;
import de.caritas.cob.agencyservice.generated.api.controller.AgenciesApi;
import io.swagger.annotations.Api;

/**
* Controller for agency API requests
Expand All @@ -30,7 +31,7 @@ public class AgencyController implements AgenciesApi {

@Autowired
public AgencyController(AgencyService agencyService) {
this.agencyService = agencyService;
this.agencyService = requireNonNull(agencyService);
}

/**
Expand All @@ -42,8 +43,8 @@ public AgencyController(AgencyService agencyService) {
* @return the List of agencies with information
*/
@Override
public ResponseEntity<List<AgencyResponseDTO>> getAgencies(@Valid String postcode,
@Valid @NotNull @RequestParam Integer consultingType) {
public ResponseEntity<List<AgencyResponseDTO>> getAgencies(@Valid @RequestParam String postcode,
@Valid @RequestParam Integer consultingType) {
if (postcode == null || postcode.length() < 3 || postcode.length() > 5) {
throw new BadRequestException("Postcode size is invalid");
}
Expand All @@ -68,7 +69,8 @@ public ResponseEntity<List<AgencyResponseDTO>> getAgencies(@Valid String postcod
* @return the List of agencies with information
*/
@Override
public ResponseEntity<List<AgencyResponseDTO>> getAgencies(@PathVariable("agencyIds") List<Long> agencyIds) {
public ResponseEntity<List<AgencyResponseDTO>> getAgenciesByIds(
@PathVariable("agencyIds") List<Long> agencyIds) {

List<AgencyResponseDTO> agencies = agencyService.getAgencies(agencyIds);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ private void addWhiteSpotAgency(ConsultingTypeSettings consultingTypeSettings,
}

private AgencyResponseDTO convertToAgencyResponseDTO(Agency agency) {
return new AgencyResponseDTO(agency.getId(),
agency.getName(),
agency.getPostCode(),
agency.getCity(),
agency.getDescription(),
agency.isTeamAgency(),
agency.isOffline(),
agency.getConsultingType().getValue());
return new AgencyResponseDTO()
.id(agency.getId())
.name(agency.getName())
.postcode(agency.getPostCode())
.city(agency.getCity())
.description(agency.getDescription())
.teamAgency(agency.isTeamAgency())
.offline(agency.isOffline())
.consultingType(agency.getConsultingType().getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void configure(HttpSecurity http) throws Exception {
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.sessionAuthenticationStrategy(sessionAuthenticationStrategy()).and().authorizeRequests()
.antMatchers("/agencies/*").permitAll()
.antMatchers(SpringFoxConfig.whiteList).permitAll()
.antMatchers(SpringFoxConfig.WHITE_LIST).permitAll()
.antMatchers("/agencies").permitAll().anyRequest().denyAll();
}

Expand Down
Loading

0 comments on commit e51a2b7

Please sign in to comment.