Search in sources :

Example 1 with SwaggerException

use of io.swagger.parser.SwaggerException in project okta-sdk-java by okta.

the class AbstractOktaJavaClientCodegen method preprocessSwagger.

@Override
public void preprocessSwagger(Swagger swagger) {
    // make sure we have the apiFile location
    String apiFile = (String) additionalProperties.get(API_FILE_KEY);
    if (apiFile == null || apiFile.isEmpty()) {
        throw new SwaggerException("'additionalProperties." + API_FILE_KEY + " property is required. This must be " + "set to the same file that Swagger is using.");
    }
    try (Reader reader = new InputStreamReader(new FileInputStream(apiFile), StandardCharsets.UTF_8.toString())) {
        rawSwaggerConfig = new Yaml().loadAs(reader, Map.class);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to parse apiFile: " + apiFile, e);
    }
    vendorExtensions.put("basePath", swagger.getBasePath());
    super.preprocessSwagger(swagger);
    tagEnums(swagger);
    buildTopLevelResourceList(swagger);
    addListModels(swagger);
    buildModelTagMap(swagger);
    removeListAfterAndLimit(swagger);
    moveOperationsToSingleClient(swagger);
    handleOktaLinkedOperations(swagger);
    buildDiscriminationMap(swagger);
}
Also used : SwaggerException(io.swagger.parser.SwaggerException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml)

Example 2 with SwaggerException

use of io.swagger.parser.SwaggerException in project okta-sdk-java by okta.

the class AbstractOktaJavaClientCodegen method buildTopLevelResourceList.

/**
 * Figure out which models are top level models (directly returned from a endpoint).
 * @param swagger The instance of swagger.
 */
protected void buildTopLevelResourceList(Swagger swagger) {
    Set<String> resources = new HashSet<>();
    // Loop through all of the operations looking for the models that are used as the response and body params
    swagger.getPaths().forEach((pathName, path) -> path.getOperations().forEach(operation -> {
        // find all body params
        operation.getParameters().forEach(parameter -> {
            if (parameter instanceof BodyParameter) {
                resources.add(((RefModel) ((BodyParameter) parameter).getSchema()).getSimpleRef());
            }
        });
        // response objects are a more complicated, start with filter for only the 200 responses
        operation.getResponses().entrySet().stream().filter(entry -> "200".equals(entry.getKey())).forEach(entry -> {
            // this schema could be a ref or an array property containing a ref (or null)
            Property rawSchema = entry.getValue().getSchema();
            if (rawSchema != null) {
                RefProperty refProperty;
                // detect array properties
                if (rawSchema instanceof ArrayProperty) {
                    Property innerProp = ((ArrayProperty) rawSchema).getItems();
                    if (innerProp instanceof RefProperty) {
                        refProperty = (RefProperty) innerProp;
                    } else {
                        // invalid swagger config file
                        throw new SwaggerException("Expected 'schema.items.$ref' to exist.");
                    }
                } else if (rawSchema instanceof RefProperty) {
                    // non array, standard ref property typically in the format of '#/Definitions/MyModel'
                    refProperty = (RefProperty) rawSchema;
                } else {
                    throw new SwaggerException("Expected 'schema' to be of type 'ArrayProperty' or 'RefProperty'.");
                }
                // get the simple name 'MyModel' instead of '#/Definitions/MyModel'
                resources.add(refProperty.getSimpleRef());
            }
        });
    }));
    // find any children of these resources
    swagger.getDefinitions().forEach((name, model) -> {
        String parent = (String) model.getVendorExtensions().get("x-okta-parent");
        if (parent != null) {
            parent = parent.replaceAll(".*/", "");
            if (resources.contains(parent)) {
                resources.add(parent);
            }
        }
    });
    // mark each model with a 'top-level' vendorExtension
    resources.stream().map(resourceName -> swagger.getDefinitions().get(resourceName)).forEach(model -> {
        model.getVendorExtensions().put("top-level", true);
    });
    this.topLevelResources = resources;
}
Also used : SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) StringUtils(org.apache.commons.lang3.StringUtils) Model(io.swagger.models.Model) Path(io.swagger.models.Path) Locale(java.util.Locale) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.swagger.models.Operation) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) Collection(java.util.Collection) Set(java.util.Set) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Response(io.swagger.models.Response) List(java.util.List) Optional(java.util.Optional) CodegenParameter(io.swagger.codegen.CodegenParameter) AbstractJavaCodegen(io.swagger.codegen.languages.AbstractJavaCodegen) Swagger(io.swagger.models.Swagger) BooleanUtils(org.apache.commons.lang3.BooleanUtils) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TreeSet(java.util.TreeSet) Yaml(org.yaml.snakeyaml.Yaml) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CodegenProperty(io.swagger.codegen.CodegenProperty) RefProperty(io.swagger.models.properties.RefProperty) Logger(org.slf4j.Logger) CodegenOperation(io.swagger.codegen.CodegenOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) Iterator(java.util.Iterator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CodegenType(io.swagger.codegen.CodegenType) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) SwaggerException(io.swagger.parser.SwaggerException) CodegenModel(io.swagger.codegen.CodegenModel) HttpMethod(io.swagger.models.HttpMethod) Collections(java.util.Collections) SwaggerException(io.swagger.parser.SwaggerException) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) CodegenProperty(io.swagger.codegen.CodegenProperty) RefProperty(io.swagger.models.properties.RefProperty) HashSet(java.util.HashSet) RefProperty(io.swagger.models.properties.RefProperty)

Aggregations

SwaggerException (io.swagger.parser.SwaggerException)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Yaml (org.yaml.snakeyaml.Yaml)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CodegenModel (io.swagger.codegen.CodegenModel)1 CodegenOperation (io.swagger.codegen.CodegenOperation)1 CodegenParameter (io.swagger.codegen.CodegenParameter)1 CodegenProperty (io.swagger.codegen.CodegenProperty)1 CodegenType (io.swagger.codegen.CodegenType)1 AbstractJavaCodegen (io.swagger.codegen.languages.AbstractJavaCodegen)1 HttpMethod (io.swagger.models.HttpMethod)1