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

字段是否必填(可加两行代码) #47

Open
yidiantongtong opened this issue Oct 12, 2020 · 1 comment
Open

字段是否必填(可加两行代码) #47

yidiantongtong opened this issue Oct 12, 2020 · 1 comment

Comments

@yidiantongtong
Copy link

yidiantongtong commented Oct 12, 2020

/**
* 递归生成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");

// 第1行
List required = Optional.ofNullable(((List)swaggerMap.get(modeName).get("required"))).orElse(new ArrayList<>());

    if (modeProperties == null) {
        return null;
    }
    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"));

// 第2行
child.setRequire(required.contains(child.getName()) ? true : false);

        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);
    }
    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;
}
@stan503
Copy link

stan503 commented Mar 25, 2021

思密达提交pr了嘛
required.contains 不友好,换成map?

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

2 participants