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);
}
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;
}
Aggregations