Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Npe on missing object values #178

Merged
merged 3 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/main/java/io/swagger/inflector/examples/ExampleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,21 @@ public static Example fromProperty(Property property, Map<String, Model> definit
output = outputExample;
}
} else if (property instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) property;
Property inner = ap.getItems();
if (inner != null) {
Object innerExample = fromProperty(inner, definitions, processedModels);
if (innerExample != null) {
if (innerExample instanceof Example) {
ArrayExample an = new ArrayExample();
an.add((Example) innerExample);
an.setName(property.getName());
output = an;
if (example != null) {
output = new ArrayExample();
}
else {
ArrayProperty ap = (ArrayProperty) property;
Property inner = ap.getItems();
if (inner != null) {
Object innerExample = fromProperty(inner, definitions, processedModels);
if (innerExample != null) {
if (innerExample instanceof Example) {
ArrayExample an = new ArrayExample();
an.add((Example) innerExample);
an.setName(property.getName());
output = an;
}
}
}
}
Expand Down Expand Up @@ -540,7 +545,10 @@ public static void mergeTo(ObjectExample output, List<Example> examples) {
for(Example ex : examples) {
if(ex instanceof ObjectExample) {
ObjectExample objectExample = (ObjectExample) ex;
output.putAll(objectExample.getValues());
Map<String, Example> values = objectExample.getValues();
if( values != null ) {
output.putAll(values);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public void writeTo(XMLStreamWriter writer, Example o) throws XMLStreamException
}

if( o.getNamespace() != null ){
writer.writeStartElement(o.getPrefix(), name, o.getNamespace());
if( o.getPrefix() != null ) {
writer.writeStartElement(o.getPrefix(), name, o.getNamespace());
}
else {
writer.writeStartElement(o.getNamespace(), name );
}
}
else {
writer.writeStartElement(name);
Expand All @@ -82,15 +87,25 @@ public void writeTo(XMLStreamWriter writer, Example o) throws XMLStreamException
if (o.getWrapped() != null && o.getWrapped()) {
if (o.getWrappedName() != null) {
if( o.getNamespace() != null ){
writer.writeStartElement(o.getPrefix(), o.getWrappedName(), o.getNamespace());
if( o.getPrefix() != null ){
writer.writeStartElement(o.getPrefix(), o.getWrappedName(), o.getNamespace());
}
else {
writer.writeStartElement(o.getNamespace(), o.getWrappedName());
}
}
else {
writer.writeStartElement(o.getWrappedName());
}

} else {
if( o.getNamespace() != null ){
writer.writeStartElement(o.getPrefix(), o.getName() + "s", o.getNamespace());
if( o.getPrefix() != null ) {
writer.writeStartElement(o.getPrefix(), o.getName() + "s", o.getNamespace());
}
else {
writer.writeStartElement(o.getNamespace(), o.getName() + "s");
}
}
else {
writer.writeStartElement( o.getName() + "s");
Expand All @@ -106,7 +121,12 @@ public void writeTo(XMLStreamWriter writer, Example o) throws XMLStreamException
}

if( o.getNamespace() != null ) {
writer.writeStartElement(o.getPrefix(), name, o.getNamespace());
if( o.getPrefix() != null ) {
writer.writeStartElement(o.getPrefix(), name, o.getNamespace());
}
else {
writer.writeStartElement(o.getNamespace(), name);
}
}
else {
writer.writeStartElement(name);
Expand All @@ -127,12 +147,28 @@ public void writeTo(XMLStreamWriter writer, Example o) throws XMLStreamException
name = getTypeName(o);
}
if (o.getAttribute() != null && o.getAttribute()) {
writer.writeAttribute(o.getPrefix(), name, o.getNamespace(), o.asString());

if( o.getNamespace() != null ){
if( o.getPrefix() != null ){
writer.writeAttribute( o.getPrefix(), o.getNamespace(), name, o.asString());
}
else {
writer.writeAttribute( o.getNamespace(), name, o.asString());
}
}
else {
writer.writeAttribute(name, o.asString());
}
} else if (name == null) {
writer.writeCharacters(o.asString());
} else {
if( o.getNamespace() != null ){
writer.writeStartElement(o.getPrefix(), name, o.getNamespace());
if( o.getPrefix() != null ) {
writer.writeStartElement(o.getPrefix(), name, o.getNamespace());
}
else {
writer.writeStartElement(o.getNamespace(), name);
}
}
else {
writer.writeStartElement(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.swagger.inflector.examples.models.*;
import io.swagger.inflector.examples.models.ArrayExample;
import io.swagger.inflector.examples.models.BooleanExample;
import io.swagger.inflector.examples.models.DecimalExample;
import io.swagger.inflector.examples.models.DoubleExample;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.examples.models.FloatExample;
import io.swagger.inflector.examples.models.IntegerExample;
import io.swagger.inflector.examples.models.LongExample;
import io.swagger.inflector.examples.models.ObjectExample;
import io.swagger.inflector.examples.models.StringExample;

import java.io.IOException;
import java.util.List;

public class JsonNodeExampleSerializer extends JsonSerializer<Example> {

Expand Down Expand Up @@ -62,7 +72,10 @@ public void writeTo(JsonGenerator jgen, Example o) throws IOException {
}
} else if (o instanceof ArrayExample) {
jgen.writeStartArray();
writeTo(jgen, o);
List<Example> items = ((ArrayExample) o).getItems();
if( !items.isEmpty()){
serialize( items.get(0), jgen, null );
}
jgen.writeEndArray();
} else {
writeValue(jgen, null, o);
Expand Down