Skip to content

Commit

Permalink
added validation class and put validation with generation, updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Davenport committed Jul 3, 2024
1 parent 3eb11a7 commit f33d10a
Show file tree
Hide file tree
Showing 26 changed files with 319 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
package org.brapi.schematools.cli;

import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.introspection.IntrospectionQuery;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.SchemaPrinter;
import io.swagger.v3.core.util.Json31;
import io.swagger.v3.oas.models.OpenAPI;
import org.brapi.schematools.core.brapischema.BrAPISchemaReader;
import org.brapi.schematools.core.brapischema.BrAPISchemaReaderException;
import org.brapi.schematools.core.graphql.GraphQLGenerator;
import org.brapi.schematools.core.graphql.options.GraphQLGeneratorOptions;
import org.brapi.schematools.core.model.BrAPIClass;
import org.brapi.schematools.core.openapi.OpenAPIGenerator;
import org.brapi.schematools.core.openapi.options.OpenAPIGeneratorOptions;
import org.brapi.schematools.core.response.Response;
import picocli.CommandLine;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -38,13 +21,9 @@ public class ValidateSubCommand implements Runnable {
public void run() {
BrAPISchemaReader schemaReader = new BrAPISchemaReader() ;

try {
schemaReader.readDirectories(schemaDirectory)
schemaReader.readDirectories(schemaDirectory)
.onFailDoWithResponse(this::printErrors)
.onSuccessDo(() -> System.out.println("The BrAPI JSON schema is valid"));
} catch (BrAPISchemaReaderException e) {
System.out.println(e.getMessage());
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ public BrAPISchemaReader() {
*
* @param schemaDirectory the parent directory that holds all the module directories
* @return a response containing a list of BrAPIClass with one type per JSON Schema or validation errors
* @throws BrAPISchemaReaderException if there is a problem reading the directories or JSON schemas
*/
public Response<List<BrAPIClass>> readDirectories(Path schemaDirectory) throws BrAPISchemaReaderException {
public Response<List<BrAPIClass>> readDirectories(Path schemaDirectory) {
try {
return dereferenceAndValidate(find(schemaDirectory, 3, this::schemaPathMatcher).map(this::createBrAPISchemas).collect(Response.mergeLists())) ;
} catch (RuntimeException | IOException e) {
throw new BrAPISchemaReaderException(e);
return fail(Response.ErrorType.VALIDATION, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import graphql.AssertException;
import graphql.TypeResolutionEnvironment;
import graphql.schema.*;
import io.swagger.v3.oas.models.media.ObjectSchema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.brapi.schematools.core.brapischema.BrAPISchemaReader;
Expand Down Expand Up @@ -88,12 +87,8 @@ public Response<GraphQLSchema> generate(Path schemaDirectory) {
* @return the {@link GraphQLSchema} from the complete BrAPI Specification
*/
public Response<GraphQLSchema> generate(Path schemaDirectory, GraphQLGeneratorMetadata metadata) {

try {
return schemaReader.readDirectories(schemaDirectory).mapResultToResponse(brAPISchemas -> new Generator(options, metadata, brAPISchemas).generate()) ;
} catch (BrAPISchemaReaderException e) {
return fail(Response.ErrorType.VALIDATION, e.getMessage());
}
return options.validate().asResponse().merge(
schemaReader.readDirectories(schemaDirectory).mapResultToResponse(brAPISchemas -> new Generator(options, metadata, brAPISchemas).generate())) ;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import org.brapi.schematools.core.model.BrAPIType;
import org.brapi.schematools.core.options.AbstractOptions;
import org.brapi.schematools.core.options.Validation;

import static org.brapi.schematools.core.utils.StringUtils.toParameterCase;

Expand All @@ -20,9 +21,8 @@ public abstract class AbstractGraphQLOptions extends AbstractOptions {
@Getter(AccessLevel.PRIVATE)
private String nameFormat;

public void validate() {
super.validate();
assert nameFormat != null : String.format("'nameFormat' option on %s is null", this.getClass().getSimpleName());
public Validation validate() {
return super.validate().assertNotNull("'nameFormat' option on %s is null", this.getClass().getSimpleName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.brapi.schematools.core.model.BrAPIType;
import org.brapi.schematools.core.options.Validation;

import java.util.HashMap;
import java.util.Map;

import static org.brapi.schematools.core.utils.StringUtils.toSentenceCase;

/**
* Abstract base class for all GraphQL Options
*/
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -21,10 +25,10 @@ public class AbstractGraphQLQueryOptions extends AbstractGraphQLOptions {
@Setter(AccessLevel.PRIVATE)
private Map<String, Boolean> input = new HashMap<>();

public void validate() {
super.validate();
assert responseTypeNameFormat != null : String.format("'responseTypeNameFormat' option on %s is null", this.getClass().getSimpleName());
assert input != null : String.format("'input' option on %s is null", this.getClass().getSimpleName());
public Validation validate() {
return Validation.valid()
.assertNotNull(responseTypeNameFormat, "'responseTypeNameFormat' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(input, "'input' option on %s is null", this.getClass().getSimpleName()) ;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.brapi.schematools.core.graphql.GraphQLGenerator;
import org.brapi.schematools.core.model.BrAPIType;
import org.brapi.schematools.core.options.AbstractGeneratorOptions;
import org.brapi.schematools.core.options.Validation;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -67,25 +68,20 @@ public static GraphQLGeneratorOptions load(InputStream inputStream) throws IOExc

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

GraphQLGeneratorOptions options = mapper.readValue(inputStream, GraphQLGeneratorOptions.class);

options.validate() ;

return options ;
return mapper.readValue(inputStream, GraphQLGeneratorOptions.class);
}

public void validate() {
super.validate() ;

assert input != null : "Input Options are null";
assert queryType != null : "Query Options are null";
assert mutationType != null : "Mutation Options are null";
assert ids != null : "Id Options are null";

input.validate() ;
queryType.validate() ;
mutationType.validate() ;
ids.validate() ;
public Validation validate() {

return super.validate()
.assertNotNull(input, "Input Options are null")
.assertNotNull(queryType != null, "Query Options are null")
.assertNotNull( mutationType != null, "Mutation Options are null")
.assertNotNull(ids != null, "Id Options are null")
.merge(input)
.merge(queryType)
.merge(mutationType)
.merge(ids) ;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import org.brapi.schematools.core.model.BrAPIType;
import org.brapi.schematools.core.options.Options;
import org.brapi.schematools.core.options.Validation;
import org.brapi.schematools.core.utils.StringUtils;

import java.util.HashMap;
Expand All @@ -18,17 +20,18 @@
@Setter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class IdsOptions {
public class IdsOptions implements Options {
@Getter(AccessLevel.PRIVATE)
String nameFormat;
@JsonProperty("useIDType")
boolean usingIDType;
@Setter(AccessLevel.PRIVATE)
private Map<String, String> fieldFor = new HashMap<>();

public void validate() {
assert nameFormat != null : String.format("'nameFormat' option on %s is null", this.getClass().getSimpleName());
assert fieldFor != null : String.format("'fieldFor' option on %s is null", this.getClass().getSimpleName());
public Validation validate() {
return Validation.valid()
.assertNotNull(nameFormat, "'nameFormat' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(fieldFor, "'fieldFor' option on %s is null", this.getClass().getSimpleName()) ;
}

/**
Expand All @@ -42,7 +45,7 @@ public final String getNameFor(@NonNull String name) {
}

/**
* Gets the name of id for a specific primary model
* Gets the name of id for a specific primary model. Use {@link #setIDFieldFor} to override this value.
* @param type the primary model
* @return the name of the id for a specific primary model
*/
Expand All @@ -53,7 +56,7 @@ public final String getNameFor(@NonNull BrAPIType type) {

/**
* Gets the id field name for a specific primary model. For example the id field
* name of Study, would be 'studyDbiId' by default. Use {@link #setIDParameterFor} to override this value.
* name of Study, would be 'studyDbiId' by default. Use {@link #setIDFieldFor} to override this value.
* @param name the name of the primary model
* @return id parameter name for a specific primary model
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.brapi.schematools.core.model.BrAPIType;
import org.brapi.schematools.core.options.Options;
import org.brapi.schematools.core.options.Validation;
import org.brapi.schematools.core.utils.StringUtils;

import static org.brapi.schematools.core.utils.StringUtils.toParameterCase;

/**
* The options used to generate Input object types
*/
@Getter(AccessLevel.PRIVATE)
@Setter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class InputOptions {
public class InputOptions implements Options {
private String name;
private String nameFormat;
private String typeNameFormat;

public void validate() {
assert name != null : "'name' option on Input Options is null";
assert typeNameFormat != null : "'typeNameFormat' option on Input Options is null";
public Validation validate() {
return Validation.valid()
.assertNotNull(name, "'name' option on Input Options is null")
.assertNotNull(typeNameFormat, "'typeNameFormat' option on Input Options is null") ;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.brapi.schematools.core.model.BrAPIType;
import org.brapi.schematools.core.options.Validation;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -31,15 +32,15 @@ public class ListQueryOptions extends AbstractGraphQLQueryOptions {
private String pageTypeName;
private String pageFieldName;

public void validate() {
super.validate();
assert dataFieldName != null : String.format("'dataFieldName' option on %s is null", this.getClass().getSimpleName());
assert paged != null : String.format("'paged' option on %s is null", this.getClass().getSimpleName());
assert input != null : String.format("'input' option on %s is null", this.getClass().getSimpleName());
assert pagingInputName != null : String.format("'pagingInputName' option on %s is null", this.getClass().getSimpleName());
assert pageInputTypeName != null : String.format("'pageInputTypeName' option on %s is null", this.getClass().getSimpleName());
assert pageTypeName != null : String.format("'pageTypeName' option on %s is null", this.getClass().getSimpleName());
assert pageFieldName != null : String.format("'pageFieldName' option on %s is null", this.getClass().getSimpleName());
public Validation validate() {
return Validation.valid()
.assertNotNull(dataFieldName, "'dataFieldName' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(paged, "'paged' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(input, "'input' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(pagingInputName, "'pagingInputName' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(pageInputTypeName, "'pageInputTypeName' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(pageTypeName, "'pageTypeName' option on %s is null", this.getClass().getSimpleName())
.assertNotNull(pageFieldName, "'pageFieldName' option on %s is null", this.getClass().getSimpleName()) ;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.brapi.schematools.core.options.Options;
import org.brapi.schematools.core.options.Validation;

/**
* Provides options for the generation of the Mutation Type
Expand All @@ -13,7 +15,7 @@
@Setter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class MutationTypeOptions {
public class MutationTypeOptions implements Options {
private String name;
@Setter(AccessLevel.PRIVATE)
private CreateMutationOptions createMutation;
Expand All @@ -22,15 +24,14 @@ public class MutationTypeOptions {
@Setter(AccessLevel.PRIVATE)
private DeleteMutationOptions deleteMutation;

public void validate() {
assert name != null : "Name option on Mutation Type Options is null";

assert createMutation != null : "Create Mutation Options are null";
assert updateMutation != null : "Update Mutation Options are null";
assert deleteMutation != null : "Delete Mutation Options are null";

createMutation.validate() ;
updateMutation.validate() ;
deleteMutation.validate() ;
public Validation validate() {
return Validation.valid()
.assertNotNull(name, "Name option on Mutation Type Options is null")
.assertNotNull(createMutation, "Create Mutation Options are null")
.assertNotNull(updateMutation, "Update Mutation Options are null")
.assertNotNull(deleteMutation, "Delete Mutation Options are null")
.merge(createMutation)
.merge(updateMutation)
.merge(deleteMutation) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.brapi.schematools.core.options.Options;
import org.brapi.schematools.core.options.Validation;

/**
* Provides options for the generation of the Query Type
Expand All @@ -13,7 +15,7 @@
@Setter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class QueryTypeOptions {
public class QueryTypeOptions implements Options {
private String name;
private boolean partitionedByCrop;
@Setter(AccessLevel.PRIVATE)
Expand All @@ -23,15 +25,14 @@ public class QueryTypeOptions {
@Setter(AccessLevel.PRIVATE)
private SearchQueryOptions searchQuery;

public void validate() {
assert name != null : "Name option on QueryType Options is null";

assert singleQuery != null : "SingleQuery Options are null";
assert listQuery != null : "ListQuery Options are null";
assert searchQuery != null : "SearchQuery Options are null";

singleQuery.validate() ;
listQuery.validate() ;
searchQuery.validate() ;
public Validation validate() {
return Validation.valid()
.assertNotNull(name, "'name' option on QueryType Options is null")
.assertNotNull(singleQuery, "SingleQuery Options are null")
.assertNotNull(listQuery, "ListQuery Options are null")
.assertNotNull(searchQuery, "SearchQuery Options are null")
.merge(singleQuery)
.merge(listQuery)
.merge(searchQuery) ;
}
}
Loading

0 comments on commit f33d10a

Please sign in to comment.