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

Array shapes support #1676

Open
Kiina opened this issue Nov 28, 2024 · 3 comments
Open

Array shapes support #1676

Kiina opened this issue Nov 28, 2024 · 3 comments

Comments

@Kiina
Copy link
Contributor

Kiina commented Nov 28, 2024

I've been trying to get my custom processor to get a good swagger.json for arrays. I've tried to use the PhpDocParser but noticed it gets a bit complex after a while. Then I looked at how doctypes are handled by and saw the extractVarTypeAndDescription method. It uses some regex that won't really work with array shapes.

Let's take a simple one:

/* @var array{id: int, name: string} */

Calling the extract on it returns:

array(2) {
  ["type"]=>
  string(9) "array{id:"
  ["description"]=>
  string(18) "int, name: string}"
}

Which results in:

properties:
  name:
    description: 'int, name: string}'

Which isn't the desired result.

@DerManoMann
Copy link
Collaborator

Fair point. I think I mayredo that for v5 still, but I am not inclined to write more ad-hoc docblock parsing code.
PRs are always welcome, of course.

@Kiina
Copy link
Contributor Author

Kiina commented Dec 2, 2024

Current behavior could be preserved pretty easily:

function extractVarTypeAndDescription(?string $docblock): array
{
    $config = new ParserConfig([]);
    $lexer = new Lexer($config);
    $constExprParser =  new ConstExprParser($config);
    $parser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser);

    $tokens = new TokenIterator($lexer->tokenize($docblock));
    $phpDocNode = $parser->parse($tokens);
    $varTags = $phpDocNode->getVarTagValues();

    // Not sure if a var tag can have more than 1?
    foreach ($varTags as $varTag) {
        if($varTag->type instanceof IdentifierTypeNode){
            return ['type' => $varTag->type->name, 'description' => $varTag->description?:null];
        }
    }
}

But there probably need to be changes in some more places to handle cases like NullableTypeNode and UnionTypeNode. Since the mapNativeType is also a bit rudimentary

@DerManoMann
Copy link
Collaborator

Yep, gets tricky pretty quick. Good for adding support for multiple types, though.

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

2 participants