php-json-schema-model-generator icon indicating copy to clipboard operation
php-json-schema-model-generator copied to clipboard

Error when generating classes with FedEx OpenAPI schema

Open mehgcap opened this issue 1 year ago • 4 comments

Describe the bug When I use this library to try to generate PHP classes from FedEx's OpenAPI schema for their new RESTful API, I get this error:

PHP Fatal error: Uncaught PHPModelGenerator\Exception\SchemaException: No nested schema for composed property RequestePackageLineItemDimensions in file /[...]/utils/../schemas/rate.json

(Note: that should be RequestedPackageLineItemDimensions, but the first 'd' is missing in the reference and the definition, so it shouldn't matter.)

This is mentioned in issue 57, but the solution there was to alter the schema. Since the goal is to be as hands-off as possible, I'd much prefer to simply download the schemas and run this function on them. I don't want to have to modify every schema we need (ship, rate, address validation, and more) each time we have to update to a new API release. I've installed a java-based tool that converts schemas into PHP, and it didn't encounter this problem when using this schema, so I know it's possible. I also validated the schema with two online validators, and both indicated that the JSON is valid for OpenAPI.

Expected behavior

Given that this is a valid OpenAPI 3 schema from a major company, I expect it to be processed into PHP classes properly.

Schema

https://developer.fedex.com/api/en-us/catalog/rate/v1/docs.html > click "download JSON schema". I'd link to it directly here, but the page uses some JS to actually serve the JSON file.

Here's the function I'm using to try to generate the classes:

function generate() {
	$generator = new ModelGenerator(
		(new GeneratorConfiguration())
		->setNamespacePrefix('models\fedex')
		->setSerialization(true)
		->setCollectErrors(false)
		->setImmutable(false)
	);
	$schemaPath = dirname(__FILE__) . '/../schemas/rate.json';
	$resultDirectory = dirname(__FILE__) . '/../models/fedex';
	$generator
		->generateModelDirectory($resultDirectory)
		->generateModels(new OpenAPIv3Provider($schemaPath), $resultDirectory);
}

Version:

0.23.3

Additional context

I don't think I have anything to add. I'm experienced with PHP, but very new to OpenAPI and class generation. If more information is required, please ask.

mehgcap avatar May 16 '23 18:05 mehgcap

Hi, thanks for the report. I've figured out the issue with the RequestedPackageLineItemDimensions which is related to nested usage of allOf compositions which work with a patch I've developed (still need to write some test cases).

Another issue which occurs later is related to the Body entry of the schema, a large collection of oneOf references where only a single oneOf branch provides an actual schema. All other branches provide example data (so strictly no validation rules and every body is valid as a oneOf branch with only example data is always fulfilled no matter which data is provided). I'll have a deeper look into it and check if it makes sense to skip branches which only contain example data during the generation process.

For example:

{
  "body": 34
}

validates against the following simplified schema which shows the issue with the Body from the rate.json schema which doesn't seem to be intended:

{
  "type":"object",
  "properties":{
  	"body": {
    	"oneOf": [
          {
            "type":"object",
            "properties": {
              "name": {
              	"type": "string"
              }
            },
            "required": ["name"]
          },
          {
            "example": {
              "name": "wol-soft"
            }
          }
        ]
    }
  },
  "required": ["body"]
}

wol-soft avatar May 23 '23 10:05 wol-soft

Thank you for checking into this! It would be great if your library could support these schemas. Everything I've tried so far has either choked on them, or generated output we've had to manually modify afterward. I think the body section you mentioned was a problem, and was one thing we had to remove from classes generated with another tool.

mehgcap avatar May 23 '23 13:05 mehgcap

I pulled issue72_nestedCompositionbranch that links to this issue, and used it to generate ship models from FedEx's ship schema and got this error:

Message: Unresolved Reference #/components/schemas/RequestedShipment in file /tmp/ship.json    in file: /home/hfinn/repos/ship/vendor/wol-soft/php-json-schema-model-generator/src/PropertyProcessor/Property/ReferenceProcessor.php  
 on line: 50
    

Same as the rate schema, go to Shipping documentation > click Download Json Schema to look at the json.

Kindlewing avatar Jun 01 '23 14:06 Kindlewing

Hi, I'm still facing some issues with the rate.json related to validators in combination with the nested allOf usage.

It takes some time to further investigate the issue which I'm currently not able to invest - sorry. I'll check it, when time allows. To be fair: this seems to be the most complex schema I've seen the library being used on. Nice challenge 😄

Then I'll also have a look at the shipping schema. The schema looks correct, the reference is defined and consequently should be resolved.

wol-soft avatar Jun 07 '23 12:06 wol-soft