Java Code Examples for io.swagger.v3.oas.models.media.Content#addMediaType()
The following examples show how to use
io.swagger.v3.oas.models.media.Content#addMediaType() .
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 |
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: OpenApiObjectGenerator.java From flow with Apache License 2.0 | 6 votes |
private ApiResponse createApiSuccessfulResponse( MethodDeclaration methodDeclaration) { Content successfulContent = new Content(); // "description" is a REQUIRED property of Response ApiResponse successfulResponse = new ApiResponse().description(""); methodDeclaration.getJavadoc().ifPresent(javadoc -> { for (JavadocBlockTag blockTag : javadoc.getBlockTags()) { if (blockTag.getType() == JavadocBlockTag.Type.RETURN) { successfulResponse.setDescription( "Return " + blockTag.getContent().toText()); } } }); if (!methodDeclaration.getType().isVoidType()) { MediaType mediaItem = createReturnMediaType(methodDeclaration); successfulContent.addMediaType("application/json", mediaItem); successfulResponse.content(successfulContent); } return successfulResponse; }
Example 3
Source File: OpenAPIDeserializer.java From swagger-parser with Apache License 2.0 | 6 votes |
public Content getContent(ObjectNode node, String location, ParseResult result){ if (node == null) { return null; } Content content = new Content(); Set<String> keys = getKeys(node); for(String key : keys) { MediaType mediaType = getMediaType((ObjectNode) node.get(key), String.format("%s.'%s'", location, key), result); if (mediaType != null) { content.addMediaType(key, mediaType); } } return content; }
Example 4
Source File: OperationContext.java From servicecomb-toolkit with Apache License 2.0 | 5 votes |
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 5
Source File: OperationsTransformer.java From spring-openapi with MIT License | 5 votes |
private RequestBody createRequestBody(Method method, String userDefinedContentType) { ParameterNamePair requestBodyParameter = getRequestBody(method); if (requestBodyParameter == null) { return null; } if (shouldBeIgnored(requestBodyParameter.getParameter())) { logger.info("Ignoring parameter {}", requestBodyParameter.getName()); return null; } Content content = new Content(); content.addMediaType(resolveContentType(userDefinedContentType, requestBodyParameter.getParameter()), schemaGeneratorHelper.createMediaType( requestBodyParameter.getParameter().getType(), requestBodyParameter.getName(), singletonList(getGenericParam(requestBodyParameter.getParameter())) ) ); RequestBody requestBody = new RequestBody(); requestBody.setRequired(true); requestBody.setContent(content); requestBody.setDescription("requestBody"); requestBodyInterceptors.forEach(interceptor -> interceptor.intercept(method, requestBodyParameter.getParameter(), requestBodyParameter.getName(), requestBody) ); return requestBody; }
Example 6
Source File: SpringDocAnnotationsUtils.java From springdoc-openapi with Apache License 2.0 | 5 votes |
/** * 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 7
Source File: RequestBodyBuilder.java From springdoc-openapi with Apache License 2.0 | 5 votes |
/** * Build content. * * @param requestBody the request body * @param methodAttributes the method attributes * @param schema the schema * @param content the content */ private void buildContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema, Content content) { for (String value : methodAttributes.getMethodConsumes()) { io.swagger.v3.oas.models.media.MediaType mediaTypeObject = new io.swagger.v3.oas.models.media.MediaType(); mediaTypeObject.setSchema(schema); if (content.get(value) != null) { mediaTypeObject.setExample(content.get(value).getExample()); mediaTypeObject.setExamples(content.get(value).getExamples()); mediaTypeObject.setEncoding(content.get(value).getEncoding()); } content.addMediaType(value, mediaTypeObject); } requestBody.setContent(content); }
Example 8
Source File: SwaggerConverter.java From swagger-parser with Apache License 2.0 | 5 votes |
private RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param, List<String> consumes) { RequestBody body = new RequestBody(); BodyParameter bp = (BodyParameter) param; List<String> mediaTypes = new ArrayList<>(globalConsumes); if (consumes != null && consumes.size() > 0) { mediaTypes.clear(); mediaTypes.addAll(consumes); } if (mediaTypes.size() == 0) { mediaTypes.add("*/*"); } if (StringUtils.isNotBlank(param.getDescription())) { body.description(param.getDescription()); } body.required(param.getRequired()); Content content = new Content(); for (String type : mediaTypes) { content.addMediaType(type, new MediaType().schema( convert(bp.getSchema()))); if (StringUtils.isNotBlank(bp.getDescription())) { body.setDescription(bp.getDescription()); } } convertExamples(((BodyParameter) param).getExamples(), content); body.content(content); return body; }
Example 9
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
@Test public void resolveInlineRequestBody() throws Exception { OpenAPI openAPI = new OpenAPI(); ObjectSchema objectSchema = new ObjectSchema(); objectSchema.addProperties("street", new StringSchema()); Schema schema = new Schema(); schema.addProperties("address", objectSchema); schema.addProperties("name", new StringSchema()); MediaType mediaType = new MediaType(); mediaType.setSchema(schema); Content content = new Content(); content.addMediaType("*/*", mediaType ); RequestBody requestBody = new RequestBody(); requestBody.setContent(content); Operation operation = new Operation(); operation.setRequestBody(requestBody); PathItem pathItem = new PathItem(); pathItem.setGet(operation); openAPI.path("/hello",pathItem); new InlineModelResolver().flatten(openAPI); Operation getOperation = openAPI.getPaths().get("/hello").getGet(); RequestBody body = getOperation.getRequestBody(); assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null); Schema bodySchema = openAPI.getComponents().getSchemas().get("body"); assertTrue(bodySchema instanceof Schema); assertNotNull(bodySchema.getProperties().get("address")); }
Example 10
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
@Test public void testInlineMapResponse() throws Exception { OpenAPI openAPI = new OpenAPI(); Schema schema = new Schema(); schema.setAdditionalProperties(new StringSchema()); schema.addExtension("x-ext", "ext-prop"); ApiResponse apiResponse = new ApiResponse(); apiResponse.description("it works!"); MediaType mediaType = new MediaType(); mediaType.setSchema(schema); Content content = new Content(); content.addMediaType("*/*",mediaType); apiResponse.setContent(content); apiResponse.addExtension("x-foo", "bar"); ApiResponses apiResponses = new ApiResponses(); apiResponses.addApiResponse("200",apiResponse); openAPI.path("/foo/baz", new PathItem() .get(new Operation() .responses(apiResponses))); new InlineModelResolver().flatten(openAPI); ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200"); Schema property = response.getContent().get("*/*").getSchema(); assertTrue(property.getAdditionalProperties() != null); assertTrue(openAPI.getComponents().getSchemas() == null); assertEquals(1, property.getExtensions().size()); assertEquals("ext-prop", property.getExtensions().get("x-ext")); }
Example 11
Source File: OAS3Parser.java From carbon-apimgt with Apache License 2.0 | 5 votes |
/** * Construct openAPI definition for graphQL. Add get and post operations * * @param openAPI OpenAPI * @return modified openAPI for GraphQL */ private void modifyGraphQLSwagger(OpenAPI openAPI) { SwaggerData.Resource resource = new SwaggerData.Resource(); resource.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN); resource.setPolicy(APIConstants.DEFAULT_SUB_POLICY_UNLIMITED); resource.setPath("/"); resource.setVerb(APIConstants.HTTP_POST); Operation postOperation = createOperation(resource); //post operation RequestBody requestBody = new RequestBody(); requestBody.setDescription("Query or mutation to be passed to graphQL API"); requestBody.setRequired(true); JSONObject typeOfPayload = new JSONObject(); JSONObject payload = new JSONObject(); typeOfPayload.put(APIConstants.TYPE, APIConstants.STRING); payload.put(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME, typeOfPayload); Schema postSchema = new Schema(); postSchema.setType(APIConstants.OBJECT); postSchema.setProperties(payload); MediaType mediaType = new MediaType(); mediaType.setSchema(postSchema); Content content = new Content(); content.addMediaType(APPLICATION_JSON_MEDIA_TYPE, mediaType); requestBody.setContent(content); postOperation.setRequestBody(requestBody); //add post and get operations to path /* PathItem pathItem = new PathItem(); pathItem.setPost(postOperation); Paths paths = new Paths(); paths.put("/", pathItem); openAPI.setPaths(paths); }
Example 12
Source File: Reader.java From proteus with Apache License 2.0 | 4 votes |
private void setMediaTypeToContent(Schema schema, Content content, String value) { MediaType mediaTypeObject = new MediaType(); mediaTypeObject.setSchema(schema); content.addMediaType(value, mediaTypeObject); }
Example 13
Source File: OpenApiObjectGenerator.java From flow with Apache License 2.0 | 4 votes |
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; }
Example 14
Source File: SwaggerConverter.java From swagger-parser with Apache License 2.0 | 4 votes |
public ApiResponse convert(io.swagger.models.Response v2Response, List<String> produces) { ApiResponse response = new ApiResponse(); Content content = new Content(); if (v2Response instanceof RefResponse) { RefResponse ref = (RefResponse) v2Response; if (ref.get$ref().indexOf("#/responses") == 0) { String updatedRef = "#/components/responses" + ref.get$ref().substring("#/responses".length()); ref.set$ref(updatedRef); } response.set$ref(ref.get$ref()); } else { List<String> mediaTypes = new ArrayList<>(globalProduces); if (produces != null) { // use this for media type mediaTypes.clear(); mediaTypes.addAll(produces); } if (mediaTypes.size() == 0) { mediaTypes.add("*/*"); } response.setDescription(v2Response.getDescription()); if (v2Response.getSchema() != null) { Schema schema = convertFileSchema(convert(v2Response.getSchema())); for (String type : mediaTypes) { // TODO: examples MediaType mediaType = new MediaType(); content.addMediaType(type, mediaType.schema(schema)); } response.content(content); } response.content(convertExamples(v2Response.getExamples(), content)); response.setExtensions(convert(v2Response.getVendorExtensions())); if (v2Response.getHeaders() != null && v2Response.getHeaders().size() > 0) { response.setHeaders(convertHeaders(v2Response.getHeaders())); } } return response; }
Example 15
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 4 votes |
@Test public void testInlineResponseModel() throws Exception { OpenAPI openAPI = new OpenAPI(); StringSchema stringSchema1 = new StringSchema(); ObjectSchema objectSchema1 = new ObjectSchema(); objectSchema1.addProperties("name", stringSchema1); objectSchema1.addExtension("x-ext", "ext-prop"); MediaType mediaType1 = new MediaType(); mediaType1.setSchema(objectSchema1); Content content1 = new Content(); content1.addMediaType("*/*", mediaType1 ); ApiResponse response1= new ApiResponse(); response1.setDescription("it works!"); response1.setContent(content1); ApiResponses responses1 = new ApiResponses(); responses1.addApiResponse("200",response1); Operation operation1 = new Operation(); operation1.setResponses(responses1); PathItem pathItem1 = new PathItem(); pathItem1.setGet(operation1); openAPI.path("/foo/bar",pathItem1); StringSchema stringSchema2 = new StringSchema(); ObjectSchema objectSchema2 = new ObjectSchema(); objectSchema2.addProperties("name", stringSchema2); objectSchema2.addExtension("x-ext", "ext-prop"); MediaType mediaType2 = new MediaType(); mediaType2.setSchema(objectSchema2); Content content2 = new Content(); content2.addMediaType("*/*", mediaType2 ); ApiResponse response2 = new ApiResponse(); response2.setDescription("it works!"); response2.addExtension("x-foo","bar"); response2.setContent(content2); ApiResponses responses2 = new ApiResponses(); responses2.addApiResponse("200",response2); Operation operation2 = new Operation(); operation2.setResponses(responses2); PathItem pathItem2 = new PathItem(); pathItem2.setGet(operation2); openAPI.path("/foo/baz",pathItem2); new InlineModelResolver().flatten(openAPI); Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses(); ApiResponse response = responses.get("200"); assertNotNull(response); Schema schema = response.getContent().get("*/*").getSchema(); assertTrue(schema.get$ref() != null); assertEquals(1, schema.getExtensions().size()); assertEquals("ext-prop", schema.getExtensions().get("x-ext")); Schema model = openAPI.getComponents().getSchemas().get("inline_response_200"); assertTrue(model.getProperties().size() == 1); assertNotNull(model.getProperties().get("name")); assertTrue(model.getProperties().get("name") instanceof StringSchema); }
Example 16
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 2 votes |
@Test public void testInlineResponseModelWithTitle() throws Exception { OpenAPI openAPI = new OpenAPI(); String responseTitle = "GetBarResponse"; StringSchema stringSchema1 = new StringSchema(); ObjectSchema objectSchema1 = new ObjectSchema(); objectSchema1.setTitle(responseTitle); objectSchema1.addProperties("name", stringSchema1); MediaType mediaType1 = new MediaType(); mediaType1.setSchema(objectSchema1); Content content1 = new Content(); content1.addMediaType("*/*", mediaType1 ); ApiResponse response1= new ApiResponse(); response1.setDescription("it works!"); response1.setContent(content1); ApiResponses responses1 = new ApiResponses(); responses1.addApiResponse("200",response1); Operation operation1 = new Operation(); operation1.setResponses(responses1); PathItem pathItem1 = new PathItem(); pathItem1.setGet(operation1); openAPI.path("/foo/bar",pathItem1); StringSchema stringSchema2 = new StringSchema(); ObjectSchema objectSchema2 = new ObjectSchema(); objectSchema2.addProperties("name", stringSchema2); objectSchema2.addExtension("x-foo", "bar"); MediaType mediaType2 = new MediaType(); mediaType2.setSchema(objectSchema2); Content content2 = new Content(); content2.addMediaType("*/*", mediaType2 ); ApiResponse response2 = new ApiResponse(); response2.setDescription("it works!"); response2.setContent(content2); ApiResponses responses2 = new ApiResponses(); responses2.addApiResponse("200",response2); Operation operation2 = new Operation(); operation2.setResponses(responses2); PathItem pathItem2 = new PathItem(); pathItem2.setGet(operation2); openAPI.path("/foo/baz",pathItem2); new InlineModelResolver().flatten(openAPI); Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses(); ApiResponse response = responses.get("200"); assertNotNull(response); assertTrue(response.getContent().get("*/*").getSchema().get$ref() != null ); Schema model = openAPI.getComponents().getSchemas().get(responseTitle); assertTrue(model.getProperties().size() == 1); assertNotNull(model.getProperties().get("name")); assertTrue(model.getProperties().get("name") instanceof StringSchema); }