Java Code Examples for io.swagger.v3.oas.models.media.MediaType#schema()

The following examples show how to use io.swagger.v3.oas.models.media.MediaType#schema() . 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: OperationContext.java    From servicecomb-toolkit with Apache License 2.0 6 votes vote down vote up
private void processProduces() {

    if (getProduces() == null) {
      return;
    }

    List<String> produceList = Arrays.stream(produces).filter(s -> !StringUtils.isEmpty(s))
        .collect(Collectors.toList());

    if (!produceList.isEmpty()) {
      ApiResponse apiResponse = new ApiResponse();
      Content content = new Content();
      MediaType mediaType = new MediaType();
      Schema schema = ModelConverter
          .getSchema(getMethod().getReturnType(), getComponents(), RequestResponse.RESPONSE);
      mediaType.schema(schema);
      for (String produce : produceList) {
        content.addMediaType(produce, mediaType);
      }
      apiResponse.description("OK");
      apiResponse.setContent(content);
      addResponse(HttpStatuses.OK, apiResponse);
    }
  }
 
Example 2
Source File: OperationContext.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
public void correctResponse(ApiResponses apiResponses) {

    if (apiResponses == null) {
      return;
    }

    // no annotations are processed
    // generate a default response based on the method return value
    if (apiResponses.get(HttpStatuses.OK) == null) {
      ApiResponse apiResponse = new ApiResponse();

      Class<?> returnType = method.getReturnType();
      if (returnType == Void.TYPE || returnType == Void.class) {
        return;
      }

      MediaType mediaType = new MediaType();

      Schema refSchema = ModelConverter.getSchema(returnType, getComponents(), RequestResponse.RESPONSE);
      mediaType.schema(refSchema);

      Content content = new Content();
      content.addMediaType(MediaTypes.APPLICATION_JSON, mediaType);
      apiResponse.description("OK");
      apiResponse.setContent(content);
      apiResponses.addApiResponse(HttpStatuses.OK, apiResponse);
    }
  }
 
Example 3
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 5 votes vote down vote up
private MediaType createReturnMediaType(
        MethodDeclaration methodDeclaration) {
    MediaType mediaItem = new MediaType();
    Type methodReturnType = methodDeclaration.getType();
    Schema schema = parseTypeToSchema(methodReturnType, "");
    if (methodDeclaration.isAnnotationPresent(Nullable.class)) {
        schema = schemaResolver.createNullableWrapper(schema);
    }
    usedTypes.putAll(collectUsedTypesFromSchema(schema));
    mediaItem.schema(schema);
    return mediaItem;
}
 
Example 4
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 4 votes vote down vote up
private RequestBody createRequestBody(MethodDeclaration methodDeclaration) {
    Map<String, String> paramsDescription = new HashMap<>();
    methodDeclaration.getJavadoc().ifPresent(javadoc -> {
        for (JavadocBlockTag blockTag : javadoc.getBlockTags()) {
            if (blockTag.getType() == JavadocBlockTag.Type.PARAM) {
                paramsDescription.put(blockTag.getName().orElse(""),
                        blockTag.getContent().toText());
            }
        }
    });

    RequestBody requestBody = new RequestBody();
    Content requestBodyContent = new Content();
    requestBody.content(requestBodyContent);
    MediaType requestBodyObject = new MediaType();
    requestBodyContent.addMediaType("application/json", requestBodyObject);
    Schema requestSchema = new ObjectSchema();
    requestSchema.setRequired(new ArrayList<>());
    requestBodyObject.schema(requestSchema);
    methodDeclaration.getParameters().forEach(parameter -> {
        Schema paramSchema = parseTypeToSchema(parameter.getType(), "");
        usedTypes.putAll(collectUsedTypesFromSchema(paramSchema));
        String name = (isReservedWord(parameter.getNameAsString()) ? "_"
                : "").concat(parameter.getNameAsString());
        if (GeneratorUtils.isBlank(paramSchema.get$ref())) {
            paramSchema.description(
                    paramsDescription.remove(parameter.getNameAsString()));
        }
        requestSchema.addProperties(name, paramSchema);
        if (GeneratorUtils.isNotTrue(paramSchema.getNullable())
                && !parameter.isAnnotationPresent(Nullable.class)) {
            requestSchema.addRequiredItem(name);
        }
        paramSchema.setNullable(null);
    });
    if (!paramsDescription.isEmpty()) {
        requestSchema.addExtension(
                EXTENSION_VAADIN_CONNECT_PARAMETERS_DESCRIPTION,
                new LinkedHashMap<>(paramsDescription));
    }
    return requestBody;
}