Search in sources :

Example 6 with JsonSchema

use of com.google.api.services.discovery.model.JsonSchema in project endpoints-java by cloudendpoints.

the class DiscoveryGenerator method convertMethodParameter.

private JsonSchema convertMethodParameter(ApiParameterConfig parameterConfig, boolean isPathParameter) {
    JsonSchema schema = new JsonSchema();
    TypeToken<?> type;
    if (parameterConfig.isRepeated()) {
        schema.setRepeated(true);
        type = parameterConfig.getRepeatedItemSerializedType();
    } else {
        type = parameterConfig.getSchemaBaseType();
    }
    if (parameterConfig.isEnum()) {
        List<String> enumValues = Lists.newArrayList();
        List<String> enumDescriptions = Lists.newArrayList();
        for (java.lang.reflect.Field field : type.getRawType().getFields()) {
            if (field.isEnumConstant()) {
                enumValues.add(field.getName());
                Description description = field.getAnnotation(Description.class);
                enumDescriptions.add(description == null ? "" : description.value());
            }
        }
        schema.setEnum(enumValues);
        schema.setEnumDescriptions(enumDescriptions);
        type = TypeToken.of(String.class);
    }
    schema.setType(typeLoader.getSchemaType(type));
    schema.setFormat(FieldType.fromType(type).getDiscoveryFormat());
    if (!parameterConfig.getNullable() && parameterConfig.getDefaultValue() == null) {
        schema.setRequired(true);
    }
    // TODO: Try to find a way to move default value interpretation/conversion into the
    // general configuration code.
    String defaultValue = parameterConfig.getDefaultValue();
    if (defaultValue != null) {
        Class<?> parameterClass = type.getRawType();
        try {
            objectMapper.convertValue(defaultValue, parameterClass);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("'%s' is not a valid default value for type '%s'", defaultValue, type));
        }
        schema.setDefault(defaultValue);
    }
    if (isPathParameter) {
        schema.setLocation("path");
    } else {
        schema.setLocation("query");
    }
    if (parameterConfig.getDescription() != null) {
        schema.setDescription(parameterConfig.getDescription());
    }
    return schema;
}
Also used : Description(com.google.api.server.spi.config.Description) RestDescription(com.google.api.services.discovery.model.RestDescription) JsonSchema(com.google.api.services.discovery.model.JsonSchema)

Example 7 with JsonSchema

use of com.google.api.services.discovery.model.JsonSchema 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)

Aggregations

JsonSchema (com.google.api.services.discovery.model.JsonSchema)7 RestDescription (com.google.api.services.discovery.model.RestDescription)3 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)2 Schema (com.google.api.server.spi.config.model.Schema)2 RestMethod (com.google.api.services.discovery.model.RestMethod)2 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 FileContent (com.google.api.client.http.FileContent)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 Description (com.google.api.server.spi.config.Description)1 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)1 ApiNamespaceConfig (com.google.api.server.spi.config.model.ApiNamespaceConfig)1 Field (com.google.api.server.spi.config.model.Schema.Field)1 Discovery (com.google.api.services.discovery.Discovery)1