@Test
  public void testArrayResponse() {
    Swagger swagger = new Swagger();

    ArrayProperty schema = new ArrayProperty();
    schema.setItems(new ObjectProperty().property("name", new StringProperty()));

    swagger.path(
        "/foo/baz",
        new Path()
            .get(
                new Operation()
                    .response(
                        200,
                        new Response()
                            .vendorExtension("x-foo", "bar")
                            .description("it works!")
                            .schema(schema))));
    new InlineModelResolver().flatten(swagger);

    Response response = swagger.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertTrue(response.getSchema() instanceof ArrayProperty);

    ArrayProperty am = (ArrayProperty) response.getSchema();
    Property items = am.getItems();
    assertTrue(items instanceof ObjectProperty);
    ObjectProperty op = (ObjectProperty) items;
    Property name = op.getProperties().get("name");
    assertTrue(name instanceof StringProperty);
  }
Ejemplo n.º 2
0
  @Override
  public String getTypeDeclaration(Property p) {
    if (p instanceof ArrayProperty) {
      ArrayProperty ap = (ArrayProperty) p;
      Property inner = ap.getItems();
      String innerType = getSwaggerType(inner);

      String innerTypeDeclaration = getTypeDeclaration(inner);

      if (innerTypeDeclaration.endsWith("*")) {
        innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
      }

      // In this codition, type of property p is array of primitive,
      // return container type with pointer, e.g. `NSArray* /* NSString */'
      if (languageSpecificPrimitives.contains(innerType)) {
        return getSwaggerType(p) + "*" + " /* " + innerTypeDeclaration + " */";
      }
      // In this codition, type of property p is array of model,
      // return container type combine inner type with pointer, e.g. `NSArray<SWGTag>*'
      else {
        return getSwaggerType(p) + "<" + innerTypeDeclaration + ">*";
      }
    } else if (p instanceof MapProperty) {
      MapProperty mp = (MapProperty) p;
      Property inner = mp.getAdditionalProperties();

      String innerTypeDeclaration = getTypeDeclaration(inner);

      if (innerTypeDeclaration.endsWith("*")) {
        innerTypeDeclaration = innerTypeDeclaration.substring(0, innerTypeDeclaration.length() - 1);
      }
      return getSwaggerType(p) + "* /* NSString, " + innerTypeDeclaration + " */";
    } else {
      String swaggerType = getSwaggerType(p);

      // In this codition, type of p is objective-c primitive type, e.g. `NSSNumber',
      // return type of p with pointer, e.g. `NSNumber*'
      if (languageSpecificPrimitives.contains(swaggerType)
          && foundationClasses.contains(swaggerType)) {
        return swaggerType + "*";
      }
      // In this codition, type of p is c primitive type, e.g. `bool',
      // return type of p, e.g. `bool'
      else if (languageSpecificPrimitives.contains(swaggerType)) {
        return swaggerType;
      }
      // In this codition, type of p is objective-c object type, e.g. `SWGPet',
      // return type of p with pointer, e.g. `SWGPet*'
      else {
        return swaggerType + "*";
      }
    }
  }
 @Override
 public String getTypeDeclaration(Property p) {
   if (p instanceof ArrayProperty) {
     ArrayProperty ap = (ArrayProperty) p;
     Property inner = ap.getItems();
     return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">";
   } else if (p instanceof MapProperty) {
     MapProperty mp = (MapProperty) p;
     Property inner = mp.getAdditionalProperties();
     return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">";
   }
   return super.getTypeDeclaration(p);
 }
Ejemplo n.º 4
0
 @Override
 public String toInstantiationType(Property p) {
   if (p instanceof MapProperty) {
     MapProperty ap = (MapProperty) p;
     String inner = getSwaggerType(ap.getAdditionalProperties());
     return instantiationTypes.get("map");
   } else if (p instanceof ArrayProperty) {
     ArrayProperty ap = (ArrayProperty) p;
     String inner = getSwaggerType(ap.getItems());
     return instantiationTypes.get("array");
   } else {
     return null;
   }
 }
Ejemplo n.º 5
0
 @Override
 public String getTypeDeclaration(Property p) {
   if (p instanceof ArrayProperty) {
     ArrayProperty ap = (ArrayProperty) p;
     Property inner = ap.getItems();
     return getTypeDeclaration(inner) + "[]";
   } else if (p instanceof MapProperty) {
     MapProperty mp = (MapProperty) p;
     Property inner = mp.getAdditionalProperties();
     return getSwaggerType(p) + "[string," + getTypeDeclaration(inner) + "]";
   } else if (p instanceof RefProperty) {
     String type = super.getTypeDeclaration(p);
     return (!languageSpecificPrimitives.contains(type))
         ? "\\" + modelPackage + "\\" + type
         : type;
   }
   return super.getTypeDeclaration(p);
 }
  @Test
  public void resolveInlineArrayResponse() throws Exception {
    Swagger swagger = new Swagger();

    swagger.path(
        "/foo/baz",
        new Path()
            .get(
                new Operation()
                    .response(
                        200,
                        new Response()
                            .vendorExtension("x-foo", "bar")
                            .description("it works!")
                            .schema(
                                new ArrayProperty()
                                    .items(
                                        new ObjectProperty()
                                            .property("name", new StringProperty()))))));

    new InlineModelResolver().flatten(swagger);

    Response response = swagger.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertNotNull(response);

    assertNotNull(response.getSchema());
    Property responseProperty = response.getSchema();

    // no need to flatten more
    assertTrue(responseProperty instanceof ArrayProperty);

    ArrayProperty ap = (ArrayProperty) responseProperty;
    Property p = ap.getItems();

    assertNotNull(p);

    ObjectProperty innerModel = (ObjectProperty) p;
    assertTrue(innerModel.getProperties().size() == 1);
    assertNotNull(innerModel.getProperties().get("name"));
  }
 @Override
 public String toDefaultValue(Property p) {
   if (p instanceof ArrayProperty) {
     final ArrayProperty ap = (ArrayProperty) p;
     final String pattern;
     if (fullJavaUtil) {
       pattern = "new java.util.ArrayList<%s>()";
     } else {
       pattern = "new ArrayList<%s>()";
     }
     return String.format(pattern, getTypeDeclaration(ap.getItems()));
   } else if (p instanceof MapProperty) {
     final MapProperty ap = (MapProperty) p;
     final String pattern;
     if (fullJavaUtil) {
       pattern = "new java.util.HashMap<String, %s>()";
     } else {
       pattern = "new HashMap<String, %s>()";
     }
     return String.format(pattern, getTypeDeclaration(ap.getAdditionalProperties()));
   } else if (p instanceof IntegerProperty) {
     IntegerProperty dp = (IntegerProperty) p;
     if (dp.getDefault() != null) {
       return dp.getDefault().toString();
     }
     return "null";
   } else if (p instanceof LongProperty) {
     LongProperty dp = (LongProperty) p;
     if (dp.getDefault() != null) {
       return dp.getDefault().toString() + "l";
     }
     return "null";
   } else if (p instanceof DoubleProperty) {
     DoubleProperty dp = (DoubleProperty) p;
     if (dp.getDefault() != null) {
       return dp.getDefault().toString() + "d";
     }
     return "null";
   } else if (p instanceof FloatProperty) {
     FloatProperty dp = (FloatProperty) p;
     if (dp.getDefault() != null) {
       return dp.getDefault().toString() + "f";
     }
     return "null";
   } else if (p instanceof BooleanProperty) {
     BooleanProperty bp = (BooleanProperty) p;
     if (bp.getDefault() != null) {
       return bp.getDefault().toString();
     }
     return "null";
   } else if (p instanceof StringProperty) {
     StringProperty sp = (StringProperty) p;
     if (sp.getDefault() != null) {
       String _default = sp.getDefault();
       if (sp.getEnum() == null) {
         return "\"" + escapeText(_default) + "\"";
       } else {
         // convert to enum var name later in postProcessModels
         return _default;
       }
     }
     return "null";
   }
   return super.toDefaultValue(p);
 }