diff --git a/src/main/java/org/raml/emitter/RamlEmitter.java b/src/main/java/org/raml/emitter/RamlEmitter.java index 8df6d938..8680d2b3 100644 --- a/src/main/java/org/raml/emitter/RamlEmitter.java +++ b/src/main/java/org/raml/emitter/RamlEmitter.java @@ -15,6 +15,8 @@ */ package org.raml.emitter; +import static java.lang.Boolean.FALSE; +import static java.lang.Boolean.TRUE; import static org.apache.commons.lang.StringUtils.isNotEmpty; import static org.raml.parser.utils.ReflectionUtils.isEnum; import static org.raml.parser.utils.ReflectionUtils.isPojo; @@ -29,6 +31,7 @@ import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; +import org.raml.model.ParamType; import org.raml.model.Protocol; import org.raml.model.Raml; import org.raml.model.SecurityReference; @@ -300,7 +303,12 @@ private void dumpScalarField(StringBuilder dump, int depth, Field field, Object } else { - dump.append(sanitizeScalarValue(depth, value)).append("\n"); + if ((isNumber(pojo) || isInteger(pojo) || isBoolean(pojo)) && isDefaultValue(field)) { + //prevent numbers, integer and boolean to be surrounded by quotes + dump.append(String.valueOf(value)).append("\n"); + } else { + dump.append(sanitizeScalarValue(depth, value)).append("\n"); + } } } catch (IllegalAccessException e) @@ -309,6 +317,22 @@ private void dumpScalarField(StringBuilder dump, int depth, Field field, Object } } + private boolean isDefaultValue(Field field) { + return field.getName().equals("defaultValue"); + } + + private boolean isNumber(Object pojo) { + return pojo instanceof AbstractParam && ((AbstractParam) pojo).getType() == ParamType.NUMBER; + } + + private boolean isInteger(Object pojo) { + return pojo instanceof AbstractParam && ((AbstractParam) pojo).getType() == ParamType.INTEGER; + } + + private boolean isBoolean(Object pojo) { + return pojo instanceof AbstractParam && ((AbstractParam) pojo).getType() == ParamType.BOOLEAN; + } + private String alias(Field field) { Scalar scalar = field.getAnnotation(Scalar.class); @@ -329,6 +353,16 @@ else if (sequence != null && isNotEmpty(sequence.alias())) } return field.getName(); } + +// private boolean isBooleanOrInteger(Object value) { +// try { +// Long.valueOf(value.toString()); +// return true; +// } catch (NumberFormatException e) +// { +// return TRUE.toString().equals(value.toString()) || FALSE.toString().equals(value.toString()); +// } +// } private String sanitizeScalarValue(int depth, Object value) { diff --git a/src/test/java/org/raml/EmitterTestCase.java b/src/test/java/org/raml/EmitterTestCase.java index fb2e0c23..41719c0f 100644 --- a/src/test/java/org/raml/EmitterTestCase.java +++ b/src/test/java/org/raml/EmitterTestCase.java @@ -109,10 +109,12 @@ public void emitNumbers() " type: integer\n" + " maximum: 8.0\n" + " minimum: 1\n" + + " default: 3\n" + " number:\n" + " type: number\n" + " maximum: 9.5\n" + - " minimum: 2.0"; + " minimum: 2.0\n" + + " default: 4"; Raml raml = parseRaml(yaml, ""); RamlEmitter emitter = new RamlEmitter(); String dump = emitter.dump(raml); @@ -120,9 +122,28 @@ public void emitNumbers() assertThat(dump, not(containsString("maximum: 8.0"))); assertThat(dump, containsString("minimum: 1")); assertThat(dump, not(containsString("minimum: 1.0"))); + assertThat(dump, containsString("default: 3")); assertThat(dump, containsString("maximum: 9.5")); assertThat(dump, containsString("minimum: 2")); assertThat(dump, not(containsString("minimum: 2.0"))); + assertThat(dump, containsString("default: 4")); + } + + @Test + public void emitBoolean() + { + String yaml = "#%RAML 0.8\n" + + "title: numbers\n" + + "/resource:\n" + + " get:\n" + + " queryParameters:\n" + + " boolean:\n" + + " type: boolean\n" + + " default: true"; + Raml raml = parseRaml(yaml, ""); + RamlEmitter emitter = new RamlEmitter(); + String dump = emitter.dump(raml); + assertThat(dump, containsString("default: true")); } private Raml verifyDump(Raml source, String dump)