@Test(description = "it should serialize a ref BodyParameter") public void serializeRefBodyParameter() { final Schema model = new Schema().$ref("#/definitions/Cat"); final RequestBody p = new RequestBody() .content(new Content().addMediaType("*/*", new MediaType().schema(model))); final String json = "{\"content\":{\"*/*\":{\"schema\":{\"$ref\":\"#/definitions/Cat\"}}}}"; SerializationMatchers.assertEqualsToJson(p, json); }
RequestBody requestBody = optionalRequestBody.get(); if (StringUtils.isBlank(requestBody.get$ref()) && (requestBody.getContent() == null || requestBody.getContent().isEmpty())) { if (requestBodyParameter.getSchema() != null) { Content content = processContent(requestBody.getContent(), requestBodyParameter.getSchema(), methodConsumes, classConsumes); !requestBody.getContent().isEmpty()) { if (requestBodyParameter.getSchema() != null) { for (MediaType mediaType : requestBody.getContent().values()) { if (mediaType.getSchema() == null) { if (requestBodyParameter.getSchema() == null) {
Content content = new Content(); content.addMediaType(annotationContent.mediaType(), mediaType); } else { applyTypes(classTypes, methodTypes, content, mediaType); if (content.size() == 0) { return Optional.empty();
operation.getRequestBody().setContent(content); } else if (parentRequestBody.getContent() != null){ for (String parentMediaType: parentRequestBody.getContent().keySet()) { if (content.get(parentMediaType) == null) { content.addMediaType(parentMediaType, parentRequestBody.getContent().get(parentMediaType)); if (resolvedSchema.schema != null) { Schema returnTypeSchema = resolvedSchema.schema; Content content = new Content(); MediaType mediaType = new MediaType().schema(returnTypeSchema); AnnotationsUtils.applyTypes(classProduces == null ? new String[0] : classProduces.value(), operation.getResponses().getDefault().content(content); } else { for (String key : operation.getResponses().getDefault().getContent().keySet()) { if (operation.getResponses().getDefault().getContent().get(key).getSchema() == null) { operation.getResponses().getDefault().getContent().get(key).setSchema(returnTypeSchema); Content content = new Content(); MediaType mediaType = new MediaType(); AnnotationsUtils.applyTypes(classProduces == null ? new String[0] : classProduces.value(),
private Content convertExamples(final Map examples, final Content content) { if (examples != null) { examples.forEach((k, v) -> { MediaType mT = content.get(k); if (mT == null) { mT = new MediaType(); content.addMediaType(k.toString(), mT); } mT.setExample(v); }); } return content; }
Content content = new Content(); MediaType mt = new MediaType(); Schema schema = new Schema(); if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) { consumes = new HashSet<>(); consumes.add("application/json"); // default to application/json LOGGER.info("Default to application/json for inline body schema"); } else { consumes = requestBody.getContent().keySet(); content.addMediaType(consume, mt); String existing = matchGenerated(model); Content content = response.getContent(); for (MediaType mediaType : content.values()) { if (existing != null) { Schema schema = this.makeSchema(existing, property);
@Test(description = "it should deserialize a read only parameter") public void deserializeReadOnlyParameter() throws IOException { final String json = "{\"in\":\"path\"," + "\"content\":{" + " \"*/*\":{" + " \"schema\":{" + " \"type\":\"string\"," + " \"readOnly\":true}}}}"; final Parameter p = m.readValue(json, Parameter.class); assertTrue(p.getContent().get("*/*").getSchema().getReadOnly()); }
private void declareGetResponseFormats(OpenAPI api, String path, Class<?> binding) { PathItem pi = api.getPaths().get(path); Operation get = pi.getGet(); Content content = get.getResponses().get("200").getContent(); List<String> formats = DefaultWebFeatureService30.getAvailableFormats(binding); // first remove the ones missing Set<String> missingFormats = new HashSet<>(content.keySet()); missingFormats.removeAll(formats); missingFormats.forEach(f -> content.remove(f)); // then add the ones not already declared Set<String> extraFormats = new HashSet<>(formats); extraFormats.removeAll(content.keySet()); for (String extraFormat : extraFormats) { MediaType mediaType = new MediaType(); if (extraFormat.contains("yaml") && content.get("application/json") != null) { // same schema as JSON mediaType.schema(content.get("application/json").getSchema()); } else if (extraFormat.contains("text")) { mediaType.schema(new StringSchema()); } else { mediaType.schema(new BinarySchema()); } content.addMediaType(extraFormat, mediaType); } }
public static void applyTypes(String[] classTypes, String[] methodTypes, Content content, MediaType mediaType) { if (methodTypes != null && methodTypes.length > 0) { for (String value : methodTypes) { content.addMediaType(value, mediaType); } } else if (classTypes != null && classTypes.length > 0) { for (String value : classTypes) { content.addMediaType(value, mediaType); } } else { content.addMediaType(ParameterProcessor.MEDIA_TYPE, mediaType); } }
protected Content processContent(Content content, Schema schema, Consumes methodConsumes, Consumes classConsumes) { if (content == null) { content = new Content(); } if (methodConsumes != null) { for (String value : methodConsumes.value()) { setMediaTypeToContent(schema, content, value); } } else if (classConsumes != null) { for (String value : classConsumes.value()) { setMediaTypeToContent(schema, content, value); } } else { setMediaTypeToContent(schema, content, DEFAULT_MEDIA_TYPE_VALUE); } return content; }
private boolean containsFormContentType(RequestBody body) { if (body == null) { return false; } final Content content = body.getContent(); if (content == null || content.isEmpty()) { return false; } return content.get("application/x-www-form-urlencoded") != null || content.get("multipart/form-data") != null; }
@Nonnull private Collection<String> getResponseMediaTypes(final Response response, final ApiOperation apiOperation) { final ApiResponse apiResponse = getApiResponse(response, apiOperation); if (apiResponse == null) { return emptyList(); } return defaultIfNull(apiResponse.getContent(), new Content()).keySet(); }
private static Schema getSchemaFromContent(Content content) { if (content == null || content.isEmpty()) { return null; } if (content.size() > 1) { LOGGER.warn("Multiple schemas found, returning only the first one"); } MediaType mediaType = content.values().iterator().next(); return mediaType.getSchema(); }
@Nonnull private Collection<String> getProduces(final ApiOperation apiOperation) { return apiOperation.getOperation() .getResponses() .values() .stream() .filter(apiResponse -> apiResponse.getContent() != null) .flatMap(apiResponse -> apiResponse.getContent().keySet().stream()) .collect(Collectors.toSet()); }
/** * Return the example value of the parameter. * * @param codegenParameter Codegen parameter * @param requestBody Request body */ public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) { Content content = requestBody.getContent(); if (content.size() > 1) { // @see ModelUtils.getSchemaFromContent() LOGGER.warn("Multiple MediaTypes found, using only the first one"); } MediaType mediaType = content.values().iterator().next(); if (mediaType.getExample() != null) { codegenParameter.example = mediaType.getExample().toString(); return; } if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) { Example example = mediaType.getExamples().values().iterator().next(); if (example.getValue() != null) { codegenParameter.example = example.getValue().toString(); return; } } setParameterExampleValue(codegenParameter); }
@Override public Response transform(Pair<String, ApiResponse> from) { Map<MediaType, Schema> schemaRefs = emptyMap(); Content content = from.getValue().getContent(); if (content != null) { Set<Map.Entry<String, io.swagger.v3.oas.models.media.MediaType>> entries = content.entrySet(); schemaRefs = entries.stream().collect(toMap(e -> new MediaType(e.getKey()), e -> mediaTypeTransformer.transform(e.getValue()))); } return new Response(from.getKey(), schemaRefs); } }