Skip to content

Conversation

@mcruzdev
Copy link
Owner

Many thanks for submitting your Pull Request ❤️!

What this PR does / why we need it:

Special notes for reviewers:

Additional information (if needed):

@mcruzdev mcruzdev force-pushed the jackson-oapi branch 3 times, most recently from c0d395a to 02e191c Compare December 20, 2025 02:30
@mcruzdev
Copy link
Owner Author

mcruzdev commented Dec 20, 2025

@fjtirado In any way we need to have a String to JsonNode and finally to POJO. Whether using the PR approach or using a StdDeserializer we have this flow:

String -> JsonNode -> POJO

@fjtirado
Copy link

@fjtirado In any way we need to have a String to JsonNode and finally to POJO. Whether using the PR approach or using a StdDeserializer we have this flow:

String -> JsonNode -> POJO

I meant that there is a method in Jackson that converts InputStream to POJO directly, so you do not need to convert to string, then to jsonnode, then to pojo.

Comment on lines 32 to 74
private static final ObjectMapper YAML_MAPPER = new YAMLMapper();
private static final ObjectMapper JSON_MAPPER = new JsonMapper();

/**
* Parse the provided OpenAPI content (JSON or YAML) and return a {@link OpenAPI}.
*
* @param content the OpenAPI document content (must not be null or blank)
* @return parsed {@link OpenAPI}
* @throws IllegalArgumentException if content is null/blank or cannot be parsed
*/
public OpenAPI parse(String content) {
Objects.requireNonNull(content, "content must not be null");
String trimmed = content.trim();
if (trimmed.isEmpty()) {
throw new IllegalArgumentException("content must not be blank");
}

ObjectMapper mapper = selectMapper(trimmed);
try {
JsonNode root = mapper.readTree(content);
return new OpenAPI(root);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse content", e);
}
}

private ObjectMapper selectMapper(String trimmedContent) {
char first = firstNonWhitespaceChar(trimmedContent);
if (first == '{') {
return JSON_MAPPER;
}
return YAML_MAPPER;
}

private static char firstNonWhitespaceChar(String s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!Character.isWhitespace(c)) {
return c;
}
}
return '\0';
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcruzdev mcruzdev force-pushed the jackson-oapi branch 3 times, most recently from 58d3dfd to 8cfce9e Compare December 30, 2025 17:11
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants