Java Code Examples for io.swagger.models.ModelImpl#setType()

The following examples show how to use io.swagger.models.ModelImpl#setType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComponentSchemaTransformer.java    From spring-openapi with MIT License 6 votes vote down vote up
public Model transformSimpleSchema(Class<?> clazz, GenerationContext generationContext) {
	if (clazz.isEnum()) {
		return createEnumModel(clazz.getEnumConstants());
	}

	ModelImpl schema = new ModelImpl();
	schema.setType("object");
	getClassProperties(clazz, generationContext).forEach(schema::addProperty);

	if (generationContext.getInheritanceMap().containsKey(clazz.getName())) {
		InheritanceInfo inheritanceInfo = generationContext.getInheritanceMap().get(clazz.getName());
		schema.setDiscriminator(inheritanceInfo.getDiscriminatorFieldName());
	}
	if (clazz.getSuperclass() != null) {
		return traverseAndAddProperties(schema, generationContext, clazz.getSuperclass(), clazz);
	}
	return schema;
}
 
Example 2
Source File: JaxrsReader.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
private Parameter replaceArrayModelForOctetStream(Operation operation, Parameter parameter) {
    if (parameter instanceof BodyParameter
            && operation.getConsumes() != null
            && operation.getConsumes().contains("application/octet-stream")) {
        BodyParameter bodyParam = (BodyParameter) parameter;
        Model schema = bodyParam.getSchema();
        if (schema instanceof ArrayModel) {
            ArrayModel arrayModel = (ArrayModel) schema;
            Property items = arrayModel.getItems();
            if (items != null && items.getFormat() == "byte" && items.getType() == "string") {
                ModelImpl model = new ModelImpl();
                model.setFormat("byte");
                model.setType("string");
                bodyParam.setSchema(model);
            }
        }
    }
    return parameter;
}
 
Example 3
Source File: SwaggerUtils.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * This method will generate a sample request body for the request.
 *
 * @param method        Rest resource method.
 * @param operation     Swagger operation object.
 * @param parameterList list of parameters.
 */
private static void generateSampleRequestPayload(String method, Operation operation,
                                                 List<AxisResourceParameter> parameterList) {
    // GET method does not have a body
    if (!"GET".equals(method)) {
        BodyParameter bodyParameter = new BodyParameter();
        bodyParameter.description("Sample Payload");
        bodyParameter.name("payload");
        bodyParameter.setRequired(false);

        ModelImpl modelschema = new ModelImpl();
        modelschema.setType("object");
        Map<String, Property> propertyMap = new HashMap<>(1);
        ObjectProperty objectProperty = new ObjectProperty();
        objectProperty.name("payload");

        Map<String, Property> payloadProperties = new HashMap<>();
        for (AxisResourceParameter resourceParameter : parameterList) {
            switch (resourceParameter.getParameterDataType()) {
                case SwaggerProcessorConstants.INTEGER:
                    payloadProperties.put(resourceParameter.getParameterName(), new IntegerProperty());
                    break;
                case SwaggerProcessorConstants.NUMBER:
                    payloadProperties.put(resourceParameter.getParameterName(), new DoubleProperty());
                    break;
                case SwaggerProcessorConstants.BOOLEAN:
                    payloadProperties.put(resourceParameter.getParameterName(), new BooleanProperty());
                    break;
                default:
                    payloadProperties.put(resourceParameter.getParameterName(), new StringProperty());
                    break;
            }
        }

        objectProperty.setProperties(payloadProperties);
        propertyMap.put("payload", objectProperty);
        modelschema.setProperties(propertyMap);
        bodyParameter.setSchema(modelschema);
        operation.addParameter(bodyParameter);
    }
}
 
Example 4
Source File: OpenApiTransformer.java    From spring-openapi with MIT License 4 votes vote down vote up
protected <T> ModelImpl createEnumModel(T[] enumConstants) {
	ModelImpl schema = new ModelImpl();
	schema.setType("string");
	schema.setEnum(Stream.of(enumConstants).map(Object::toString).collect(toList()));
	return schema;
}
 
Example 5
Source File: PojoOperationGenerator.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
private void wrapParametersToBody(List<ParameterGenerator> bodyFields) {
  String simpleRef = MethodUtils.findSwaggerMethodName(method) + "Body";

  bodyModel = new ModelImpl();
  bodyModel.setType(ModelImpl.OBJECT);
  for (ParameterGenerator parameterGenerator : bodyFields) {
    // to collect all information by swagger mechanism
    // must have a parameter type
    // but all these parameters will be wrap to be one body parameter, their parameter type must be null
    // so we first set to be BODY, after collected, set back to be null
    parameterGenerator.setHttpParameterType(HttpParameterType.BODY);
    scanMethodParameter(parameterGenerator);

    Property property = ModelConverters.getInstance().readAsProperty(parameterGenerator.getGenericType());
    property.setDescription(parameterGenerator.getGeneratedParameter().getDescription());
    bodyModel.addProperty(parameterGenerator.getParameterName(), property);

    parameterGenerator.setHttpParameterType(null);
  }
  swagger.addDefinition(simpleRef, bodyModel);

  SwaggerGeneratorFeature feature = swaggerGenerator.getSwaggerGeneratorFeature();
  // bodyFields.size() > 1 is no reason, just because old version do this......
  // if not care for this, then can just delete all logic about EXT_JAVA_CLASS/EXT_JAVA_INTF
  if (feature.isExtJavaClassInVendor()
      && bodyFields.size() > 1
      && StringUtils.isNotEmpty(feature.getPackageName())) {
    bodyModel.getVendorExtensions().put(SwaggerConst.EXT_JAVA_CLASS, feature.getPackageName() + "." + simpleRef);
  }

  RefModel refModel = new RefModel();
  refModel.setReference("#/definitions/" + simpleRef);

  bodyParameter = new BodyParameter();
  bodyParameter.name(simpleRef);
  bodyParameter.setSchema(refModel);
  bodyParameter.setName(parameterGenerators.size() == 1 ? parameterGenerators.get(0).getParameterName() : simpleRef);

  List<ParameterGenerator> newParameterGenerators = new ArrayList<>();
  newParameterGenerators.add(new ParameterGenerator(
      bodyParameter.getName(),
      Collections.emptyList(),
      null,
      HttpParameterType.BODY,
      bodyParameter));
  parameterGenerators.stream().filter(p -> p.getHttpParameterType() != null)
      .forEach(p -> newParameterGenerators.add(p));
  parameterGenerators = newParameterGenerators;
}
 
Example 6
Source File: AbstractOktaJavaClientCodegen.java    From okta-sdk-java with Apache License 2.0 4 votes vote down vote up
private Map<String, Model> processListsFromProperties(Collection<Property> properties, Model baseModel, Swagger swagger) {

        Map<String, Model> result = new LinkedHashMap<>();

        for (Property p : properties) {
            if (p != null && "array".equals(p.getType())) {

                ArrayProperty arrayProperty = (ArrayProperty) p;
                if ( arrayProperty.getItems() instanceof RefProperty) {
                    RefProperty ref = (RefProperty) arrayProperty.getItems();

                    String baseName = ref.getSimpleRef();

                    // Do not generate List wrappers for primitives (or strings)
                    if (!languageSpecificPrimitives.contains(baseName) && topLevelResources.contains(baseName)) {

                        String modelName = baseName + "List";

                        ModelImpl model = new ModelImpl();
                        model.setName(modelName);
                        model.setAllowEmptyValue(false);
                        model.setDescription("Collection List for " + baseName);

                        if (baseModel == null) {
                            baseModel = swagger.getDefinitions().get(baseName);
                        }

                        // only add the tags from the base model
                        if (baseModel.getVendorExtensions().containsKey("x-okta-tags")) {
                            model.setVendorExtension("x-okta-tags", baseModel.getVendorExtensions().get("x-okta-tags"));
                        }

                        model.setVendorExtension("x-isResourceList", true);
                        model.setVendorExtension("x-baseType", baseName);
                        model.setType(modelName);

                        result.put(modelName, model);
                    }
                }
            }
        }

        return result;
    }
 
Example 7
Source File: WSDL11SOAPOperationExtractor.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Gets swagger output parameter model for a given soap operation
 *
 * @param bindingOperation soap operation
 * @return list of swagger models for the parameters
 * @throws APIMgtWSDLException
 */
private List<ModelImpl> getSoapOutputParameterModel(BindingOperation bindingOperation) throws APIMgtWSDLException {
    List<ModelImpl> outputParameterModelList = new ArrayList<>();
    Operation operation = bindingOperation.getOperation();
    if (operation != null) {
        Output output = operation.getOutput();
        if (output != null) {
            Message message = output.getMessage();
            if (message != null) {
                Map map = message.getParts();

                for (Object obj : map.entrySet()) {
                    Map.Entry entry = (Map.Entry) obj;
                    Part part = (Part) entry.getValue();
                    if (part != null) {
                        if (part.getElementName() != null) {
                            outputParameterModelList.add(parameterModelMap.get(part.getElementName()
                                    .getLocalPart()));
                        } else {
                            if (part.getTypeName() != null && parameterModelMap
                                    .containsKey(part.getTypeName().getLocalPart())) {
                                outputParameterModelList
                                        .add(parameterModelMap.get(part.getTypeName().getLocalPart()));
                            } else {
                                ModelImpl model = new ModelImpl();
                                model.setType(ObjectProperty.TYPE);
                                model.setName(message.getQName().getLocalPart());
                                if (getPropertyFromDataType(part.getTypeName().getLocalPart()) instanceof RefProperty) {
                                    RefProperty property = (RefProperty) getPropertyFromDataType(part.getTypeName()
                                            .getLocalPart());
                                    property.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT +
                                            part.getTypeName().getLocalPart());
                                    model.addProperty(part.getName(), property);
                                } else {
                                    model.addProperty(part.getName(),
                                            getPropertyFromDataType(part.getTypeName().getLocalPart()));
                                }
                                parameterModelMap.put(model.getName(), model);
                                outputParameterModelList.add(model);
                            }
                        }
                    }
                }
            }
        }
    }
    return outputParameterModelList;
}