Java Code Examples for org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration

The following examples show how to use org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: raml-java-tools   Source File: PojoToRamlImplTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void enumeration() throws Exception {

    PojoToRamlImpl pojoToRaml = new PojoToRamlImpl(FieldClassParser.factory(), AdjusterFactory.NULL_FACTORY);
    Result types =  pojoToRaml.classToRaml(SimpleEnum.class);

    Api api = createApi(types);

    List<TypeDeclaration> buildTypes = api.types();

    assertEquals(1, buildTypes.size());
    assertEquals("SimpleEnum", buildTypes.get(0).name());
    assertArrayEquals(new String[] {"ONE", "TWO"}, ((StringTypeDeclaration) buildTypes.get(0)).enumValues().toArray(new String[0]));

    Emitter emitter = new Emitter();
    emitter.emit(api);
}
 
Example 2
Source Project: raml-java-tools   Source File: UnionTypesHelper.java    License: Apache License 2.0 6 votes vote down vote up
private static Map<Class<? extends TypeDeclaration>, Integer> getPriorityTypeMap() {
    return new ImmutableMap.Builder<Class<? extends TypeDeclaration>, Integer>()
        .put(NullTypeDeclaration.class, 1)
        .put(BooleanTypeDeclaration.class, 2)
        .put(IntegerTypeDeclaration.class, 3)
        .put(NumberTypeDeclaration.class, 4)
        .put(DateTypeDeclaration.class, 5)
        .put(TimeOnlyTypeDeclaration.class, 6)
        .put(DateTimeOnlyTypeDeclaration.class, 7)
        .put(DateTimeTypeDeclaration.class, 8)
        .put(StringTypeDeclaration.class, 9)
        .put(ObjectTypeDeclaration.class, 10)
        .put(ArrayTypeDeclaration.class, 11)
        .put(UnionTypeDeclaration.class, 12)
        .put(FileTypeDeclaration.class, 13)
        .put(AnyTypeDeclaration.class, 14)
        .build();
}
 
Example 3
Source Project: raml-java-tools   Source File: EnumerationTypeHandler.java    License: Apache License 2.0 5 votes vote down vote up
List pullEnumValues(TypeDeclaration typeDeclaration) {

        if ( typeDeclaration instanceof  IntegerTypeDeclaration ) {
            return ((IntegerTypeDeclaration)typeDeclaration).enumValues();
        } else  if (typeDeclaration instanceof NumberTypeDeclaration) {
            return ((NumberTypeDeclaration)typeDeclaration).enumValues();
        } else {
            return ((StringTypeDeclaration)typeDeclaration).enumValues();
        }
    }
 
Example 4
@Override
public RamlInterpretationResult interpret(RamlRoot document, TypeDeclaration type, JCodeModel builderModel, boolean property) {
	RamlInterpretationResult result = new RamlInterpretationResult(type.required());

	if (type instanceof StringTypeDeclaration) {
		StringTypeDeclaration stringType = (StringTypeDeclaration) type;
		// do stringy stuff - enums and stuff.
		RamlTypeValidations validations = result.getValidations();
		validations.withPattern(stringType.pattern());
		validations.withLenghts(stringType.minLength(), stringType.maxLength());

		// Create and handle Enums here
		if (stringType.enumValues() != null && !stringType.enumValues().isEmpty()) {
			// We have an enum. we need to create it and set it
			String enumName = stringType.type();
			if ("string".equalsIgnoreCase(enumName)) {
				enumName = stringType.name();
			}
			if ("string".equalsIgnoreCase(enumName) || enumName.contains("/")) {
				// enumName is either a string or media type
				enumName = DEFAULT_ENUM_NAME;
			}
			EnumBuilder builder = new EnumBuilder(builderModel, enumName);
			builder.withEnums(stringType.enumValues(), String.class);
			result.setBuilder(builder);
			result.setCodeModel(builderModel);
		}

	}
	if (result.getBuilder() == null) {
		result.setResolvedClass(CodeModelHelper.findFirstClassBySimpleName(builderModel, "java.lang.String"));
	}

	return result;
}
 
Example 5
@Override
public String getRawType() {
	if (queryParameter instanceof StringTypeDeclaration && "string".equalsIgnoreCase(queryParameter.type())) {
		List<String> items = ((StringTypeDeclaration) queryParameter).enumValues();
		if (!items.isEmpty()) {
			return NamingHelper.convertToClassName(queryParameter.name());
		}
	}
	return queryParameter.type();
}
 
Example 6
@Override
public Integer getMinLength() {
	if (queryParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) queryParameter).minLength();
	}
	return null;
}
 
Example 7
@Override
public Integer getMaxLength() {
	if (queryParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) queryParameter).maxLength();
	}
	return null;
}
 
Example 8
@Override
public String getPattern() {
	if (queryParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) queryParameter).pattern();
	}
	return null;
}
 
Example 9
@Override
public String getPattern() {
	if (uriParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) uriParameter).pattern();
	}
	return null;
}
 
Example 10
@Override
public Integer getMinLength() {
	if (uriParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) uriParameter).minLength();
	}
	return null;
}
 
Example 11
@Override
public Integer getMaxLength() {
	if (uriParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) uriParameter).maxLength();
	}
	return null;
}
 
Example 12
@Override
public String getPattern() {
	if (formParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) formParameter).pattern();
	}
	return null;
}
 
Example 13
@Override
public Integer getMinLength() {
	if (formParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) formParameter).minLength();
	}
	return null;
}
 
Example 14
@Override
public Integer getMaxLength() {
	if (formParameter instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) formParameter).maxLength();
	}
	return null;
}
 
Example 15
Source Project: springmvc-raml-plugin   Source File: RJP10V2RamlHeader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String getPattern() {
	if (header instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) header).pattern();
	}
	return null;
}
 
Example 16
Source Project: springmvc-raml-plugin   Source File: RJP10V2RamlHeader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Integer getMinLength() {
	if (header instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) header).minLength();
	}
	return null;
}
 
Example 17
Source Project: springmvc-raml-plugin   Source File: RJP10V2RamlHeader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Integer getMaxLength() {
	if (header instanceof StringTypeDeclaration) {
		return ((StringTypeDeclaration) header).maxLength();
	}
	return null;
}
 
Example 18
Source Project: springmvc-raml-plugin   Source File: RJP10V2RamlRootTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void ramlRootShouldReflectDataTypes() {
	Map<String, RamlDataType> personType = ramlRoot.getTypes();

	ObjectTypeDeclaration personDataType = (ObjectTypeDeclaration) personType.get("Person").getType();

	assertThat(personDataType.displayName().value(), equalTo("Person"));
	assertThat(personDataType.type(), equalTo("object"));

	StringTypeDeclaration testXProp = (StringTypeDeclaration) personDataType.properties().get(0);
	assertThat(testXProp.displayName().value(), equalTo("testX"));
	assertThat(testXProp.defaultValue(), equalTo("def_value"));
	assertThat(testXProp.minLength(), equalTo(3));
	assertThat(testXProp.maxLength(), equalTo(10));

	ArrayTypeDeclaration testY = (ArrayTypeDeclaration) personDataType.properties().get(1);
	assertThat(testY.description().value(), equalTo("array attribute"));
	assertThat(testY.example().value(), endsWith("[\n\"a\",\n\"b\"\n]"));
	assertThat(testY.items().type(), equalTo("string"));
	assertThat(testY.minItems(), equalTo(2));
	assertThat(testY.maxItems(), equalTo(5));

	ObjectTypeDeclaration managerDataType = (ObjectTypeDeclaration) personType.get("Manager").getType();
	assertThat(managerDataType.type(), equalTo("Person"));

	ArrayTypeDeclaration personsDataType = (ArrayTypeDeclaration) personType.get("Persons").getType();
	assertThat(personsDataType.type(), equalTo("array"));
	assertThat(personsDataType.items().type(), equalTo("Person"));
}
 
Example 19
Source Project: springmvc-raml-plugin   Source File: RJP10V2RamlRootTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void ramlRootShouldReflectDataTypesFromLibraries() {
	Map<String, RamlDataType> dataTypes = ramlRoot.getTypes();

	ObjectTypeDeclaration personDataType = (ObjectTypeDeclaration) dataTypes.get("Song").getType();

	assertThat(personDataType.displayName().value(), equalTo("Song"));
	assertThat(personDataType.type(), equalTo("object"));

	StringTypeDeclaration title = (StringTypeDeclaration) personDataType.properties().get(0);
	assertThat(title.displayName().value(), equalTo("title"));
	assertThat(title.example().value(), equalTo("Smells Like Teen Spirit"));
	assertThat(title.minLength(), equalTo(5));
	assertThat(title.maxLength(), equalTo(999));

	StringTypeDeclaration artist = (StringTypeDeclaration) personDataType.properties().get(1);
	assertThat(artist.name(), equalTo("artist"));
	assertThat(artist.required(), equalTo(true));

	ObjectTypeDeclaration album = (ObjectTypeDeclaration) personDataType.properties().get(2);
	assertThat(album.name(), equalTo("album"));
	assertThat(album.required(), equalTo(false));

	IntegerTypeDeclaration year = (IntegerTypeDeclaration) personDataType.properties().get(3);
	assertThat(year.name(), equalTo("year"));
	assertThat(year.example().value(), equalTo("1991"));
	assertThat(year.required(), equalTo(false));
}
 
Example 20
Source Project: springmvc-raml-plugin   Source File: RJP10V2RamlRootTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void ramlRootShouldReflectDataTypesFromAllLibraries() {
	Map<String, RamlDataType> dataTypes = ramlRoot.getTypes();

	// this data type is defined in library that is referenced in another
	// library (and not in a root raml)
	ObjectTypeDeclaration albumDataType = (ObjectTypeDeclaration) dataTypes.get("Album").getType();

	assertThat(albumDataType.displayName().value(), equalTo("Album"));
	assertThat(albumDataType.type(), equalTo("object"));

	StringTypeDeclaration title = (StringTypeDeclaration) albumDataType.properties().get(0);
	assertThat(title.displayName().value(), equalTo("title"));
	assertThat(title.example().value(), equalTo("Nevermind"));
	assertThat(title.minLength(), equalTo(1));
	assertThat(title.maxLength(), equalTo(999));

	StringTypeDeclaration artist = (StringTypeDeclaration) albumDataType.properties().get(1);
	assertThat(artist.name(), equalTo("artist"));
	assertThat(artist.required(), equalTo(true));

	StringTypeDeclaration studio = (StringTypeDeclaration) albumDataType.properties().get(2);
	assertThat(studio.name(), equalTo("studio"));
	assertThat(studio.required(), equalTo(false));

	IntegerTypeDeclaration year = (IntegerTypeDeclaration) albumDataType.properties().get(3);
	assertThat(year.name(), equalTo("year"));
	assertThat(year.example().value(), equalTo("1991"));
	assertThat(year.required(), equalTo(false));
}
 
Example 21
Source Project: raml-java-tools   Source File: EnumerationTypeHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Optional<CreationResult> create(GenerationContext generationContext, CreationResult preCreationResult) {

    Class cls = (typeDeclaration instanceof StringTypeDeclaration)?String.class:Number.class;

    FieldSpec.Builder field = FieldSpec.builder(ClassName.get(cls), "name").addModifiers(Modifier.PROTECTED, Modifier.FINAL);
    EnumerationPluginContext enumerationPluginContext = new EnumerationPluginContextImpl(generationContext, preCreationResult);

    ClassName className = preCreationResult.getJavaName(EventType.INTERFACE);

    TypeSpec.Builder enumBuilder = TypeSpec.enumBuilder(className);

    enumBuilder.addField(field.build())
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .addMethod(
                    MethodSpec.constructorBuilder().addParameter(ClassName.get(cls), "name")
                            .addStatement("this.$N = $N", "name", "name")
                            .build()
            );
    enumBuilder = generationContext.pluginsForEnumerations(typeDeclaration).classCreated(enumerationPluginContext, typeDeclaration, enumBuilder, EventType.INTERFACE);
    if ( enumBuilder == null ) {
        return Optional.absent();
    }

    for (Object value : pullEnumValues(typeDeclaration)) {
        TypeSpec.Builder enumValueBuilder;
        if ( value instanceof String) {
            enumValueBuilder= TypeSpec.anonymousClassBuilder("$S", value);
            enumValueBuilder = generationContext.pluginsForEnumerations(typeDeclaration).enumValue(enumerationPluginContext, typeDeclaration, enumValueBuilder, (String)value, EventType.INTERFACE);
        } else {

            enumValueBuilder= TypeSpec.anonymousClassBuilder("$L", value);
            enumValueBuilder = generationContext.pluginsForEnumerations(typeDeclaration).enumValue(enumerationPluginContext, typeDeclaration, enumValueBuilder, (Number)value, EventType.INTERFACE);
        }
        if ( enumValueBuilder == null ) {
            continue;
        }

        enumBuilder.addEnumConstant(Names.constantName(String.valueOf(value)),
                enumValueBuilder.build());

    }

    return Optional.of(preCreationResult.withInterface(enumBuilder.build()));
}
 
Example 22
Source Project: raml-java-tools   Source File: Main.java    License: Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        Api api = document()
                .baseUri("http:fun.com/fun")
                .title("Hello!")
                .version("1.0beta6")
                .withTypes(

                        TypeDeclarationBuilder.typeDeclaration("Foo").ofType(
                                TypeBuilder.type("object")
                                        .withFacets(FacetBuilder.facet("required").ofType("boolean"))
                                        .withAnnotations(AnnotationBuilder.annotation("Foo")
                                                .withProperties(PropertyValueBuilder.property("time", "2022-02-02"), PropertyValueBuilder.propertyOfArray("count", 1,2)))
                        ),
                        TypeDeclarationBuilder.typeDeclaration("EnumFoo").ofType(TypeBuilder.type().enumValues("UN", "DEUX")),
                        TypeDeclarationBuilder.typeDeclaration("EnumNum").ofType(TypeBuilder.type("integer").enumValues(1,2)),

                        TypeDeclarationBuilder.typeDeclaration("Goo").ofType(TypeBuilder.type("object")),
                        TypeDeclarationBuilder.typeDeclaration("GooWithExamples").ofType(TypeBuilder.type("object")
                                .withProperty(TypePropertyBuilder.property("count", "integer"),TypePropertyBuilder.property("realType", "Foo"))
                                .withExamples(ExamplesBuilder.example("one").withPropertyValue(PropertyValueBuilder.property("count", 1)))
                        ),
                        TypeDeclarationBuilder.typeDeclaration("GooWithExample").ofType(TypeBuilder.type("object")
                                .withProperty(
                                        TypePropertyBuilder.property("count", "integer"),
                                        TypePropertyBuilder.property("counts", TypeBuilder.arrayOf(TypeBuilder.type("string"))),
                                        TypePropertyBuilder.property("realType", "Foo"))
                                .withExample(ExamplesBuilder.singleExample().strict(false).withPropertyValue(PropertyValueBuilder.property("count", 1)))
                        )


                )

                .withAnnotationTypes(
                        AnnotationTypeBuilder.annotationType("Foo").withProperty(property("time", "date-only")).withProperty(property("count", "integer[]"))
                ).buildModel();
/*                .withResources(
                        resource("/no")
                                .description("fooo!!!")
                                .displayName("Mama!!!")
                                .with(
                                        method("get")
                                                .description("fooofooofooo")
                                                .withQueryParameter(ParameterBuilder.parameter("apaaa").ofType("integer"))
                                                .withAnnotations(AnnotationBuilder.annotation("Foo").withProperties(
                                                        PropertyValueBuilder.property("time", "2022-02-02"),
                                                        PropertyValueBuilder.propertyOfArray("count", 7)))
                                                .withBodies(
                                                        BodyBuilder.body("application/json")
                                                                .ofType(TypeBuilder.type("Foo", "Goo")
                                                                        .withProperty(TypePropertyBuilder.property("foo", "string"))
                                                                )
                                                ).withResponses(response(200))
                                )
*/
//                ).buildModel();

        StringTypeDeclaration stdzero = (StringTypeDeclaration) api.types().get(0);
        System.err.println(stdzero.enumValues());

        System.err.println(api.types().get(0).name());
        System.err.println();
        Emitter emitter = new Emitter();
        emitter.emit(api);

        StringWriter writer = new StringWriter();
        emitter.emit(api, writer);

        RamlModelResult re_read = new RamlModelBuilder().buildApi(new StringReader(writer.toString()), ".");
        if (re_read.hasErrors()) {
            for (ValidationResult validationResult : re_read.getValidationResults()) {
                System.err.println(validationResult);
            }
        }

        StringTypeDeclaration std = (StringTypeDeclaration) re_read.getApiV10().types().get(0);
        System.err.println(std.enumValues());

        ObjectTypeDeclaration third = (ObjectTypeDeclaration) re_read.getApiV10().types().get(3);
        System.err.println(third.properties().get(0).name());
        System.err.println(third.properties().get(0).type());
        System.err.println(third.properties().get(1).name());
        System.err.println(third.properties().get(1).type());
        System.err.println(third.properties().get(1).parentTypes().get(0).type());
    }
 
Example 23
@Override
public Set<Class<? extends TypeDeclaration>> getSupportedTypes() {
	return Collections.singleton(StringTypeDeclaration.class);
}