Skip to content
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

Unable to convert XSD to Open API v3 Json schema #97

Open
saurabhjaju2418 opened this issue Apr 2, 2024 · 0 comments
Open

Unable to convert XSD to Open API v3 Json schema #97

saurabhjaju2418 opened this issue Apr 2, 2024 · 0 comments

Comments

@saurabhjaju2418
Copy link

I want to convert xsd to open api v3 json schema, as i asked to copilot he suggest jsonix library.
Here is the code suggested, but this code is not found in jar

try {
// Compile XSD to JAXB classes
XJC xjc = Jsonix.generateXJC(new File(xsdFilePath));
SchemaInfo schemaInfo = xjc.generateSchemaInfo();

        // Generate JSON schema
        JsonSchemaGenerator jsonSchemaGenerator = Jsonix.generateJsonSchemaGenerator(schemaInfo);
        JsonSchemaInfo jsonSchemaInfo = jsonSchemaGenerator.generateJsonSchemaInfo();

        // Add required fields based on minOccurs
        Map<String, Boolean> requiredFields = getRequiredFieldsFromXsd(schemaInfo);
        jsonSchemaInfo = addRequiredFieldsToSchema(jsonSchemaInfo, requiredFields);

        // Serialize JSON schema to a file
        Jsonix.writeJsonSchemaInfo(jsonSchemaInfo, new File(jsonSchemaFilePath));

        System.out.println("Conversion successful! JSON schema saved at: " + jsonSchemaFilePath);
    } catch (IOException e) {
        e.printStackTrace();
    }

private static Map<String, Boolean> getRequiredFieldsFromXsd(SchemaInfo schemaInfo) {
// Implement logic to extract required fields from schemaInfo
// For each field, map its name to true if minOccurs > 0, otherwise false
// Example: { "uniqueReferenceNumber": true, "recordLocator": true, ... }
// You'll need to adapt this part to your specific XSD
Map<String, Boolean> requiredFields = new HashMap<>();
// ... populate requiredFields ...
return requiredFields;
}

private static JsonSchemaInfo addRequiredFieldsToSchema(JsonSchemaInfo jsonSchemaInfo,
                                                        Map<String, Boolean> requiredFields) {
    // Add required fields to the existing JSON schema
    // Modify jsonSchemaInfo to indicate required fields
    // Example: Set "required" property for each field based on requiredFields map
    // You'll need to adapt this part to your specific JSON schema structure
    // ...
    return jsonSchemaInfo;
}
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

No branches or pull requests

1 participant