Skip to content

Commit

Permalink
Feat: Improved docket configuration (#191)
Browse files Browse the repository at this point in the history
* Feat: Added default-content-type Docket configuration

The configuration for `defaultContentType` was not possible via SpringBoot Properties.

Now we can indicate something like `springwolf.docket.default-content-type=application/json` (or `springwolf.docket.defaultContentType=application/json` to configure the value

* Feat: Added id Docket configuration

It was not possible to configure the AsyncAPI identifier

Now we can indicate something like `springwolf.docket.id=urn:something:something` (or `springwolf.docket.id=https://something` to configure the identifier value

* chore: Add default value for defaultContentType=application/json

---------

Co-authored-by: Timon Back <timon.back@otto.de>
  • Loading branch information
Carlos Tasada and timonback authored Jun 2, 2023
1 parent d6fb008 commit 8cd0163
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ public static class ConfigDocket {
@Nullable
private String basePackage;

/**
* Identifier of the application the AsyncAPI document is defining.
*
* @see com.asyncapi.v2._0_0.model.AsyncAPI#id
*/
@Nullable
private String id;

/**
* A string representing the default content type to use when encoding/decoding a message's payload.
*
* @see com.asyncapi.v2._0_0.model.AsyncAPI#defaultContentType
*/
@Nullable
private String defaultContentType;

@Nullable
private Map<String, Server> servers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void buildAsyncApi() {

asyncAPI = AsyncAPI.builder()
.info(docket.getInfo())
.id(docket.getId())
.defaultContentType(docket.getDefaultContentType())
.servers(docket.getServers())
.channels(channelsService.getChannels())
.components(components)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import com.asyncapi.v2._0_0.model.channel.ChannelItem;
import com.asyncapi.v2._0_0.model.info.Info;
import com.asyncapi.v2._0_0.model.server.Server;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

import javax.validation.constraints.NotNull;
import java.util.Collections;
Expand Down Expand Up @@ -35,6 +39,17 @@ public class AsyncAPI {
@Builder.Default
private String asyncapi = "2.0.0";

/**
* Identifier of the application the AsyncAPI document is defining.
* <p>
* This field represents a unique universal identifier of the application the AsyncAPI document is defining.
* It must conform to the URI format, according to RFC3986.
* <p>
* It is RECOMMENDED to use a URN to globally and uniquely identify the application during long periods of time,
* even after it becomes unavailable or ceases to exist.
*/
private String id;

/**
* <b>Required.</b>
* Provides metadata about the API. The metadata can be used by the clients if needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Data;
import lombok.NonNull;
import lombok.Singular;
import org.springframework.http.MediaType;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -51,4 +52,18 @@ public class AsyncApiDocket {
@Singular
private final List<ConsumerData> consumers;

/**
* A string representing the default content type to use when encoding/decoding a message's payload.
*
* @see <a href="https://www.asyncapi.com/docs/reference/specification/v2.0.0#defaultContentTypeString">Default Content Type</a>
*/
@Builder.Default
private final String defaultContentType = MediaType.APPLICATION_JSON_VALUE;

/**
* A string representing the default content type to use when encoding/decoding a message's payload.
*
* @see <a href="https://www.asyncapi.com/docs/reference/specification/v2.0.0#A2SIdString">Identifier</a>
*/
private final String id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ private AsyncApiDocket parseApplicationConfigProperties(SpringWolfConfigProperti

Info info = buildInfo(configProperties.getDocket().getInfo());

return AsyncApiDocket.builder()
AsyncApiDocket.AsyncApiDocketBuilder builder = AsyncApiDocket.builder()
.basePackage(configProperties.getDocket().getBasePackage())
.info(info)
.servers(configProperties.getDocket().getServers())
.build();
.id(configProperties.getDocket().getId());

if(configProperties.getDocket().getDefaultContentType() != null) {
builder.defaultContentType(configProperties.getDocket().getDefaultContentType());
}

return builder.build();
}

private static Info buildInfo(@Nullable SpringWolfConfigProperties.ConfigDocket.Info info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ void testContextWithAsyncApiDocketBean() {
"springwolf.enabled=true",
"springwolf.docket.info.title=Info title was loaded from spring properties",
"springwolf.docket.info.version=1.0.0",
"springwolf.docket.id=urn:io:github:stavshamir:springwolf:example",
"springwolf.docket.default-content-type=application/yaml",
"springwolf.docket.base-package=io.github.stavshamir.springwolf.example",
"springwolf.docket.servers.test-protocol.protocol=test",
"springwolf.docket.servers.test-protocol.url=some-server:1234",
Expand All @@ -81,6 +83,9 @@ void testContextWithApplicationProperties() {
assertNotNull(context);

assertThat(asyncApiService.getAsyncAPI()).isNotNull();
assertThat(asyncApiService.getAsyncAPI().getInfo().getTitle()).isEqualTo("Info title was loaded from spring properties");
assertThat(asyncApiService.getAsyncAPI().getDefaultContentType()).isEqualTo("application/yaml");
assertThat(asyncApiService.getAsyncAPI().getId()).isEqualTo("urn:io:github:stavshamir:springwolf:example");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void testConfigurationShouldMapAllPropertiesToTheDocket() {
ConfigDocket configDocket = new ConfigDocket();
configDocket.setBasePackage("test-base-package");

configDocket.setDefaultContentType("application/json");

Server server = Server.builder()
.protocol("some-protocol")
.url("some-url")
Expand All @@ -46,6 +48,7 @@ void testConfigurationShouldMapAllPropertiesToTheDocket() {
AsyncApiDocket asyncApiDocket = docketConfiguration.getAsyncApiDocket();

// then
assertThat(asyncApiDocket.getDefaultContentType()).isEqualTo(configDocket.getDefaultContentType());
assertThat(asyncApiDocket.getServers()).isEqualTo(configDocket.getServers());
assertThat(asyncApiDocket.getInfo().getTitle()).isEqualTo(info.getTitle());
assertThat(asyncApiDocket.getInfo().getVersion()).isEqualTo(info.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"name" : "Apache License 2.0"
}
},
"defaultContentType" : "application/json",
"servers" : {
"amqp" : {
"url" : "amqp:5672",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"name": "Apache License 2.0"
}
},
"defaultContentType" : "application/json",
"servers": {
"kafka": {
"url": "kafka:29092",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"name" : "Apache License 2.0"
}
},
"defaultContentType" : "application/json",
"servers" : {
"kafka" : {
"url" : "localhost:9092",
Expand Down

0 comments on commit 8cd0163

Please sign in to comment.