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

Allow for alternate properties file to determine MP OpenAPI spec level #1389

Merged
merged 1 commit into from
Mar 2, 2023
Merged
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
35 changes: 25 additions & 10 deletions core/src/main/java/io/smallrye/openapi/api/util/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,41 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Optional;
import java.util.Properties;

import org.eclipse.microprofile.openapi.models.OpenAPI;

import io.smallrye.openapi.runtime.OpenApiRuntimeException;
import io.smallrye.openapi.runtime.util.ModelUtil;

public final class VersionUtil {

private VersionUtil() {
}

static final String MP_VERSION = ModelUtil.supply(() -> {
try (InputStream pomProperties = org.eclipse.microprofile.openapi.models.OpenAPI.class.getResourceAsStream(
"/META-INF/maven/org.eclipse.microprofile.openapi/microprofile-openapi-api/pom.properties")) {
Properties p = new Properties();
p.load(pomProperties);
return p.getProperty("version");
} catch (IOException e) {
throw new UncheckedIOException(e);
static final String SR_VERSION_PROPERTIES = VersionUtil.class.getName() + ".properties";
static final String MP_POM_PROPERTIES = "/META-INF/maven/org.eclipse.microprofile.openapi/microprofile-openapi-api/pom.properties"; // NOSONAR

static final String MP_VERSION = ModelUtil.supply(() -> loadProperty(
VersionUtil.class.getResource(SR_VERSION_PROPERTIES), "microprofile.openapi.version")
.orElseGet(() -> loadProperty(OpenAPI.class.getResource(MP_POM_PROPERTIES), "version")
.orElseThrow(() -> new OpenApiRuntimeException("Unable to determine MicroProfile OpenAPI version"))));

static Optional<String> loadProperty(URL source, String key) {
if (source != null) {
try (InputStream properties = source.openStream()) {
Properties p = new Properties();
p.load(properties);
return Optional.ofNullable(p.getProperty(key));
} catch (IOException e) {
// Ignore it
}
}
});

return Optional.empty();
}

static final String[] MP_VERSION_COMPONENTS = ModelUtil.supply(() -> {
int suffix = MP_VERSION.indexOf('-');
Expand Down