Conversation
c0d395a to
02e191c
Compare
|
@fjtirado In any way we need to have a String to JsonNode and finally to POJO. Whether using the PR approach or using a
|
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. |
| 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'; | ||
| } |
There was a problem hiding this comment.
You can select the mapper to be use from the file extension, as in here https://github.com/serverlessworkflow/sdk-java/blob/main/impl/validation/src/main/java/io/serverlessworkflow/impl/jackson/schema/JsonSchemaValidatorFactory.java#L38-L43
58d3dfd to
8cfce9e
Compare
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>
Many thanks for submitting your Pull Request ❤️!
What this PR does / why we need it:
Special notes for reviewers:
Additional information (if needed):