Search in sources :

Example 1 with ParameterSchema

use of com.linkedin.restli.restspec.ParameterSchema in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method addParams.

private void addParams(RestfulRequestBuilder<?, ?, ?> builder, ParameterSchemaArray parameters) {
    if (parameters != null) {
        for (ParameterSchema parameter : parameters) {
            if (// ignoring legacy case where items was used for arrays
            !parameter.hasItems()) {
                DataSchema dataSchema = RestSpecCodec.textToSchema(parameter.getType(), _schemaResolver);
                Object value = _dataGenerator.buildData(parameter.getName(), dataSchema);
                builder.setParam(parameter.getName(), value);
            }
        }
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Example 2 with ParameterSchema

use of com.linkedin.restli.restspec.ParameterSchema in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateActionMethod.

private void generateActionMethod(JDefinedClass facadeClass, JExpression baseUriExpr, JClass keyClass, ActionSchema action, JVar resourceSpecField, String resourceName, List<String> pathKeys, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeysTypes, Map<String, List<String>> pathToAssocKeys, JExpression requestOptionsExpr) throws JClassAlreadyExistsException {
    final JClass returnType = getActionReturnType(facadeClass, action.getReturns());
    final JClass vanillaActionBuilderClass = getCodeModel().ref(ActionRequestBuilderBase.class).narrow(keyClass, returnType);
    final String actionName = action.getName();
    final String actionBuilderClassName = CodeUtil.capitalize(resourceName) + "Do" + CodeUtil.capitalize(actionName) + METHOD_BUILDER_SUFFIX.get(_version);
    final JDefinedClass actionBuilderClass = facadeClass.getPackage()._class(JMod.PUBLIC, actionBuilderClassName);
    annotate(actionBuilderClass, null);
    checkVersionAndDeprecateBuilderClass(actionBuilderClass, false);
    actionBuilderClass._extends(vanillaActionBuilderClass.narrow(actionBuilderClass));
    final JMethod actionBuilderConstructor = actionBuilderClass.constructor(JMod.PUBLIC);
    final JVar uriParam = actionBuilderConstructor.param(_stringClass, "baseUriTemplate");
    final JVar classParam = actionBuilderConstructor.param(getCodeModel().ref(Class.class).narrow(returnType), "returnClass");
    final JVar resourceSpecParam = actionBuilderConstructor.param(_resourceSpecClass, "resourceSpec");
    final JVar actionsRequestOptionsParam = actionBuilderConstructor.param(RestliRequestOptions.class, "requestOptions");
    actionBuilderConstructor.body().invoke(SUPER).arg(uriParam).arg(classParam).arg(resourceSpecParam).arg(actionsRequestOptionsParam);
    actionBuilderConstructor.body().add(JExpr._super().invoke("name").arg(actionName));
    final JMethod actionMethod = facadeClass.method(JMod.PUBLIC, actionBuilderClass, "action" + CodeUtil.capitalize(actionName));
    actionMethod.body()._return(JExpr._new(actionBuilderClass).arg(baseUriExpr).arg(returnType.dotclass()).arg(resourceSpecField).arg(requestOptionsExpr));
    if (action.getParameters() != null) {
        for (ParameterSchema param : action.getParameters()) {
            final String paramName = param.getName();
            final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
            final JavaBinding binding = getJavaBindingType(param.getType(), facadeClass);
            final JMethod typesafeMethod = _version == RestliVersion.RESTLI_2_0_0 ? actionBuilderClass.method(JMod.PUBLIC, actionBuilderClass, RestLiToolsUtils.nameCamelCase(paramName + "Param")) : actionBuilderClass.method(JMod.PUBLIC, actionBuilderClass, "param" + CodeUtil.capitalize(paramName));
            final JVar typesafeMethodParam = typesafeMethod.param(binding.valueClass, "value");
            final JClass dataTemplateUtil = getCodeModel().ref(DataTemplateUtil.class);
            final JExpression dataSchema = dataTemplateUtil.staticInvoke("getSchema").arg(binding.schemaClass.dotclass());
            typesafeMethod.body().add(JExpr._super().invoke(isOptional ? "setParam" : "setReqParam").arg(resourceSpecField.invoke("getRequestMetadata").arg(actionName).invoke("getFieldDef").arg(paramName)).arg(typesafeMethodParam));
            typesafeMethod.body()._return(JExpr._this());
        }
    }
    generatePathKeyBindingMethods(pathKeys, actionBuilderClass, pathKeyTypes, assocKeysTypes, pathToAssocKeys);
    generateClassJavadoc(actionBuilderClass, action);
    generateFactoryMethodJavadoc(actionMethod, action);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) ActionRequestBuilderBase(com.linkedin.restli.client.base.ActionRequestBuilderBase) JClass(com.sun.codemodel.JClass) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 3 with ParameterSchema

use of com.linkedin.restli.restspec.ParameterSchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsFinder.

private void findModelsFinder(FinderSchema finderSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    ParameterSchemaArray parameters = finderSchema.getParameters();
    if (parameters != null) {
        for (ParameterSchema parameterSchema : parameters) {
            findModelsParameter(parameterSchema, foundTypes, typeOrder);
        }
    }
    MetadataSchema metadata = finderSchema.getMetadata();
    if (metadata != null) {
        String type = metadata.getType();
        recordType(type, foundTypes, typeOrder);
    }
}
Also used : MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray)

Example 4 with ParameterSchema

use of com.linkedin.restli.restspec.ParameterSchema in project rest.li by linkedin.

the class RequestBuilderSpecGenerator method generateQueryParamBindingMethods.

private void generateQueryParamBindingMethods(ParameterSchemaArray parameters, RequestBuilderSpec requestBuilderClass) {
    for (ParameterSchema param : parameters) {
        final String paramName = param.getName();
        final DataSchema typeSchema = RestSpecCodec.textToSchema(param.getType(), _schemaResolver);
        final ClassTemplateSpec typeClassSpec = schemaToTemplateSpec(typeSchema);
        final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
        final String setMethodName = RestLiToolsUtils.nameCamelCase(paramName + "Param");
        final QueryParamBindingMethodSpec spec = new QueryParamBindingMethodSpec(setMethodName);
        spec.setParamName(paramName);
        spec.setNeedAddParamMethod(false);
        spec.setOptional(isOptional);
        spec.setArgType(typeClassSpec);
        spec.setDoc(param.getDoc());
        requestBuilderClass.addQueryParamMethod(spec);
        if (typeSchema instanceof ArrayDataSchema) {
            final DataSchema itemsSchema = ((ArrayDataSchema) typeSchema).getItems();
            final ClassTemplateSpec itemSpecClass = schemaToTemplateSpec(itemsSchema);
            final String addMethodName = RestLiToolsUtils.nameCamelCase("add" + RestLiToolsUtils.normalizeCaps(paramName) + "Param");
            final QueryParamBindingMethodSpec addSpec = new QueryParamBindingMethodSpec(addMethodName);
            addSpec.setParamName(paramName);
            addSpec.setNeedAddParamMethod(true);
            addSpec.setOptional(isOptional);
            addSpec.setArgType(itemSpecClass);
            addSpec.setDoc(param.getDoc());
            requestBuilderClass.addQueryParamMethod(addSpec);
        }
    }
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) QueryParamBindingMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.QueryParamBindingMethodSpec)

Example 5 with ParameterSchema

use of com.linkedin.restli.restspec.ParameterSchema in project rest.li by linkedin.

the class ResourceCompatibilityChecker method checkParameterArrayField.

/**
   * @return whether the check passes
   */
private boolean checkParameterArrayField(RecordDataSchema.Field field, ParameterSchemaArray prevArray, ParameterSchemaArray currArray) {
    final HashMap<String, Integer> currRemainder = new HashMap<String, Integer>();
    if (!checkComplexArrayField(field, "name", prevArray, currArray, currRemainder, false)) {
        return false;
    }
    _infoPath.push(field.getName());
    boolean result = true;
    for (int paramIndex : currRemainder.values()) {
        // all parameters only appear in curr must not be required
        final ParameterSchema param = currArray.get(paramIndex);
        if (isQueryParameterOptional(param.isOptional(), param.getDefault(GetMode.DEFAULT))) {
            _infoMap.addRestSpecInfo(CompatibilityInfo.Type.PARAMETER_NEW_OPTIONAL, _infoPath, param.getName());
        } else {
            _infoMap.addRestSpecInfo(CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, _infoPath, param.getName());
            result = false;
        }
    }
    _infoPath.pop();
    // all parameter errors have been recorded, avoid duplicate errors by returning true
    return result;
}
Also used : HashMap(java.util.HashMap) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Aggregations

ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)13 DataSchema (com.linkedin.data.schema.DataSchema)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 ParameterSchemaArray (com.linkedin.restli.restspec.ParameterSchemaArray)4 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)3 ActionSchema (com.linkedin.restli.restspec.ActionSchema)3 JClass (com.sun.codemodel.JClass)3 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2 MapDataSchema (com.linkedin.data.schema.MapDataSchema)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 FieldDef (com.linkedin.data.template.FieldDef)2 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)2 MetadataSchema (com.linkedin.restli.restspec.MetadataSchema)2 JVar (com.sun.codemodel.JVar)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DataMap (com.linkedin.data.DataMap)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1