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

Refactor configuration/option flags from config file. Add placeholder flags for (property) array generation and PATCH path #178

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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<packaging>jar</packaging>
<groupId>edu.isi.oba</groupId>
<artifactId>oba</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<name>core</name>
<url>https://github.com/KnowledgeCaptureAndDiscovery/OBA</url>

Expand Down
31 changes: 10 additions & 21 deletions src/main/java/edu/isi/oba/Mapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.isi.oba;

import edu.isi.oba.config.CONFIG_FLAG;
import edu.isi.oba.config.YamlConfig;
import static edu.isi.oba.Oba.logger;

Expand Down Expand Up @@ -33,17 +34,11 @@ class Mapper {
YamlConfig config_data;

public OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
private Boolean follow_references;
private Boolean default_descriptions;
private Boolean default_properties;

public Mapper(YamlConfig config_data) throws OWLOntologyCreationException, IOException {
this.config_data = config_data;
this.selected_paths = config_data.getPaths();
this.mappedClasses = new ArrayList<>();
this.follow_references = config_data.getFollow_references();
this.default_descriptions = config_data.getDefault_descriptions();
this.default_properties = config_data.getDefault_properties();

List<String> config_ontologies = config_data.getOntologies();
String destination_dir = config_data.getOutput_dir() + File.separator + config_data.getName();
Expand Down Expand Up @@ -106,14 +101,10 @@ private void download_ontologies(List<String> config_ontologies, String destinat
* The schemas includes the properties
*
* @param destination_dir directory to write the final results
* @param config_data yaml configuration
*/
public void createSchemas(String destination_dir) {
Query query = new Query(destination_dir);
Path pathGenerator = new Path(this.config_data.getEnable_get_paths(),
this.config_data.getEnable_post_paths(),
this.config_data.getEnable_put_paths(),
this.config_data.getEnable_delete_paths(),
PathGenerator pathGenerator = new PathGenerator(this.config_data.getConfigFlags(),
this.config_data.getAuth().getEnable()
);

Expand Down Expand Up @@ -148,7 +139,7 @@ public void createSchemas(String destination_dir) {
}
}

private void add_user_path(Path pathGenerator) {
private void add_user_path(PathGenerator pathGenerator) {
//User schema
Map<String, Schema> userProperties = new HashMap<>();
StringSchema username = new StringSchema();
Expand All @@ -165,7 +156,7 @@ private void add_user_path(Path pathGenerator) {
this.paths.addPathItem("/user/login", pathGenerator.user_login(userSchema.getName()));
}

private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator, OWLOntology ontology,
private List<OWLClass> add_owlclass_to_openapi(Query query, PathGenerator pathGenerator, OWLOntology ontology,
String defaultOntologyPrefixIRI, OWLClass cls, Boolean topLevel) {
List<OWLClass> ref = new ArrayList<>();
String classPrefixIRI = cls.getIRI().getNamespace();
Expand All @@ -182,7 +173,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
if(ontology.containsClassInSignature(clsToCheck.getIRI())) {
System.out.println("ADD "+ clsToCheck);
for (OWLOntology temp_ontology : this.ontologies) {
if (follow_references) {
if (this.config_data.getConfigFlagValue(CONFIG_FLAG.FOLLOW_REFERENCES)) {
this.mappedClasses.add(clsToCheck);
getMapperSchema(query, temp_ontology, clsToCheck, this.schemaDescriptions.get(clsToCheck.getIRI()));
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, clsToCheck, false);
Expand All @@ -197,11 +188,9 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,
logger.info("The class " + ref_class + " exists ");
} else {
for (OWLOntology temp_ontology : this.ontologies) {
if ( follow_references ) {
if (this.config_data.getConfigFlagValue(CONFIG_FLAG.FOLLOW_REFERENCES)) {
this.mappedClasses.add(ref_class);
getMapperSchema(query, temp_ontology, ref_class,this.schemaDescriptions.get(ref_class.getIRI()));
// OWLDocumentFormat format = ontology.getFormat();
// String temp_defaultOntologyPrefixIRI = format.asPrefixOWLDocumentFormat().getDefaultPrefix();
add_owlclass_to_openapi(query, pathGenerator, temp_ontology, classPrefixIRI, ref_class, false);
}
}
Expand All @@ -219,7 +208,7 @@ private List<OWLClass> add_owlclass_to_openapi(Query query, Path pathGenerator,

private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass cls, String cls_description) {
//Convert from OWL Class to OpenAPI Schema.
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, cls_description, schemaNames, ontology, this.follow_references, this.default_descriptions, this.default_properties);
MapperSchema mapperSchema = new MapperSchema(this.ontologies, cls, cls_description, schemaNames, ontology, this.config_data.getConfigFlags());
//Write queries
query.write_readme(mapperSchema.name);
//Create the OpenAPI schema
Expand All @@ -228,7 +217,7 @@ private MapperSchema getMapperSchema(Query query, OWLOntology ontology, OWLClass
return mapperSchema;
}

private void addOpenAPIPaths(Path pathGenerator, MapperSchema mapperSchema, OWLClass cls) {
private void addOpenAPIPaths(PathGenerator pathGenerator, MapperSchema mapperSchema, OWLClass cls) {
if (selected_classes != null && !selected_classes.contains(cls)) {
logger.info("Ignoring class " + cls.toString());
} else {
Expand All @@ -251,11 +240,11 @@ private void setSchemaNames(Set<OWLClass> classes) {
private void setSchemaDrescriptions(Set<OWLClass> classes, OWLOntology ontology){
for (OWLClass cls: classes) {
System.out.println(cls);
schemaDescriptions.put(cls.getIRI(), ObaUtils.getDescription(cls, ontology, this.default_descriptions));
schemaDescriptions.put(cls.getIRI(), ObaUtils.getDescription(cls, ontology, this.config_data.getConfigFlagValue(CONFIG_FLAG.DEFAULT_DESCRIPTIONS)));
}
}

private void add_path(Path pathGenerator, MapperSchema mapperSchema) {
private void add_path(PathGenerator pathGenerator, MapperSchema mapperSchema) {
String singular_name = "/" + mapperSchema.name.toLowerCase() + "s/{id}";
String plural_name = "/" + mapperSchema.name.toLowerCase() + "s";
//Create the plural paths: for example: /models/
Expand Down
45 changes: 21 additions & 24 deletions src/main/java/edu/isi/oba/MapperOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.swagger.models.Method;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.PathParameter;
Expand Down Expand Up @@ -30,14 +29,12 @@ class MapperOperation {
private final ApiResponses apiResponses = new ApiResponses();
private final Cardinality cardinality;
private final Schema schema;
private final Operation operation;

public Operation getOperation() {
return operation;
}

private final Operation operation;


public MapperOperation(String schemaName, String schemaURI, Method method, Cardinality cardinality, Boolean auth) {
this.auth = auth;
this.cardinality = cardinality;
Expand All @@ -50,6 +47,9 @@ public MapperOperation(String schemaName, String schemaURI, Method method, Cardi
case GET:
setOperationGet();
break;
case PATCH:
setOperationPatch();
break;
case PUT:
setOperationPut();
break;
Expand All @@ -59,7 +59,8 @@ public MapperOperation(String schemaName, String schemaURI, Method method, Cardi
case DELETE:
setOperationDelete();
break;

default:
break;
}

if (cardinality == Cardinality.SINGULAR){
Expand All @@ -70,32 +71,30 @@ public MapperOperation(String schemaName, String schemaURI, Method method, Cardi
.schema(new StringSchema()));
}

if (auth && (method == Method.PUT || method == Method.POST || method == Method.DELETE )) {
if (auth && Set.of(Method.PATCH, Method.PUT, Method.POST, Method.DELETE).contains(method)) {
parameters.add(new QueryParameter()
.description("Username")
.name("user")
.required(false)
.schema(new StringSchema()));
}

operation = new Operation()
.description(description)
.summary(summary)
.addTagsItem(schemaName)
.parameters(parameters)
.responses(apiResponses);


if (method == Method.PUT || method == Method.POST ){
if (Set.of(Method.PATCH, Method.PUT, Method.POST).contains(method)) {
operation.setRequestBody(requestBody);
}


if (method == Method.PUT || method == Method.POST || method == Method.DELETE ){
if (Set.of(Method.PATCH, Method.PUT, Method.POST, Method.DELETE).contains(method)) {
SecurityRequirement securityRequirement = new SecurityRequirement();
securityRequirement.addList("BearerAuth");
operation.addSecurityItem(securityRequirement);
}

}

private void setOperationGet() {
Expand All @@ -113,7 +112,7 @@ private void setOperationGet() {
case PLURAL:
summary = "List all instances of " + this.schemaName;
description = "Gets a list of all instances of " + this.schemaName +
" (more information in " +this.schemaURI+")";
" (more information in " + this.schemaURI + ")";
responseDescriptionOk = "Successful response - returns an array with the instances of " + schemaName + ".";

//Set response
Expand All @@ -140,9 +139,9 @@ private void setOperationGet() {
.schema(new IntegerSchema()._default(100).maximum(BigDecimal.valueOf(200)).minimum(BigDecimal.valueOf(1))));
break;
case SINGULAR:
summary = "Get a single " + this.schemaName +" by its id";
description = "Gets the details of a given " + this.schemaName+
" (more information in " +this.schemaURI+")";
summary = "Get a single " + this.schemaName + " by its id";
description = "Gets the details of a given " + this.schemaName +
" (more information in " + this.schemaURI + ")";
responseDescriptionOk = "Gets the details of a given " + schemaName;

//Set request
Expand All @@ -153,7 +152,10 @@ private void setOperationGet() {
break;

}
}

private void setOperationPatch() {
// TODO: implement
}

private void setOperationPost() {
Expand All @@ -177,13 +179,12 @@ private void setOperationPost() {
);
}


private void setOperationPut() {
String requestDescription = "An old " + this.schemaName + "to be updated";

summary = "Update an existing " + this.schemaName;
description = "Updates an existing " + this.schemaName+
" (more information in " +this.schemaURI+")";
description = "Updates an existing " + this.schemaName +
" (more information in " + this.schemaURI + ")";

//Set request
MediaType mediaType = new MediaType().schema(schema);
Expand All @@ -199,22 +200,18 @@ private void setOperationPut() {
)
.addApiResponse("404", new ApiResponse()
.description("Not Found"));

}


private void setOperationDelete() {
summary = "Delete an existing " + this.schemaName;
description = "Delete an existing " + this.schemaName+
" (more information in " +this.schemaURI+")";
description = "Delete an existing " + this.schemaName +
" (more information in " + this.schemaURI + ")";

//Set the response
apiResponses
.addApiResponse("202", new ApiResponse()
.description("Deleted"))
.addApiResponse("404", new ApiResponse()
.description("Not Found"));

}

}
Loading
Loading