io.swagger.v3.oas.annotations.media.ExampleObject Java Examples

The following examples show how to use io.swagger.v3.oas.annotations.media.ExampleObject. 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: HelloController.java    From springdoc-openapi with Apache License 2.0 9 votes vote down vote up
@PostMapping("/test2")
@RequestBody(
		description = "Details of the Item to be created",
		required = true,
		content = @Content(
				schema = @Schema(implementation = User.class),
				mediaType = MediaType.APPLICATION_JSON_VALUE,
				examples = {
						@ExampleObject(
								name = "An example request with the minimum required fields to create.",
								value = "min",
								summary = "Minimal request"),
						@ExampleObject(
								name = "An example request with all fields provided with example values.",
								value = "full",
								summary = "Full request") }))
public void test2(String hello) {
}
 
Example #2
Source File: Advice.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
@ExceptionHandler(TypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ApiResponse(
		responseCode = "400",
		description = "Bad Request",
		content =
		@Content(
				mediaType = MediaType.APPLICATION_JSON_VALUE,
				schema = @Schema(implementation = ApiError.class),
				examples = {
						@ExampleObject(
								name = "Service-400",
								summary = "400 from the service directly",
								value =
										"{\"status\": 400,"
												+ "\"errorCode\": \"ERROR_001\","
												+ "\"message\": \"An example message...\""
												+ "}")
				}))
public ResponseEntity<ApiError> badRequest(HttpServletRequest req, Exception exception) {
	ApiError erroObj = new ApiError(400, "A code", "A message");
	return new ResponseEntity<>(erroObj, HttpStatus.BAD_REQUEST);
}
 
Example #3
Source File: Advice.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ApiResponse(
		responseCode = "500",
		description = "Internal Server Error",
		content =
		@Content(
				mediaType = MediaType.APPLICATION_JSON_VALUE,
				schema = @Schema(implementation = ApiError.class),
				examples = {
						@ExampleObject(
								name = "Service-500",
								summary = "500 from the service directly",
								value =
										"{\"status\": 500,"
												+ "\"errorCode\": \"ERROR_002\","
												+ "\"message\": \"Another example message...\""
												+ "}")
				}))
public ResponseEntity<ApiError> internalServerError(HttpServletRequest req, Exception exception) {
	ApiError erroObj = new ApiError(500, "A  different code", "A  different message");
	return new ResponseEntity<>(erroObj, HttpStatus.INTERNAL_SERVER_ERROR);
}
 
Example #4
Source File: GenericParameterBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Sets examples.
 *
 * @param parameterDoc the parameter doc
 * @param parameter the parameter
 */
private void setExamples(io.swagger.v3.oas.annotations.Parameter parameterDoc, Parameter parameter) {
	Map<String, Example> exampleMap = new HashMap<>();
	if (parameterDoc.examples().length == 1 && StringUtils.isBlank(parameterDoc.examples()[0].name())) {
		Optional<Example> exampleOptional = AnnotationsUtils.getExample(parameterDoc.examples()[0]);
		exampleOptional.ifPresent(parameter::setExample);
	}
	else {
		for (ExampleObject exampleObject : parameterDoc.examples()) {
			AnnotationsUtils.getExample(exampleObject)
					.ifPresent(example -> exampleMap.put(exampleObject.name(), example));
		}
	}
	if (exampleMap.size() > 0) {
		parameter.setExamples(exampleMap);
	}
}
 
Example #5
Source File: HelloController.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
@PostMapping("/test")
@RequestBody(
		content = @Content(
				examples = @ExampleObject(
						value = "sample"
				)
		)
)
public String postMyRequestBody(
		String myRequestBody) {
	return null;
}
 
Example #6
Source File: HelloController.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
@Operation(summary = "Get Something by key", responses = {
		@ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(oneOf = {
				String.class, Integer.class }), examples = {
				@ExampleObject(name = "The String example", value = "urgheiurgheirghieurg"),
				@ExampleObject(name = "The Integer example", value = "311414") })),
		@ApiResponse(responseCode = "404", description = "Thing not found"),
		@ApiResponse(responseCode = "401", description = "Authentication Failure") })
@GetMapping(value = "/hello")
ResponseEntity<Void> sayHello() {
	return null;
}
 
Example #7
Source File: SpringDocAnnotationsUtils.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Gets content.
 *
 * @param annotationContents the annotation contents
 * @param classTypes the class types
 * @param methodTypes the method types
 * @param schema the schema
 * @param components the components
 * @param jsonViewAnnotation the json view annotation
 * @return the content
 */
public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.Content[] annotationContents,
		String[] classTypes, String[] methodTypes, Schema schema, Components components,
		JsonView jsonViewAnnotation) {
	if (ArrayUtils.isEmpty(annotationContents)) {
		return Optional.empty();
	}
	// Encapsulating Content model
	Content content = new Content();

	for (io.swagger.v3.oas.annotations.media.Content annotationContent : annotationContents) {
		MediaType mediaType = getMediaType(schema, components, jsonViewAnnotation, annotationContent);
		ExampleObject[] examples = annotationContent.examples();
		setExamples(mediaType, examples);
		addExtension(annotationContent, mediaType);
		io.swagger.v3.oas.annotations.media.Encoding[] encodings = annotationContent.encoding();
		addEncodingToMediaType(jsonViewAnnotation, mediaType, encodings);
		if (StringUtils.isNotBlank(annotationContent.mediaType())) {
			content.addMediaType(annotationContent.mediaType(), mediaType);
		}
		else {
			if (mediaType.getSchema() != null)
				applyTypes(classTypes, methodTypes, content, mediaType);
		}
	}

	if (content.size() == 0 && annotationContents.length != 1) {
		return Optional.empty();
	}
	return Optional.of(content);
}
 
Example #8
Source File: HelloController.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@GetMapping("/test")
@ApiResponses(value = { @ApiResponse(description = "successful operation", content = { @Content(examples = @ExampleObject(name = "500", ref = "#/components/examples/http500Example"), mediaType = "application/json", schema = @Schema(implementation = User.class)), @Content(mediaType = "application/xml", schema = @Schema(implementation = User.class)) }) })
public void test1(String hello) {
}