001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.support.swagger;
017
018import com.alibaba.fastjson.serializer.JSONSerializer;
019import com.alibaba.fastjson.serializer.ObjectSerializer;
020import io.swagger.models.properties.RefProperty;
021
022import java.io.IOException;
023import java.lang.reflect.Type;
024import java.util.HashMap;
025import java.util.Map;
026
027/**
028 * @author lsup
029 * @Description 适配swaggerUI, 解决页面"Unknown Type : ref"问题。
030 */
031public class RefPropertySerializer implements ObjectSerializer {
032
033    private static final String DOLLAR = "$";
034
035    @Override
036    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
037        RefProperty refProperty = (RefProperty) object;
038        Map<String, String> json = new HashMap<String, String>(2);
039        if (refProperty.getType().equalsIgnoreCase(RefProperty.TYPE)) {
040            json.put(DOLLAR + RefProperty.TYPE, refProperty.get$ref());
041        }
042        serializer.write(json);
043    }
044}