From 26483e62322c5ab4680114d1cf60ea7bb8d0f677 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Mon, 22 Aug 2022 00:00:29 +0200 Subject: [PATCH] BAEL-4345 add code for the swagger specify two responses with the same code article --- .../spring-boot-swagger-2/README.md | 2 + .../spring-boot-swagger-2/pom.xml | 75 +++++++++++++++++++ .../baeldung/tworesponses/Application.java | 13 ++++ .../com/baeldung/tworesponses/Config.java | 27 +++++++ .../src/main/resources/application.properties | 2 + .../src/main/resources/static/api_2.yaml | 45 +++++++++++ .../src/main/resources/static/api_3.yaml | 44 +++++++++++ 7 files changed, 208 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger-2/README.md create mode 100644 spring-boot-modules/spring-boot-swagger-2/pom.xml create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Application.java create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Config.java create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_2.yaml create mode 100644 spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_3.yaml diff --git a/spring-boot-modules/spring-boot-swagger-2/README.md b/spring-boot-modules/spring-boot-swagger-2/README.md new file mode 100644 index 0000000000..97c76dc431 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/README.md @@ -0,0 +1,2 @@ +## Relevant Articles: + diff --git a/spring-boot-modules/spring-boot-swagger-2/pom.xml b/spring-boot-modules/spring-boot-swagger-2/pom.xml new file mode 100644 index 0000000000..44555b9e43 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + spring-boot-swagger-2 + 0.1.0-SNAPSHOT + spring-boot-swagger-2 + jar + Module For Spring Boot Swagger + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + + + 3.0.0 + 3.0.34 + 1.6.10 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springdoc + springdoc-openapi-ui + ${springdoc.version} + + + io.springfox + springfox-oas + ${springfox.version} + + + io.springfox + springfox-swagger-ui + ${springfox.version} + + + javax.validation + validation-api + + + + + + + io.swagger.codegen.v3 + swagger-codegen-maven-plugin + ${swagger-codegen-maven-plugin.version} + + + + generate + + + ${project.basedir}/src/main/resources/static/api_3.yaml + spring + + true + true + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Application.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Application.java new file mode 100644 index 0000000000..0932e42747 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Application.java @@ -0,0 +1,13 @@ +package com.baeldung.tworesponses; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String args[]) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Config.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Config.java new file mode 100644 index 0000000000..9238a16698 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/tworesponses/Config.java @@ -0,0 +1,27 @@ +package com.baeldung.tworesponses; + +import org.springdoc.core.SpringDocConfigProperties; +import org.springdoc.core.SpringDocConfiguration; +import org.springdoc.core.providers.ObjectMapperProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class Config { + + @Bean + SpringDocConfiguration springDocConfiguration() { + return new SpringDocConfiguration(); + } + + @Bean + SpringDocConfigProperties springDocConfigProperties() { + return new SpringDocConfigProperties(); + } + + @Bean + ObjectMapperProvider objectMapperProvider(SpringDocConfigProperties springDocConfigProperties) { + return new ObjectMapperProvider(springDocConfigProperties); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties new file mode 100644 index 0000000000..03302ebb1c --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties @@ -0,0 +1,2 @@ +springdoc.api-docs.enabled=false +springdoc.swagger-ui.url=/api_3.yaml diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_2.yaml b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_2.yaml new file mode 100644 index 0000000000..c9f7f25d44 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_2.yaml @@ -0,0 +1,45 @@ +swagger: 2.0.0 +info: + title: Demo api + description: Demo api for the article 'specify two responses with same code based on optional parameter' + version: 0.1.0 +paths: + /vehicle: + get: + responses: + '200': + description: Get a vehicle. Can contain either a Car or a Bike + schema: + $ref: '#/definitions/CarOrBike' + examples: + application/json: + owner: baeldung + plate: AEX305 + speed: +definitions: + Car: + type: object + properties: + owner: + type: string + plate: + type: string + Bike: + type: object + properties: + owner: + type: string + speed: + type: integer + CarOrBike: + description: a car will have an owner and a plate, whereas a bike has an owner and a speed + type: object + required: + - owner + properties: + owner: + type: string + plate: + type: string + speed: + type: integer diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_3.yaml b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_3.yaml new file mode 100644 index 0000000000..12de1be39a --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/static/api_3.yaml @@ -0,0 +1,44 @@ +openapi: 3.0.0 +info: + title: Demo api + description: Demo api for the article 'specify two responses with same code based on optional parameter' + version: 0.1.0 +paths: + /vehicle: + get: + responses: + '200': + description: Get a vehicle + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/Car' + - $ref: '#/components/schemas/Bike' + examples: + car: + summary: an example of car + value: + owner: baeldung + plate: AEX305 + bike: + summary: an example of bike + value: + owner: john doe + speed: 25 +components: + schemas: + Car: + type: object + properties: + owner: + type: string + plate: + type: string + Bike: + type: object + properties: + owner: + type: string + speed: + type: integer