Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support identifier field in licence objects #1834

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ default String getInfoLicenseName() {
return getConfigValue(SmallRyeOASConfig.INFO_LICENSE_NAME, String.class, () -> null);
}

default String getInfoLicenseIdentifier() {
return getConfigValue(SmallRyeOASConfig.INFO_LICENSE_IDENTIFIER, String.class, () -> null);
}

default String getInfoLicenseUrl() {
return getConfigValue(SmallRyeOASConfig.INFO_LICENSE_URL, String.class, () -> null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private SmallRyeOASConfig() {
public static final String INFO_CONTACT_NAME = SMALLRYE_PREFIX + "info.contact.name";
public static final String INFO_CONTACT_URL = SMALLRYE_PREFIX + "info.contact.url";
public static final String INFO_LICENSE_NAME = SMALLRYE_PREFIX + "info.license.name";
public static final String INFO_LICENSE_IDENTIFIER = SMALLRYE_PREFIX + "info.license.identifier";
public static final String INFO_LICENSE_URL = SMALLRYE_PREFIX + "info.license.url";
public static final String OPERATION_ID_STRAGEGY = SMALLRYE_PREFIX + "operationIdStrategy";
public static final String DUPLICATE_OPERATION_ID_BEHAVIOR = SMALLRYE_PREFIX + "duplicateOperationIdBehavior";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class LicenseImpl extends ExtensibleImpl<License> implements License, Mod

private String name;
private String url;
private String identifier;

/**
* @see org.eclipse.microprofile.openapi.models.info.License#getName()
Expand Down Expand Up @@ -45,4 +46,20 @@ public void setUrl(String url) {
this.url = url;
}

/**
* @see org.eclipse.microprofile.openapi.models.info.License#getIdentifier()
*/
@Override
public String getIdentifier() {
return this.identifier;
}

/**
* @see org.eclipse.microprofile.openapi.models.info.License#setIdentifier(java.lang.String)
*/
@Override
public void setIdentifier(String identifier) {
this.identifier = identifier;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected static final void configureInfo(OpenApiConfig config, OpenAPI oai) {
config.getInfoLicenseName(),
config.getInfoLicenseUrl())) {
setIfPresent(config.getInfoLicenseName(), oai.getInfo().getLicense()::setName);
setIfPresent(config.getInfoLicenseIdentifier(), oai.getInfo().getLicense()::setIdentifier);
setIfPresent(config.getInfoLicenseUrl(), oai.getInfo().getLicense()::setUrl);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LicenseIO<V, A extends V, O extends V, AB, OB> extends ModelIO<Lice

private static final String PROP_NAME = "name";
private static final String PROP_URL = "url";
private static final String PROP_IDENTIFIER = "identifier";

public LicenseIO(IOContext<V, A, O, AB, OB> context) {
super(context, Names.LICENSE, Names.create(License.class));
Expand All @@ -26,6 +27,7 @@ public License read(AnnotationInstance annotation) {
License license = new LicenseImpl();
license.setName(value(annotation, PROP_NAME));
license.setUrl(value(annotation, PROP_URL));
license.setIdentifier(value(annotation, PROP_IDENTIFIER));
license.setExtensions(extensionIO().readExtensible(annotation));
return license;
}
Expand All @@ -36,6 +38,7 @@ public License readObject(O node) {
License license = new LicenseImpl();
license.setName(jsonIO().getString(node, PROP_NAME));
license.setUrl(jsonIO().getString(node, PROP_URL));
license.setIdentifier(jsonIO().getString(node, PROP_IDENTIFIER));
license.setExtensions(extensionIO().readMap(node));
return license;
}
Expand All @@ -44,6 +47,7 @@ public Optional<O> write(License model) {
return optionalJsonObject(model).map(node -> {
setIfPresent(node, PROP_NAME, jsonIO().toJson(model.getName()));
setIfPresent(node, PROP_URL, jsonIO().toJson(model.getUrl()));
setIfPresent(node, PROP_IDENTIFIER, jsonIO().toJson(model.getIdentifier()));
setAllIfPresent(node, extensionIO().write(model));
return node;
}).map(jsonIO()::buildObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ConfigExtensionsTest extends JaxRsDataObjectScannerTestBase {
private static final String CONTACT_NAME = "mp.openapi.extensions.smallrye.info.contact.name";
private static final String CONTACT_URL = "mp.openapi.extensions.smallrye.info.contact.url";
private static final String LICENSE_NAME = "mp.openapi.extensions.smallrye.info.license.name";
private static final String LICENSE_IDENTIFIER = "mp.openapi.extensions.smallrye.info.license.identifier";
private static final String LICENSE_URL = "mp.openapi.extensions.smallrye.info.license.url";

@Test
Expand Down Expand Up @@ -68,8 +69,9 @@ void testSettingJustContactEmail() throws IOException, JSONException {
}

@Test
void testSettingJustLicenseName() throws IOException, JSONException {
void testSettingJustLicenseNameAndIdentifier() throws IOException, JSONException {
System.setProperty(LICENSE_NAME, "Apache License 2.0");
System.setProperty(LICENSE_IDENTIFIER, "Apache-2.0");
Config config = ConfigProvider.getConfig();
OpenApiConfig openApiConfig = OpenApiConfig.fromConfig(config);
try {
Expand All @@ -81,6 +83,7 @@ void testSettingJustLicenseName() throws IOException, JSONException {

} finally {
System.clearProperty(LICENSE_NAME);
System.clearProperty(LICENSE_IDENTIFIER);
}
}

Expand All @@ -96,6 +99,7 @@ void testSettingAllInfo() throws IOException, JSONException {
System.setProperty(CONTACT_URL, "https://www.phillip-kruger.com");
System.setProperty(LICENSE_NAME, "Apache License 2.0");
System.setProperty(LICENSE_URL, "https://choosealicense.com/licenses/apache-2.0/");
//Licence Identifier excluded for being exclusive with URL

Config config = ConfigProvider.getConfig();
OpenApiConfig openApiConfig = OpenApiConfig.fromConfig(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"title" : "Generated API",
"version" : "1.0",
"license" : {
"name" : "Apache License 2.0"
"name" : "Apache License 2.0",
"identifier" : "Apache-2.0"
}
},
"paths" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ void basic_info(MavenExecutionResult result) throws IOException {
assertEquals(properties.get("infoContactUrl"), schema.getInfo().getContact().getUrl());
assertEquals(properties.get("infoContactEmail"), schema.getInfo().getContact().getEmail());
assertEquals(properties.get("infoLicenseName"), schema.getInfo().getLicense().getName());
assertEquals(properties.get("infoLicenseUrl"),
schema.getInfo().getLicense().getUrl());
assertEquals(properties.get("infoLicenseIdentifier"),
schema.getInfo().getLicense().getIdentifier());
//The URL is not tested here due to being exclusive with Identifier
assertEquals(properties.get("infoVersion"), schema.getInfo().getVersion());

List<String> servers = schema.getServers().stream().map(Server::getUrl).collect(Collectors.toList());
Expand Down
Loading