Skip to content
Snippets Groups Projects
Commit 6809ce39 authored by Clayton, Brandon Scott's avatar Clayton, Brandon Scott
Browse files

Merge branch 'schema' into 'main'

Feature Type Schema

See merge request !678
parents 009ed171 cd06eba8
No related branches found
No related tags found
2 merge requests!679Production Release | nshmp-haz,!678Feature Type Schema
Pipeline #201323 passed
...@@ -5,6 +5,7 @@ import java.util.List; ...@@ -5,6 +5,7 @@ import java.util.List;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import gov.usgs.earthquake.nshmp.model.HazardModel; import gov.usgs.earthquake.nshmp.model.HazardModel;
import gov.usgs.earthquake.nshmp.www.source.FeaturesService;
import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpResponse;
...@@ -56,6 +57,7 @@ public class SwaggerController { ...@@ -56,6 +57,7 @@ public class SwaggerController {
var schemas = components.getSchemas(); var schemas = components.getSchemas();
SwaggerUtils.siteClassSchema(schemas, List.copyOf(model.siteClasses().keySet())); SwaggerUtils.siteClassSchema(schemas, List.copyOf(model.siteClasses().keySet()));
SwaggerUtils.imtSchema(schemas, List.copyOf(model.config().hazard.imts)); SwaggerUtils.imtSchema(schemas, List.copyOf(model.config().hazard.imts));
FeaturesService.featureTypeSchema(schemas);
openApi.servers(null); openApi.servers(null);
openApi.getInfo().setTitle(model.name() + " Web Services"); openApi.getInfo().setTitle(model.name() + " Web Services");
......
...@@ -2,7 +2,10 @@ package gov.usgs.earthquake.nshmp.www.source; ...@@ -2,7 +2,10 @@ package gov.usgs.earthquake.nshmp.www.source;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -22,6 +25,8 @@ import gov.usgs.earthquake.nshmp.www.source.SourceService.SourceModel; ...@@ -22,6 +25,8 @@ import gov.usgs.earthquake.nshmp.www.source.SourceService.SourceModel;
import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpResponse;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
/** /**
...@@ -44,6 +49,44 @@ public class FeaturesService { ...@@ -44,6 +49,44 @@ public class FeaturesService {
} }
} }
/**
* Updates the {@code FeatureType} enum schema to Swagger.
*
* @param schemas The Swagger schemas
*/
public static void featureTypeSchema(Map<String, Schema> schemas) {
var schema = new Schema<FeatureType>();
schema.setType(SchemaTypeUtil.STRING_TYPE);
featureTypes().stream().forEach(featureType -> schema.addEnumItemObject(featureType));
schemas.put(
String.format(
"%s.%s",
FeaturesService.class.getSimpleName(),
FeatureType.class.getSimpleName()),
schema);
}
private static Set<FeatureType> featureTypes() {
Set<FeatureType> featureTypes = new TreeSet<>();
featureTypes.add(FeatureType.ALL);
Arrays.stream(FeatureType.values())
.sorted()
.filter(featureType -> featureType != FeatureType.ALL)
.forEach(featureType -> {
var features = Models.features(
ServletUtil.model(),
Optional.of(SourceType.valueOf(featureType.name())));
if (features.isPresent()) {
featureTypes.add(featureType);
}
});
return featureTypes;
}
private static HttpResponse<String> processResponse( private static HttpResponse<String> processResponse(
HttpRequest<?> request, HttpRequest<?> request,
RequestData requestData) { RequestData requestData) {
...@@ -51,10 +94,14 @@ public class FeaturesService { ...@@ -51,10 +94,14 @@ public class FeaturesService {
var features = requestData.featureType == FeatureType.ALL var features = requestData.featureType == FeatureType.ALL
? Models.features(ServletUtil.model(), Optional.empty()) ? Models.features(ServletUtil.model(), Optional.empty())
: Models.features(ServletUtil.model(), : Models.features(
ServletUtil.model(),
Optional.of(SourceType.valueOf(requestData.featureType.name()))); Optional.of(SourceType.valueOf(requestData.featureType.name())));
// TODO check features.isEmpty() and redirect to error response if (features.isEmpty()) {
throw new IllegalStateException(
String.format("No features found for type %s", requestData.featureType));
}
if (requestData.raw) { if (requestData.raw) {
return HttpResponse.ok(features.orElseThrow().toJson()); return HttpResponse.ok(features.orElseThrow().toJson());
...@@ -124,17 +171,17 @@ public class FeaturesService { ...@@ -124,17 +171,17 @@ public class FeaturesService {
Metadata(HazardModel model) { Metadata(HazardModel model) {
this.model = new SourceModel(model); this.model = new SourceModel(model);
this.featureType = Arrays.stream(FeatureType.values()) this.featureType = featureTypes().stream()
.map(featureType -> new Parameter(featureType.toString(), featureType.name())) .map(featureType -> new Parameter(featureType.toString(), featureType.name()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public SourceModel getModel() { public List<Parameter> getFeatureType() {
return model; return featureType;
} }
public List<Parameter> getSourceType() { public SourceModel getModel() {
return featureType; return model;
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment