Skip to content

Commit

Permalink
Merge pull request #860 from libgraviton/feature/colliding-sub-extref
Browse files Browse the repository at this point in the history
colliding sub extref
  • Loading branch information
narcoticfresh committed Mar 20, 2024
2 parents a3396ec + e358170 commit 3f8180c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions src/Graviton/GeneratorBundle/Generator/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,20 @@ public function generateSchema(array $parameters, bool $isSubResource, string $t

$thisSchema['additionalProperties'] = false;

$entityName = SchemaBuilder::getSchemaEntityName($parameters['document'], $parameters['json']->getNamespace());

// already set?
if (isset($schema['components']['schemas'][$parameters['document']])) {
if (isset($schema['components']['schemas'][$entityName])) {
throw new \RuntimeException(
sprintf(
'Schema object "%s" already exists, will not overwrite. Name collision for file %s.',
$parameters['document'],
$entityName,
$targetFile
)
);
}

$schema['components']['schemas'][$parameters['document']] = $thisSchema;
$schema['components']['schemas'][$entityName] = $thisSchema;

if (!$isSubResource) {
$schema = $this->writePaths($schema, $parameters);
Expand Down Expand Up @@ -279,10 +281,6 @@ private function writePaths(array $schema, array $parameters) : array
// this is the resource name!
$docName = $parameters['document'];

if ($docName == 'NewEmbedHashTest') {
$hans = 3;
}

$writeResponses = [
201 => [
'description' => 'Successful operation'
Expand Down Expand Up @@ -471,6 +469,7 @@ public function consolidateAllSchemas(?string $baseDir, OutputInterface $output,
->files()
->in($directories)
->path('config/schema')
->sortByName()
->name(['openapi.tmp.json', 'openapi.yaml']); // ending in .tmp!

$mainFile = $this->getSchema($targetFile, 'Graviton');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class FieldFlagsBuilder extends RuntimeDefinitionBuilderAbstract
*/
public function build(RuntimeBuilderData $data) : void
{

$baseSchema = $this->getSchemaBaseObject($data->definition, $data->schemaFile);
$fields = $this->getAllFields($baseSchema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use cebe\openapi\spec\Schema;
use Graviton\GeneratorBundle\Definition\JsonDefinition;
use Graviton\GeneratorBundle\RuntimeDefinition\Builder\RuntimeBuilderData;
use Graviton\GeneratorBundle\Schema\SchemaBuilder;
use Symfony\Component\Finder\SplFileInfo;

/**
Expand Down Expand Up @@ -37,8 +38,9 @@ abstract public function build(RuntimeBuilderData $data) : void;
*/
public function getSchemaBaseObject(JsonDefinition $definition, SplFileInfo $schemaFile) : Schema
{
$entityName = SchemaBuilder::getSchemaEntityName($definition->getId(), $definition->getNamespace());
$schema = Reader::readFromJsonFile($schemaFile->getPathname());
return $schema->components->schemas[$definition->getId()];
return $schema->components->schemas[$entityName];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Graviton\GeneratorBundle\Schema\Builder;

use Graviton\GeneratorBundle\Schema\SchemaBuilder;
use Graviton\GeneratorBundle\Schema\SchemaBuilderInterface;

/**
Expand Down Expand Up @@ -41,9 +42,11 @@ public function buildSchema(array $schemaField, array $fieldDefinition, array $a
}

if (str_starts_with($type, 'class:')) {
$className = explode('\\', $type);
$shortClassName = array_pop($className);
$type = '#/components/schemas/'.$shortClassName;
// and take the 2nd one, the bundle name
$type = sprintf(
'#/components/schemas/%s',
SchemaBuilder::getSchemaEntityName($type, $type)
);
}

if ($isArray) {
Expand Down
27 changes: 27 additions & 0 deletions src/Graviton/GeneratorBundle/Schema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,31 @@ public function buildSchema(array $schemaField, array $fieldDefinition, array $a

return $schemaField;
}

/**
* get the name for an entity that is used in the openapi schema
*
* @param string $shortName class name
* @param string $bundleScanString a string containing the bundle name somewhere
* @return string entity name
*/
public static function getSchemaEntityName(string $shortName, string $bundleScanString) : string
{
if (str_contains($shortName, '\\')) {
$parts = explode('\\', $shortName);
$shortName = array_pop($parts);
}

preg_match('/([a-zA-Z0-9]*)Bundle/i', $bundleScanString, $matches);

if (!isset($matches[1])) {
throw new \RuntimeException('Unable to determine entity name from bundlescanstring: '.$bundleScanString);
}

if ($matches[1] != $shortName) {
return $matches[1].$shortName;
}

return $shortName;
}
}
4 changes: 2 additions & 2 deletions tests/Rest/Controller/ShowcaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ public function testCorrectIdExposingInSchema()
$schema = $client->getResults();

// get the nested app schema
$nestedAppSchema = $schema->components->schemas->{'ShowCaseNestedApps'};
$nestedAppSchema = $schema->components->schemas->{'ShowCaseShowCaseNestedApps'};
$this->assertIsObject($nestedAppSchema);

// make sure we have an extref field here
Expand All @@ -1002,7 +1002,7 @@ public function testCorrectIdExposingInSchema()
$this->assertObjectNotHasProperty('id', $nestedAppSchema->properties);

// embed case - check the embedded 'contactCode'
$contactCodeSchema = $schema->components->schemas->{'ShowCaseContactCode'};
$contactCodeSchema = $schema->components->schemas->{'ShowCaseShowCaseContactCode'};
$this->assertIsObject($contactCodeSchema);

//$this->assertStringEndsWith('Embedded', $schema->properties->contactCode->{'x-documentClass'});
Expand Down

0 comments on commit 3f8180c

Please sign in to comment.