All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.msuno.swagger.spring.boot.autoconfigure.mappers.ServiceModelToSwagger2Mapper Maven / Gradle / Ivy

There is a newer version: 2.5.2
Show newest version
/*
 *
 *  Copyright 2015 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package cn.msuno.swagger.spring.boot.autoconfigure.mappers;

import static cn.msuno.swagger.spring.boot.autoconfigure.mappers.ModelMapper.modelRefToProperty;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.EntryTransformer;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Maps.newTreeMap;
import static com.google.common.collect.Maps.transformEntries;
import static springfox.documentation.builders.BuilderDefaults.nullToEmptyList;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

import io.swagger.models.Contact;
import io.swagger.models.Info;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Scheme;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
import io.swagger.models.properties.Property;
import springfox.documentation.schema.ModelReference;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiListing;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Documentation;
import springfox.documentation.service.Header;
import springfox.documentation.service.ResponseMessage;

@Mapper(uses = {
    ModelMapper.class,
    ParameterMapper.class,
    SecurityMapper.class,
    LicenseMapper.class,
    VendorExtensionsMapper.class
})
public abstract class ServiceModelToSwagger2Mapper {

  @Mappings({
      @Mapping(target = "info", source = "resourceListing.info"),
      @Mapping(target = "paths", source = "apiListings"),
      @Mapping(target = "host", source = "host"),
      @Mapping(target = "schemes", source = "schemes"),
      @Mapping(target = "definitions", source = "apiListings"),
      @Mapping(target = "securityDefinitions", source = "resourceListing"),
      @Mapping(target = "securityRequirement", ignore = true),
      @Mapping(target = "security", ignore = true),
      @Mapping(target = "swagger", ignore = true),
      @Mapping(target = "parameters", ignore = true),
      @Mapping(target = "responses", ignore = true),
      @Mapping(target = "externalDocs", ignore = true),
      @Mapping(target = "vendorExtensions", source = "vendorExtensions")
  })
  public abstract Swagger mapDocumentation(Documentation from);

  @Mappings({
      @Mapping(target = "license", source = "from",
          qualifiedBy = { LicenseMapper.LicenseTranslator.class, LicenseMapper.License.class }),
      @Mapping(target = "contact", source = "from.contact"),
      @Mapping(target = "termsOfService", source = "termsOfServiceUrl"),
      @Mapping(target = "vendorExtensions", source = "vendorExtensions")
  })
  protected abstract Info mapApiInfo(ApiInfo from);

  protected abstract Contact map(springfox.documentation.service.Contact from);

  @Mappings({
      @Mapping(target = "description", source = "notes"),
      @Mapping(target = "operationId", source = "uniqueId"),
      @Mapping(target = "schemes", source = "protocol"),
      @Mapping(target = "security", source = "securityReferences"),
      @Mapping(target = "responses", source = "responseMessages"),
      @Mapping(target = "vendorExtensions", source = "vendorExtensions"),
      @Mapping(target = "externalDocs", ignore = true)
  })
  protected abstract Operation mapOperation(springfox.documentation.service.Operation from);

  @Mappings({
      @Mapping(target = "externalDocs", ignore = true),
      @Mapping(target = "vendorExtensions", source = "vendorExtensions")
  })
  protected abstract Tag mapTag(springfox.documentation.service.Tag from);

  protected List mapSchemes(List from) {
    return from(from).transform(toScheme()).toList();
  }

  protected List>> mapAuthorizations(
      Map> from) {
    List>> security = newArrayList();
    for (Map.Entry> each : from.entrySet()) {
      Map> newEntry = newHashMap();
      newEntry.put(each.getKey(), from(each.getValue()).transform(scopeToString()).toList());
      security.add(newEntry);
    }
    return security;
  }

  protected Map mapResponseMessages(Set from) {
    Map responses = newTreeMap();
    for (ResponseMessage responseMessage : from) {
      Property responseProperty;
      ModelReference modelRef = responseMessage.getResponseModel();
      responseProperty = modelRefToProperty(modelRef);
      Response response = new Response()
          .description(responseMessage.getMessage())
          .schema(responseProperty);
      response.setExamples(Maps.newHashMap());
      response.setHeaders(transformEntries(responseMessage.getHeaders(), toPropertyEntry()));
      Map extensions = new VendorExtensionsMapper()
          .mapExtensions(responseMessage.getVendorExtensions());
      response.getVendorExtensions().putAll(extensions);
      responses.put(String.valueOf(responseMessage.getCode()), response);
    }
    return responses;
  }

  private EntryTransformer toPropertyEntry() {
    return new EntryTransformer() {
      @Override
      public Property transformEntry(String key, Header value) {
        Property property = modelRefToProperty(value.getModelReference());
        property.setDescription(value.getDescription());
        return property;
      }
    };
  }

  protected Map mapApiListings(Multimap apiListings) {
    Map paths = newTreeMap();
    for (ApiListing each : apiListings.values()) {
      for (ApiDescription api : each.getApis()) {
        paths.put(api.getPath(), mapOperations(api, Optional.fromNullable(paths.get(api.getPath()))));
      }
    }
    return paths;
  }

  private Function scopeToString() {
    return new Function() {
      @Override
      public String apply(AuthorizationScope input) {
        return input.getScope();
      }
    };
  }

  private Path mapOperations(ApiDescription api, Optional existingPath) {
    Path path = existingPath.or(new Path());
    for (springfox.documentation.service.Operation each : nullToEmptyList(api.getOperations())) {
      Operation operation = mapOperation(each);
      path.set(each.getMethod().toString().toLowerCase(), operation);
    }
    return path;
  }


  private Function toScheme() {
    return new Function() {
      @Override
      public Scheme apply(String input) {
        return Scheme.forValue(input);
      }
    };
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy