Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Fixes #550 prevetn default values of booleans, integers and numbers t…
Browse files Browse the repository at this point in the history
…o be surrounded by quotes when using RamlEmitter
  • Loading branch information
pacafuentes committed Sep 11, 2018
1 parent 28a5a50 commit e43df79
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/main/java/org/raml/emitter/RamlEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -300,7 +303,12 @@ private void dumpScalarField(StringBuilder dump, int depth, Field field, Object
}
else
{
dump.append(sanitizeScalarValue(depth, value)).append("\n");
if (isNumberOrBoolean(pojo) && field.getName().equals("defaultValue")) {
//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)
Expand All @@ -309,6 +317,10 @@ private void dumpScalarField(StringBuilder dump, int depth, Field field, Object
}
}

private boolean isNumberOrBoolean(Object pojo) {
return pojo instanceof AbstractParam && (((AbstractParam) pojo).getType() == ParamType.NUMBER || ((AbstractParam) pojo).getType() == ParamType.INTEGER || ((AbstractParam) pojo).getType() == ParamType.BOOLEAN);
}

private String alias(Field field)
{
Scalar scalar = field.getAnnotation(Scalar.class);
Expand All @@ -329,6 +341,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)
{
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/org/raml/EmitterTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,41 @@ 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);
assertThat(dump, containsString("maximum: 8"));
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)
Expand Down

0 comments on commit e43df79

Please sign in to comment.