-
Notifications
You must be signed in to change notification settings - Fork 0
Proof of concept: use only Jackson for OpenAPI #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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):