Skip to content

Commit

Permalink
Polish Yaml support
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 11, 2024
1 parent 6a8f0d6 commit eb88802
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,15 @@ public class MediaType extends MimeType implements Serializable {

/**
* Public constant media type for {@code application/yaml}.
* @since 6.2
*/
public static final MediaType APPLICATION_YAML;

/**
* A String equivalent of {@link MediaType#APPLICATION_YAML}.
* @since 6.2
*/
public static final String APPLICATION_YAML_VALE = "application/yaml";
public static final String APPLICATION_YAML_VALUE = "application/yaml";

/**
* Public constant media type for {@code image/gif}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.yaml.MappingJackson2YamlHttpMessageConverter;
import org.springframework.util.ClassUtils;

/**
Expand All @@ -47,6 +48,8 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv

private static final boolean jackson2SmilePresent;

private static final boolean jackson2YamlPresent;

private static final boolean gsonPresent;

private static final boolean jsonbPresent;
Expand All @@ -64,6 +67,7 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
jackson2YamlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.yaml.YAMLFactory", classLoader);
gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);
jsonbPresent = ClassUtils.isPresent("jakarta.json.bind.Jsonb", classLoader);
kotlinSerializationCborPresent = ClassUtils.isPresent("kotlinx.serialization.cbor.Cbor", classLoader);
Expand Down Expand Up @@ -99,6 +103,10 @@ else if (jsonbPresent) {
addPartConverter(new MappingJackson2SmileHttpMessageConverter());
}

if (jackson2YamlPresent) {
addPartConverter(new MappingJackson2YamlHttpMessageConverter());
}

if (kotlinSerializationCborPresent) {
addPartConverter(new KotlinSerializationCborHttpMessageConverter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter
* HttpMessageConverter} that can read and write the <a href="https://yaml.io/">YAML</a>
* data format using <a href="https://github.com/FasterXML/jackson-dataformat-yaml/tree/master">
* data format using <a href="https://github.com/FasterXML/jackson-dataformats-text/tree/2.17/yaml">
* the dedicated Jackson 2.x extension</a>.
*
* <p>By default, this converter supports the {@link MediaType#APPLICATION_YAML_VALE}
* <p>By default, this converter supports the {@link MediaType#APPLICATION_YAML_VALUE}
* media type. This can be overridden by setting the {@link #setSupportedMediaTypes
* supportedMediaTypes} property.
*
Expand Down Expand Up @@ -62,7 +62,6 @@ public MappingJackson2YamlHttpMessageConverter(ObjectMapper objectMapper) {
Assert.isInstanceOf(YAMLFactory.class, objectMapper.getFactory(), "YAMLFactory required");
}


/**
* {@inheritDoc}
* The {@code ObjectMapper} must be configured with a {@code YAMLFactory} instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private Properties getDefaultMediaTypes() {
defaultMediaTypes.put("cbor", MediaType.APPLICATION_CBOR_VALUE);
}
if (jackson2YamlPresent) {
defaultMediaTypes.put("yaml", MediaType.APPLICATION_YAML_VALE);
defaultMediaTypes.put("yaml", MediaType.APPLICATION_YAML_VALUE);
}
return defaultMediaTypes;
}
Expand Down

0 comments on commit eb88802

Please sign in to comment.