Search in sources :

Example 1 with ParameterDef

use of com.devonfw.cobigen.openapiplugin.model.ParameterDef in project cobigen by devonfw.

the class OpenAPIInputReader method extractParameters.

/**
 * @param operationOverlay overlay of the operation to get all parameters from
 * @return a list of {@link ParameterDef} from a collection of parameters of an operation
 */
private List<ParameterDef> extractParameters(Overlay<Operation> operationOverlay) {
    Collection<? extends Parameter> parameters = operationOverlay.get().getParameters();
    Collection<String> tags = operationOverlay.get().getTags();
    RequestBody requestBody = operationOverlay.get().getRequestBody();
    List<ParameterDef> parametersList = new LinkedList<>();
    ParameterDef parameter;
    for (Parameter param : parameters) {
        parameter = new ParameterDef();
        parameter.setIsBody(false);
        switch(param.getIn()) {
            case "path":
                parameter.setInPath(true);
                break;
            case "query":
                parameter.setInQuery(true);
                break;
            case "header":
                parameter.setInHeader(true);
                break;
        }
        parameter.setName(param.getName());
        Schema schema = param.getSchema();
        Map<String, Object> constraints = extractConstraints(schema);
        if (param.isRequired()) {
            constraints.put(ModelConstant.NOTNULL, true);
        } else {
            constraints.put(ModelConstant.NOTNULL, false);
        }
        parameter.setConstraints(constraints);
        parameter.setDescription(param.getDescription());
        if (schema.getType().equals(Constants.ARRAY)) {
            parameter.setIsCollection(true);
            if (schema.getItemsSchema() != null) {
                parameter.setIsEntity(true);
            }
        }
        try {
            if (Overlay.isReference(operationOverlay.getOverlay(), param.getKey())) {
                parameter.setIsEntity(true);
                parameter.setType(schema.getName());
            } else {
                parameter.setType(schema.getType());
                parameter.setFormat(schema.getFormat());
            }
        } catch (NullPointerException e) {
            throw new CobiGenRuntimeException("Error at parameter " + param.getName() + ". Invalid OpenAPI file, path parameters need to have a schema defined.");
        }
        parametersList.add(parameter);
    }
    if (requestBody != null) {
        Schema mediaSchema;
        for (String media : requestBody.getContentMediaTypes().keySet()) {
            parameter = new ParameterDef();
            parameter.setIsBody(true);
            parameter.setMediaType(media);
            if (tags.contains(Constants.SEARCH_CRITERIA) || tags.contains(Constants.SEARCH_CRITERIA.toLowerCase())) {
                parameter.setIsSearchCriteria(true);
                parameter.setName("criteria");
            }
            if (requestBody.getContentMediaTypes().get(media).getSchema() != null) {
                mediaSchema = requestBody.getContentMediaTypes().get(media).getSchema();
                parameter.setIsEntity(true);
                parameter.setType(requestBody.getContentMediaType(media).getSchema().getName());
                if (!parameter.getIsSearchCriteria()) {
                    char[] c = mediaSchema.getName().toCharArray();
                    c[0] = Character.toLowerCase(c[0]);
                    parameter.setName(new String(c));
                }
            }
            if (parameter.getType() != null) {
                Map<String, Object> constraints = new HashMap<>();
                constraints.put(ModelConstant.NOTNULL, true);
                parameter.setConstraints(constraints);
                parametersList.add(parameter);
            }
        }
    }
    return parametersList;
}
Also used : CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) ParameterDef(com.devonfw.cobigen.openapiplugin.model.ParameterDef) HashMap(java.util.HashMap) Schema(com.reprezen.kaizen.oasparser.model3.Schema) LinkedList(java.util.LinkedList) Parameter(com.reprezen.kaizen.oasparser.model3.Parameter) RequestBody(com.reprezen.kaizen.oasparser.model3.RequestBody)

Example 2 with ParameterDef

use of com.devonfw.cobigen.openapiplugin.model.ParameterDef in project cobigen by devonfw.

the class OpenAPIInputReaderTest method testRetrieveConstraintsOfParameter.

@Test
public void testRetrieveConstraintsOfParameter() throws Exception {
    List<ParameterDef> parameters = getParametersOfOperations("two-components.yaml");
    List<Map<String, Object>> constraints = new LinkedList<>();
    for (ParameterDef param : parameters) {
        constraints.add(param.getConstraints());
    }
    assertThat(constraints).extracting("minimum").hasSize(10);
    assertThat(constraints).extracting("maximum").hasSize(10);
    assertThat(constraints).extracting("notNull").hasSize(10);
    assertThat(constraints).extracting("minimum").contains(0, 10);
    assertThat(constraints).extracting("maximum").contains(50, 200);
    assertThat(constraints).extracting("notNull").containsExactly(true, true, false, false, true, true, true, false, false, true);
}
Also used : ParameterDef(com.devonfw.cobigen.openapiplugin.model.ParameterDef) Map(java.util.Map) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with ParameterDef

use of com.devonfw.cobigen.openapiplugin.model.ParameterDef in project cobigen by devonfw.

the class OpenAPIInputReaderTest method getParametersOfOperations.

private List<ParameterDef> getParametersOfOperations(String testInputFilename) throws Exception {
    List<Object> inputObjects = getInputs(testInputFilename);
    List<ComponentDef> cmps = new LinkedList<>();
    for (Object o : inputObjects) {
        if (isComponentDef(o)) {
            cmps.add(((ComponentDef) o));
        } else {
            cmps.add(((EntityDef) o).getComponent());
        }
    }
    List<ParameterDef> parameters = new LinkedList<>();
    for (ComponentDef cmp : cmps) {
        for (PathDef path : cmp.getPaths()) {
            for (OperationDef op : path.getOperations()) {
                for (ParameterDef param : op.getParameters()) {
                    parameters.add(param);
                }
            }
        }
    }
    return parameters;
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) ParameterDef(com.devonfw.cobigen.openapiplugin.model.ParameterDef) OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) LinkedList(java.util.LinkedList)

Aggregations

ParameterDef (com.devonfw.cobigen.openapiplugin.model.ParameterDef)3 LinkedList (java.util.LinkedList)3 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)1 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)1 PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)1 Parameter (com.reprezen.kaizen.oasparser.model3.Parameter)1 RequestBody (com.reprezen.kaizen.oasparser.model3.RequestBody)1 Schema (com.reprezen.kaizen.oasparser.model3.Schema)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1