public static String toTypeName(MType type) { if(type.isSimpleType()) { SwaggerType st = swaggerWriter.convertSimpleType(type.asSimpleType()); if(null == st) { throw new IllegalStateException("Unsupported type '" + type + "'"); } return st.name().toLowerCase(); } if(type.isCollectionType()) { MType elementType = type.asCollectionType().getElementType(); return "array<" + toTypeName(elementType) + ">"; } if(type.isComplexType()) { return type.asComplexType().getName(); } if(type.isTypeRef()) { return type.asTypeRef().getRefTypeName(); } if(type.isDictionaryType()) { return "map<string," + toTypeName(type.asDictionaryType().getValueType()) + ">"; } if(type.isObjectType()) { return "object"; } if(type.isVoidType()) { return ""; } throw new IllegalStateException("Unsupported type '" + type + "'"); }
/** * Returns <code>true</code> if this type is a complex type of reference to a complex type. */ public boolean isComplexTypeOrRef() { return isComplexType() || (isTypeRef() && asTypeRef().getRefTypeKind() == MTypeKind.COMPLEX); }
Class<?> toJavaType(MType type) { if(type.isSimpleType()) { return type.asSimpleType().getJavaType(); } if(type.isCollectionType()) { return Array.newInstance(toJavaType(type.asCollectionType().getElementType()), 0).getClass(); } if(type.isComplexType() || type.isDictionaryType() || type.isTypeRef()) { return Map.class; } if(type.isObjectType()) { return Object.class; } throw new IllegalStateException("Unsupported data type '" + type + "'"); }
protected boolean isBodyType(MType type) { if(type.isComplexType()) { return true; } if(type.isTypeRef()) { return true; } if(type.isDictionaryType()) { return true; } if(type.isCollectionType()) { MType elementType = type.asCollectionType().getElementType(); return isBodyType(elementType); } return false; }
public static Validator of(MetaApi api, String name, boolean required, MType type, @Nullable MetaObjFormatted p) { if(type.isSimpleType()) { SimpleValidator validator = null; if(null != p) { return validator; }else { return new TypeValidator(name, required, type.asSimpleType(), validator); if(type.isComplexTypeOrRef()) { Boolean partial = null; if(type instanceof MComplexTypeRef) { MetaEntity entity = api.getEntity(type.getName()); if(null != entity) { return new EntityValidator(name, required, api, entity, partial, true); MetaModel model = api.getModel(type.getName()); if(null != model) { return new ModelValidator(name, required, api, model, partial, true); if(type.isComplexType()) { model = MetaUtils.toMetaModel(type.asComplexType()); return new ModelValidator(name, required, api, model, partial, true); if(type.isCollectionType()) { return ofArray(api, name, required, type.asCollectionType(), p); if(type.isDictionaryType()) { return ofMap(api, name, required, type.asDictionaryType());
private void onTypeResolved(MType mtype) { if (mtype.isComplexType()) { MComplexType ct = mtype.asComplexType(); if (!complexTypes.containsKey(ct.getJavaType())) { listener.onComplexTypeResolved(ct.getJavaType(), mtype.asComplexType()); complexTypes.put(ct.getJavaType(), ct); } for (MProperty p : ct.getProperties()) { onTypeResolved(p.getType()); } } else if (mtype.isCollectionType()) { MType elementType = mtype.asCollectionType().getElementType(); onTypeResolved(elementType); } } }
private void checkType(MetaApi api, MetaModel model, MetaProperty p, MType type) { if (type.isTypeRef()) { String name = type.asTypeRef().getRefTypeName(); if (null == api.getEntity(name) && null == api.getModel(name)) { if (Strings.containsIgnoreCase(name, "definitions/")) { throw new IllegalStateException("The ref type + '" + name + "' at '" + model.getName() + "#" + p.getName() + "' not exists, should use '#/definitions/' prefix!"); } else { throw new IllegalStateException("The ref type + '" + name + "' at '" + model.getName() + "#" + p.getName() + "' not exists!"); } } } else if (type.isCollectionType()) { checkType(api, model, p, type.asCollectionType().getElementType()); } }
@Override public Object apply(ActionParams params) { Map<String,Object> map = params.toMap(); Object result; if(command.getMetadata().isSelect()) { //todo: page query, total count Query query = dao.createQuery(command).params(map); if(null != returnType) { if(returnType.isSimpleType()) { result = Converts.convert(query.scalarValueOrNull(), returnType.asSimpleType().getJavaType()); }else if(returnType.isCollectionType() && returnType.asCollectionType().getElementType().isSimpleType()) { result = query.scalars().list(returnType.asCollectionType().getElementType().asSimpleType().getJavaType()); }else { result = query.list(); } }else { result = query.list(); } }else{ //todo: the return type must be simple type result = dao.executeUpdate(command, map); if(null != returnType) { result = Converts.convert(result, returnType.asSimpleType().getJavaType()); } } return ApiResponse.of(result); }
private static boolean hasUnresolvedType(MType type) { if(type instanceof MUnresolvedTypeRef) { return true; } if(type.isCollectionType()) { return hasUnresolvedType(type.asCollectionType().getElementType()); } if(type.isDictionaryType()) { return hasUnresolvedType(type.asDictionaryType().getValueType()); } return false; }
/** * Returns the referenced type name. */ public String getRefTypeName() { return type.isCollectionType() ? type.asCollectionType().getElementType().asTypeRef().getRefTypeName() : type.asTypeRef().getRefTypeName(); }
@Override public Object apply(ActionParams params) { try { Map<String, Object> map = params.toMap(); engine.setContextVariable(PARAMS, Collections.unmodifiableMap(map)); Object value = JsConvert.convert(module.eval(map)); log.debug("Result : {}", value); if (returnTypeDefined && null != value && returnType.isSimpleType()) { value = Converts.convert(value, returnType.asSimpleType().getJavaType()); } return value; }catch (RuntimeException e) { throw e; }catch (Exception e) { throw new RuntimeException(e.getMessage(), e); }finally { engine.removeContextVariable(PARAMS); } }
if (mtype.isComplexType()) { CACHED_COMPLEX_TYPES.put(type, mtype.asComplexType()); onTypeResolved(mtype); if(alwaysReturnComplexTypeRef && mtype.isComplexType()) { return mtype.asComplexType().createTypeRef();
protected boolean setDefaultLocation(RouteBuilder route, MApiParameterBuilder p) { if(null == p.getLocation()) { if (route.getPathTemplate().getTemplateVariables().contains(p.getName())) { p.setLocation(MApiParameter.Location.PATH); return true; } if(p.getType().isComplexType() || p.getType().isTypeRef() || p.getType().isDictionaryType()) { p.setLocation(MApiParameter.Location.BODY); return true; } p.setLocation(MApiParameter.Location.QUERY); return true; } return false; }
if(null == from || from == Location.UNDEFINED) { if(p.getType().isTypeRef() || p.getType().isCollectionType()) { return MApiParameter.Location.BODY; }else{
private static MType doResolveType(MetaApi api, MType type) { if(type instanceof MUnresolvedTypeRef) { return resolveType(api, ((MUnresolvedTypeRef) type).getRefTypeName()); } if(type instanceof MCollectionType) { return new MCollectionType(doResolveType(api, type.asCollectionType().getElementType())); } if(type instanceof MDictionaryType) { return new MDictionaryType(type.asDictionaryType().getKeyType(), doResolveType(api, type.asDictionaryType().getValueType())); } throw new IllegalStateException("Unsupported type '" + type + "'"); }
@Override public MApiModelBuilder tryAddModel(ApiMetadataContext context, ApiMetadataBuilder md, MComplexType ct) { String name = modelName(context, ct); MApiModelBuilder model = md.tryGetModel(name); if(null != model) { if(null != ct.getJavaType() && !model.getJavaTypes().contains(ct.getJavaType())) { model.addJavaType(ct.getJavaType()); } return null; } model = new MApiModelBuilder(ct, name); model.getProperties().values().forEach(p -> { if(p.getType().isComplexType()) { MComplexType complexType = (MComplexType)p.getType(); if(null == md.tryGetModel(complexType.getName())) { tryAddModel(context, md, ct); } } }); md.addModel(model); return model; }
if(!fm.getDataType().isSimpleType()) { String serializeFormat = context.getOrmContext().getConfig().getDefaultSerializer(); FieldSerializer serializer = factory.tryGetBean(FieldSerializer.class, serializeFormat);
protected void resolveArgumentType(RouteBuilder route, MApiParameterBuilder p, ArgumentBuilder arg) { MType type = p.getType(); if (type.isComplexType() || type.isTypeRef()) { arg.setType(Map.class); return; if(type.isSimpleType()) { arg.setType(type.asSimpleType().getJavaType()); return; if(type.isObjectType()) { arg.setType(Object.class); return; if(type.isDictionaryType()) { arg.setType(Map.class); return; if(type.isCollectionType()) { MType elementType = type.asCollectionType().getElementType(); if(elementType.isSimpleType()) { arg.setType(Array.newInstance(elementType.asSimpleType().getJavaType(),0).getClass()); }else if(elementType.isObjectType()){ arg.setType(Object[].class); }else {
if(!p.getType().isSimpleType()){ return; MSimpleType type = p.getType().asSimpleType();
protected void createResponses(ApiMetadataContext context, ApiMetadataBuilder m) { ApiConfig c = context.getConfig(); c.getCommonResponses().forEach((name, r) -> { MType type = r.getType(); if(type.isComplexType()) { MComplexType ct = type.asComplexType(); if(!m.containsModel(ct.getName())) { tryAddModel(context, m, ct); } type = ct.createTypeRef(); } MApiResponseBuilder rb = new MApiResponseBuilder(r); rb.setType(type); m.putResponse(name, rb); }); }