OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.5k forks source link

How to get the request path in code. #18704

Open kush20 opened 4 months ago

kush20 commented 4 months ago

While implementing a POST body.

I want to return status of 201 Created along with the URI. However, how do i get the request-path in the first place..

@Override
public ResponseEntity<Void> createPet (Pet p ) {  
     Pet p1 = petRepository.save(p);
     return ResponseEntity.created("  request-path..  ?? "  + p1.id);   // <-- how can i get the input path here
}

i dont want to hard-code request-path.

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>7.5.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpecRootDirectory> ${project.basedir}/src/main/resources/openapi
        </inputSpecRootDirectory>
        <generatorName>spring</generatorName>
        <apiPackage>com.example.openapi.api</apiPackage>
        <modelPackage>com.example.openapi.model</modelPackage>
        <configOptions>
          <modelSuffix>Dto</modelSuffix>
          <useJakartaEe>true</useJakartaEe>
          <library>spring-boot</library>
          <delegatePattern>true</delegatePattern>
          <hideGenerationTimestamp>true</hideGenerationTimestamp>
          <interfaceOnly>true</interfaceOnly>
          <useSpringBoot3>true</useSpringBoot3>
          <useSpringfox>false</useSpringfox>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

stefankoppier commented 4 months ago

Hi @kush20. I might misinterpred your question, but if your question is how to get the request path of the request made to the spring controller. I don't think this is a bug of the generator. This is existing Spring functionality, you can use HttpServletRequest to get the request path of the current request handling thread.