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

Global unified configuration suggestions #1623

Open
caixingyue opened this issue Jul 8, 2024 · 0 comments
Open

Global unified configuration suggestions #1623

caixingyue opened this issue Jul 8, 2024 · 0 comments

Comments

@caixingyue
Copy link

I have used the Java springdoc-openapi-starter-webmvc-ui package. In the global configuration, I think it is better than the existing PHP swagger package configuration scheme.

This is a solution that I conceived based on the Java swagger configuration. Based on this configuration, multiple YAMLs will be generated. Swagger UI will support switching between multiple configurations.

Java swagger also supports single YAML. It supports both schemes. It seems that it is just the result of different annotations. If necessary, I can also provide relevant references.

tip: The final generated YAML is in line with the swagger specification, but in terms of user and dev experience, it will be more unified and convenient.

#[OA\Configuration]
class OpenApi
{
    #[OA\GroupedOpenApi]
    public function partnerApi()
    {
        return GroupedOpenApi::builder()
            ->group("partner")
            ->pathsToMatch("/api/**")
            ->addOpenApiCustomizer(function ($openApi) {
                $openApi->getInfo()->setTitle("Partner API");
                $openApi->getInfo()->setVersion("1.1");
                $openApi->getInfo()->setDescription("This interface document is only provided to Xingrui's internal partners. Some interfaces require relevant permissions to be opened.");
            })
            ->addOperationCustomizer(function ($operation, $handlerMethod) {
                $operation->addParametersItem((new OA\Parameter())
                    ->in('header')
                    ->name('Authorization')
                    ->description("Authorization token")
                    ->required(true)
                    ->schema(type: 'string')
                );

                return $operation;
            })
            ->addOperationCustomizer($this->globalResponseMessages())
            ->build();
    }

    #[OA\GroupedOpenApi]
    public function merchantApi()
    {
        return GroupedOpenApi::builder()
            ->group("merchant")
            ->pathsToMatch("/merchantApi/**")
            ->addOpenApiCustomizer(function ($openApi) {
                $openApi->getInfo()->setTitle("Merchant API");
                $openApi->getInfo()->setVersion("1.0");
                $openApi->getInfo()->setDescription("This interface document is provided to all merchants. Some interfaces require relevant permissions to be enabled.");
            })
            ->addOperationCustomizer(function ($operation, $handlerMethod) {
                $operation->addParametersItem((new OA\Parameter())
                    ->in('header')
                    ->name('Authorization')
                    ->description("Authorization token")
                    ->required(true)
                    ->schema(type: 'string')
                );

                return $operation;
            })
            ->addOperationCustomizer($this->globalResponseMessages())
            ->build();
    }

    private function globalResponseMessages() {
        return function ($operation, $handlerMethod) {
            $method = $handlerMethod->getMethod();
            $operationAnnotation = $method->getAnnotation(Operation::class);

            $operation->getResponses()->addApiResponse(new OA\Response(response: 404, description: 'Resource not found'));

            return $operation;
        };
    }
}

@caixingyue caixingyue changed the title This interface document is provided to all merchants. Some interfaces require relevant permissions to be enabled. Global unified configuration suggestions Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants