public MApiModelBuilder(MComplexType type, String name) { this.type = type; this.name = Strings.isEmpty(name) ? type.getName() : name; this.title = type.getTitle(); this.summary = type.getSummary(); this.description = type.getDescription(); this.entity = type.isEntity(); boolean annotatedOnly = false; if(null != type.getJavaType()) { this.javaTypes.add(type.getJavaType()); ApiModel a = type.getJavaType().getAnnotation(ApiModel.class); if(null != a) { this.name = Strings.firstNotEmpty(a.name(), a.value(), this.name); this.description = Strings.firstNotEmpty(a.desc(), this.description); annotatedOnly = a.explicitProperties(); } } for (MProperty mp : type.getProperties()) { if(annotatedOnly) { BeanProperty bp = mp.getBeanProperty(); if(null != bp && !bp.isAnnotationPresent(ApiProperty.class)) { continue; } } addProperty(new MApiPropertyBuilder(mp)); } }
@Override public MComplexType build() { return new MComplexType(name, title, summary, description, baseType, javaType, Builders.buildList(properties), _abstract, entity); }
protected String modelName(ApiMetadataContext context, MComplexType ct) { if(null != ct.getJavaType()) { ApiModel a = ct.getJavaType().getAnnotation(ApiModel.class); if(null != a) { String name = Strings.firstNotEmpty(a.name(),a.value()); if(!Strings.isEmpty(name)) { return name; } } ModelConfig mc = context.getConfig().getModelConfig(ct.getJavaType()); if(null != mc && !Strings.isEmpty(mc.getName())) { return mc.getName(); } } return ct.getName(); }
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); } } }
MComplexType ct = type.asComplexType(); MApiModel model = null; if(null != ct.getJavaType()) { model = m.tryGetModel(ct.getJavaType()); }else { model = m.tryGetModel(ct.getName()); writeRefType(context, m, w, ct.createTypeRef()); }else { model = new MApiModelBuilder(ct).build();
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); }); }
@Override public MComplexType getComplexType(String name) { for(MComplexType ct : complexTypes.values()) { if(ct.getName().equalsIgnoreCase(name)) { return ct; } } return null; }
return ct.createTypeRef(); return mtype.asComplexType().createTypeRef();
if(m.getModels().containsKey(ct.getName())) { writeRefType(context, m, w, ((MComplexType)type).createTypeRef()); }else { MApiModel model = new MApiModelBuilder(ct).build();
@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; }
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 + "'"); }