Skip to content

Commit

Permalink
added validation command
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Davenport committed Jun 26, 2024
1 parent 7f032a4 commit 3eb11a7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

@CommandLine.Command(
name = "brapi",
description = "Command line tools for the BrAPI JSON schema, see the sub-commands for details",
version = "0.0.2",
footer = "Copyright (c) 2024 The Breeding API",
subcommands = {
ValidateSubCommand.class,
GenerateSubCommand.class
},
version = "1.0",
mixinStandardHelpOptions = true
)
public class BrAPICommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import java.util.List;

@CommandLine.Command(
name = "generate", mixinStandardHelpOptions = true
name = "generate", mixinStandardHelpOptions = true,
description = "Generates the OpenAPI Specification or GraphQL Schema from a BrAPI JSON schema"
)
public class GenerateSubCommand implements Runnable {

private static final OutputFormat DEFAULT_FORMAT = OutputFormat.OPEN_API;

@CommandLine.Parameters(index = "0", description = "The directory containing the BrAPI json schema")
@CommandLine.Parameters(index = "0", description = "The directory containing the BrAPI JSON schema")
private Path schemaDirectory;

@CommandLine.Option(names = {"-l", "--language"}, defaultValue = "GRAPHQL", fallbackValue = "OPEN_API", description = "The format of the Output. Possible options are: ${COMPLETION-CANDIDATES}. Default is ${DEFAULT_FORMAT}")
Expand Down Expand Up @@ -105,7 +106,12 @@ private void outputIntrospectionSchema(GraphQLSchema schema) {
}

private void printGraphQLSchemaErrors(Response<GraphQLSchema> response) {
System.err.printf("There were %d errors generating the GraphQL Schema:%n", response.getAllErrors().size());
if (response.getAllErrors().size() == 1) {
System.err.printf("There was 1 error generating the GraphQL Schema:%n");
} else {
System.err.printf("There were %d errors generating the GraphQL Schema:%n", response.getAllErrors().size());
}

response.getAllErrors().forEach(this::printError);
}

Expand Down Expand Up @@ -151,7 +157,12 @@ private void outputOpenAPISpecification(OpenAPI specification, Path outputPathFi
}

private void printOpenAPISpecificationErrors(Response<List<OpenAPI>> response) {
System.err.println("There were errors generating the OpenAPI Specification:");
if (response.getAllErrors().size() == 1) {
System.err.printf("There was 1 error generating the OpenAPI Specification:%n");
} else {
System.err.printf("There were %d errors generating the OpenAPI Specification:%n", response.getAllErrors().size());
}

response.getAllErrors().forEach(this::printError);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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;

@CommandLine.Command(
name = "validate", mixinStandardHelpOptions = true,
description = "Validates the BrAPI JSON schema"
)
public class ValidateSubCommand implements Runnable {

@CommandLine.Parameters(index = "0", description = "The directory containing the BrAPI JSON schema")
private Path schemaDirectory;

@Override
public void run() {
BrAPISchemaReader schemaReader = new BrAPISchemaReader() ;

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

}

private void printErrors(Response<List<BrAPIClass>> response) {
if (response.getAllErrors().size() == 1) {
System.err.printf("There was 1 error validating the JSON Schema:%n");
} else {
System.err.printf("There were %d errors validating the JSON Schema:%n", response.getAllErrors().size());
}

response.getAllErrors().forEach(this::printError);
}

private void printError(Response.Error error) {
switch (error.getType()) {

case VALIDATION -> {
System.err.print("Validation Error :");
}
case PERMISSION, OTHER -> {
System.err.print("Error :");
}
}
System.err.print('\t');

System.err.println(error.getMessage());
}
}

0 comments on commit 3eb11a7

Please sign in to comment.