Search in sources :

Example 11 with ApiParameterConfig

use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.

the class DiscoveryGenerator method convertMethodParameters.

private Map<String, JsonSchema> convertMethodParameters(ApiMethodConfig methodConfig) {
    Map<String, JsonSchema> parameters = Maps.newLinkedHashMap();
    Collection<String> pathParameters = methodConfig.getPathParameters();
    for (ApiParameterConfig parameterConfig : methodConfig.getParameterConfigs()) {
        if (parameterConfig.getClassification() == Classification.API_PARAMETER) {
            parameters.put(parameterConfig.getName(), convertMethodParameter(parameterConfig, pathParameters.contains(parameterConfig.getName())));
        }
    }
    return parameters;
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) JsonSchema(com.google.api.services.discovery.model.JsonSchema)

Example 12 with ApiParameterConfig

use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.

the class ApiConfigAnnotationReader method readMethodRequestParameter.

private void readMethodRequestParameter(ApiMethodConfig methodConfig, Annotation parameterName, Annotation description, Annotation nullable, Annotation defaultValue, TypeToken<?> type) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    String parameterNameString = null;
    if (parameterName != null) {
        parameterNameString = getAnnotationProperty(parameterName, "value");
    }
    String descriptionString = null;
    if (description != null) {
        descriptionString = getAnnotationProperty(description, "value");
    }
    String defaultValueString = null;
    if (defaultValue != null) {
        defaultValueString = getAnnotationProperty(defaultValue, "value");
    }
    ApiParameterConfig parameterConfig = methodConfig.addParameter(parameterNameString, descriptionString, nullable != null, defaultValueString, type);
    Annotation apiSerializer = type.getRawType().getAnnotation(annotationTypes.get("ApiTransformer"));
    if (apiSerializer != null) {
        Class<? extends Transformer<?, ?>> serializer = getAnnotationProperty(apiSerializer, "value");
        parameterConfig.setSerializer(serializer);
    }
    if (parameterConfig.isRepeated()) {
        TypeToken<?> repeatedItemType = parameterConfig.getRepeatedItemType();
        apiSerializer = repeatedItemType.getRawType().getAnnotation(annotationTypes.get("ApiTransformer"));
        if (apiSerializer != null) {
            Class<? extends Transformer<?, ?>> repeatedItemSerializer = getAnnotationProperty(apiSerializer, "value");
            parameterConfig.setRepeatedItemSerializer(repeatedItemSerializer);
        }
    }
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) Annotation(java.lang.annotation.Annotation)

Example 13 with ApiParameterConfig

use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.

the class ApiConfigValidator method validateResourceAndFieldNames.

private void validateResourceAndFieldNames(ApiMethodConfig methodConfig) throws PropertyParameterNameConflictException {
    for (ApiParameterConfig parameterConfig : methodConfig.getParameterConfigs()) {
        if (parameterConfig.getClassification() == Classification.RESOURCE) {
            Schema schema = schemaRepository.getOrAdd(parameterConfig.getSchemaBaseType(), methodConfig.getApiConfig());
            Set<String> fieldNames = schema.fields().keySet();
            for (ApiParameterConfig parameter : methodConfig.getParameterConfigs()) {
                if (parameter.getClassification() == Classification.API_PARAMETER && !"id".equals(parameter.getName()) && fieldNames.contains(parameter.getName())) {
                    log.atWarning().log("Parameter %s conflicts with a resource field name. This may " + "result in unexpected values in the request.", parameter.getName());
                }
            }
        }
    }
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) Schema(com.google.api.server.spi.config.model.Schema)

Example 14 with ApiParameterConfig

use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.

the class ApiConfigAnnotationReaderTest method testKnownParameterizedType.

@Test
public void testKnownParameterizedType() throws Exception {
    @Api
    class Bar<T1> {

        @SuppressWarnings("unused")
        public void bar(T1 t1) {
        }
    }
    class Foo extends Bar<Integer> {
    }
    ApiConfig config = createConfig(Foo.class);
    annotationReader.loadEndpointMethods(serviceContext, Foo.class, config.getApiClassConfig().getMethods());
    ApiParameterConfig parameter = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Foo.class.getSuperclass().getDeclaredMethod("bar", Object.class))).getParameterConfigs().get(0);
    assertEquals(ApiParameterConfig.Classification.API_PARAMETER, parameter.getClassification());
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) Foo(com.google.api.server.spi.testing.Foo) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Example 15 with ApiParameterConfig

use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.

the class ApiConfigAnnotationReaderTest method testParameterAnnotations.

@Test
public void testParameterAnnotations() throws Exception {
    @Api
    class Endpoint {

        @SuppressWarnings("unused")
        public void method(@Named("foo") @Nullable @DefaultValue("4") int foo) {
        }
    }
    ApiConfig config = createConfig(Endpoint.class);
    annotationReader.loadEndpointClass(serviceContext, Endpoint.class, config);
    annotationReader.loadEndpointMethods(serviceContext, Endpoint.class, config.getApiClassConfig().getMethods());
    ApiMethodConfig methodConfig = Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
    ApiParameterConfig parameterConfig = Iterables.getOnlyElement(methodConfig.getParameterConfigs());
    validateParameter(parameterConfig, "foo", true, "4", int.class, null, int.class);
}
Also used : Named(com.google.api.server.spi.config.Named) DefaultValue(com.google.api.server.spi.config.DefaultValue) ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api) Nullable(com.google.api.server.spi.config.Nullable) Test(org.junit.Test)

Aggregations

ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)17 Api (com.google.api.server.spi.config.Api)7 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)7 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)7 SimpleLevelOverridingInheritedApi (com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi)7 Test (org.junit.Test)6 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)4 Schema (com.google.api.server.spi.config.model.Schema)4 JsonSchema (com.google.api.services.discovery.model.JsonSchema)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 EndpointMethod (com.google.api.server.spi.EndpointMethod)2 Foo (com.google.api.server.spi.testing.Foo)2 RestMethod (com.google.api.services.discovery.model.RestMethod)2 Request (com.google.api.services.discovery.model.RestMethod.Request)2 Response (com.google.api.services.discovery.model.RestMethod.Response)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 DefaultValue (com.google.api.server.spi.config.DefaultValue)1 Description (com.google.api.server.spi.config.Description)1