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

请求对象body里面的参数没有识别出必填和非必填 #62

Open
meredith-zeng opened this issue Sep 6, 2021 · 0 comments
Open

Comments

@meredith-zeng
Copy link

meredith-zeng commented Sep 6, 2021

建议修改方案:
WordServiceImpl类中修改以下两个方法:

/**
* 递归生成ModelAttr
* 对$ref类型设置具体属性
*/
private ModelAttr getAndPutModelAttr(Map<String, Map<String, Object>> swaggerMap, Map<String, ModelAttr> resMap, String modeName) {
ModelAttr modeAttr;
if ((modeAttr = resMap.get("#/definitions/" + modeName)) == null) {
modeAttr = new ModelAttr();
resMap.put("#/definitions/" + modeName, modeAttr);
} else if (modeAttr.isCompleted()) {
return resMap.get("#/definitions/" + modeName);
}
Map<String, Object> modeProperties = (Map<String, Object>) swaggerMap.get(modeName).get("properties");
if (modeProperties == null) {
return null;
}

    // 第一步:判断对象req是否有必填字段
    if (swaggerMap.get(modeName).get("required") != null){
        //第二步:获取到需要添加require字段为true的对应的modeProperties的key
        ArrayList<String> list = (ArrayList<String>) swaggerMap.get(modeName).get("required");
        //第三步:遍历modeProperties,给需要添加require的properties添加require字段并置为true
        for (int i = 0; i < list.size(); i++){
            Map<String, Object> properties = (Map<String, Object>) modeProperties.get(list.get(i));
            properties.put("require",true);
            modeProperties.put(list.get(i),properties);
        }
    }





    List<ModelAttr> attrList = getModelAttrs(swaggerMap, resMap, modeAttr, modeProperties);
    List allOf = (List) swaggerMap.get(modeName).get("allOf");
    if(allOf!=null){
        for (int i = 0; i < allOf.size(); i++) {
            Map c = (Map) allOf.get(i);
            if(c.get("$ref")!=null){
                String refName = c.get("$ref").toString();
                //截取 #/definitions/ 后面的
                String clsName = refName.substring(14);
                Map<String, Object> modeProperties1 = (Map<String, Object>) swaggerMap.get(clsName).get("properties");
                List<ModelAttr> attrList1 = getModelAttrs(swaggerMap, resMap, modeAttr, modeProperties1);
                if(attrList1!=null && attrList!=null){
                    attrList.addAll(attrList1);
                }else if(attrList==null && attrList1!=null){
                    attrList = attrList1;
                }
            }
        }
    }

    Object title = swaggerMap.get(modeName).get("title");
    Object description = swaggerMap.get(modeName).get("description");
    modeAttr.setClassName(title == null ? "" : title.toString());
    modeAttr.setDescription(description == null ? "" : description.toString());
    modeAttr.setProperties(attrList);
    return modeAttr;
}

private List<ModelAttr> getModelAttrs(Map<String, Map<String, Object>> swaggerMap, Map<String, ModelAttr> resMap, ModelAttr modeAttr, Map<String, Object> modeProperties) {
    Iterator<Entry<String, Object>> mIt = modeProperties.entrySet().iterator();

    List<ModelAttr> attrList = new ArrayList<>();

    //解析属性
    while (mIt.hasNext()) {
        Entry<String, Object> mEntry = mIt.next();
        Map<String, Object> attrInfoMap = (Map<String, Object>) mEntry.getValue();
        ModelAttr child = new ModelAttr();
        child.setName(mEntry.getKey());
        child.setType((String) attrInfoMap.get("type"));
        if (attrInfoMap.get("format") != null) {
            child.setType(child.getType() + "(" + attrInfoMap.get("format") + ")");
        }
        child.setType(StringUtils.defaultIfBlank(child.getType(), "object"));






        //meredith-zeng
        //如果attreInfoMap中有require字段,则将require字段设置为true
        if (attrInfoMap.get("require") != null){
            child.setRequire(true);
        }





        Object ref = attrInfoMap.get("$ref");
        Object items = attrInfoMap.get("items");
        if (ref != null || (items != null && (ref = ((Map) items).get("$ref")) != null)) {
            String refName = ref.toString();
            //截取 #/definitions/ 后面的
            String clsName = refName.substring(14);
            modeAttr.setCompleted(true);
            ModelAttr refModel = getAndPutModelAttr(swaggerMap, resMap, clsName);
            if (refModel != null) {
                child.setProperties(refModel.getProperties());
            }
            child.setType(child.getType() + ":" + clsName);
        }
        child.setDescription((String) attrInfoMap.get("description"));
        attrList.add(child);
    }
    return attrList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant