Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MelleD committed Mar 26, 2024
1 parent 5c3f07d commit ffedfb1
Show file tree
Hide file tree
Showing 23 changed files with 465 additions and 77 deletions.
9 changes: 9 additions & 0 deletions bin/configs/spring-cloud-auth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: spring
outputDir: samples/client/petstore/spring-cloud-auth
library: spring-cloud
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-auth.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
documentationProvider: none
artifactId: petstore-spring-cloud-auth
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CodegenSecurity {
// Oauth specific
public String flow, authorizationUrl, tokenUrl, refreshUrl;
public List<Map<String, Object>> scopes;
public Boolean hasScopes;

public Boolean isCode, isPassword, isApplication, isImplicit;
// OpenId specific
public String openIdConnectUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6940,6 +6940,7 @@ private void setOauth2Info(CodegenSecurity codegenSecurity, OAuthFlow flow) {
scopes.add(scope);
}
codegenSecurity.scopes = scopes;
codegenSecurity.hasScopes = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class ClientPropertiesConfiguration {
{{#isOAuth}}
final Properties {{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}} = new Properties();
{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.put("spring.security.oauth2.client.registration.{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.client-id", "set-{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}-client-id" );
{{#scopes}}
{{#hasScopes}}
{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.put("{{#scopes}}{{#-first}}spring.security.oauth2.client.registration.{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.scope", "{{/-first}}{{scope}}{{^-last}},{{/-last}}{{/scopes}}" );
{{/scopes}}
{{/hasScopes}}
{{#isCode}}
{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.put("spring.security.oauth2.client.registration.{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.authorization-grant-type", "authorization_code" );
{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.put("spring.security.oauth2.client.registration.{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}.redirect-uri", "set-{{{name}}}{{#lambda.pascalcase}}{{{flow}}}{{/lambda.pascalcase}}-redirect-uri" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1198,7 +1199,7 @@ public void testGenerationOfClientPropertiesConfigurationForOAuth() throws Excep
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_18090.yaml", null, new ParseOptions()).getOpenAPI();
.readLocation("src/test/resources/3_0/spring/petstore-auth.yaml", null, new ParseOptions()).getOpenAPI();

final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
Expand Down Expand Up @@ -1226,9 +1227,23 @@ public void testGenerationOfClientPropertiesConfigurationForOAuth() throws Excep

Path filePath = Paths.get(output.getAbsolutePath(), "src/main/java/org/openapitools/configuration/ClientPropertiesConfiguration.java");

String content = new String(Files.readAllBytes(filePath), "UTF-8");
Assert.assertTrue(content.contains("properties.put(\"spring.security.oauth2.client.provider.oAuth2AccessCode.token-uri\", \"${tokenUrl}\" );"));
Assert.assertTrue(content.contains("properties.put(\"spring.security.oauth2.client.provider.oAuth2AccessCode.authorization-uri\", \"${authorizationUrl}\" );"));

assertFileContains(filePath,
"oAuth2AccessCode.put(\"spring.security.oauth2.client.registration.oAuth2AccessCode.redirect-uri\", \"set-oAuth2AccessCode-redirect-uri\" );",
"oAuth2AccessCode.put(\"spring.security.oauth2.client.registration.oAuth2AccessCode.authorization-grant-type\", \"authorization_code\" );",
"oAuth2AccessCode.put(\"spring.security.oauth2.client.registration.oAuth2AccessCode.client-id\", \"set-oAuth2AccessCode-client-id\" );",
"oAuth2AccessCode.put(\"spring.security.oauth2.client.registration.oAuth2AccessCode.scope\", \"openid,profile,aud\" );",
"oAuth2AccessCode.put(\"spring.security.oauth2.client.provider.oAuth2AccessCode.token-uri\", \"${tokenUrl}\" );",
"oAuth2AccessCode.put(\"spring.security.oauth2.client.provider.oAuth2AccessCode.authorization-uri\", \"${authorizationUrl}\" );",


"oAuth2Application.put(\"spring.security.oauth2.client.registration.oAuth2Application.client-id\", \"set-oAuth2Application-client-id\" );",
"oAuth2Application.put(\"spring.security.oauth2.client.registration.oAuth2Application.authorization-grant-type\", \"client_credentials\" );",
"oAuth2Application.put(\"spring.security.oauth2.client.provider.oAuth2Application.token-uri\", \"/openid-connect/token\" );"

);

assertFileNotContains(filePath,"spring.security.oauth2.client.registration.oAuth2Application.scope");
}

@Test
Expand Down
29 changes: 0 additions & 29 deletions modules/openapi-generator/src/test/resources/3_0/issue_18090.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: "3.0.3"
info:
description: 'This spec is mainly for testing Petstore server and contains fake
endpoints, models. Please do not use this for any other purpose. Special
characters: " \'
version: 1.0.0
title: OpenAPI Petstore Auth
license:
name: Apache-2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html

security:
- OAuth2: [openid, profile, aud]

paths:
/some/endpoint:
get:
responses:
"200":
description: OK

components:
securitySchemes:
OAuth2:
type: oauth2
x-tokenName: id_token
flows:
authorizationCode:
authorizationUrl: "${authorizationUrl}"
tokenUrl: "${tokenUrl}"
scopes:
openid: Access OpenId Connect info
profile: Profile info
aud: Audience info
clientCredentials:
tokenUrl: '/openid-connect/token'
scopes: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
README.md
pom.xml
src/main/java/org/openapitools/api/SomeApi.java
src/main/java/org/openapitools/api/SomeApiClient.java
src/main/java/org/openapitools/configuration/ApiKeyRequestInterceptor.java
src/main/java/org/openapitools/configuration/ClientConfiguration.java
src/main/java/org/openapitools/configuration/ClientPropertiesConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.5.0-SNAPSHOT
53 changes: 53 additions & 0 deletions samples/client/petstore/spring-cloud-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# petstore-spring-cloud-auth

## Requirements

Building the API client library requires [Maven](https://maven.apache.org/) to be installed.

## Installation

To install the API client library to your local Maven repository, simply execute:

```shell
mvn install
```

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

```shell
mvn deploy
```

Refer to the [official documentation](https://maven.apache.org/plugins/maven-deploy-plugin/usage.html) for more information.

### Maven users

Add this dependency to your project's POM:

```xml
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>petstore-spring-cloud-auth</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
```

### Gradle users

Add this dependency to your project's build file:

```groovy
compile "org.openapitools:petstore-spring-cloud-auth:1.0.0"
```

### Others

At first generate the JAR by executing:

mvn package

Then manually install the following JARs:

* target/petstore-spring-cloud-auth-1.0.0.jar
* target/lib/*.jar
75 changes: 75 additions & 0 deletions samples/client/petstore/spring-cloud-auth/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>petstore-spring-cloud-auth</artifactId>
<packaging>jar</packaging>
<name>petstore-spring-cloud-auth</name>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
<version>5.7.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (7.5.0-SNAPSHOT).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.api;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Generated;

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
@Validated
public interface SomeApi {

/**
* GET /some/endpoint
*
* @return OK (status code 200)
*/
@RequestMapping(
method = RequestMethod.GET,
value = "/some/endpoint"
)

ResponseEntity<Void> someEndpointGet(

);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openapitools.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.openapitools.configuration.ClientConfiguration;

@FeignClient(name="${some.name:some}", url="${some.url:http://localhost}", configuration = ClientConfiguration.class)
public interface SomeApiClient extends SomeApi {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.openapitools.configuration;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Util;


public class ApiKeyRequestInterceptor implements RequestInterceptor {
private final String location;
private final String name;
private String value;

public ApiKeyRequestInterceptor(String location, String name, String value) {
Util.checkNotNull(location, "location", new Object[0]);
Util.checkNotNull(name, "name", new Object[0]);
Util.checkNotNull(value, "value", new Object[0]);
this.location = location;
this.name = name;
this.value = value;
}

@Override
public void apply(RequestTemplate requestTemplate) {
if(location.equals("header")) {
requestTemplate.header(name, value);
} else if(location.equals("query")) {
requestTemplate.query(name, value);
}
}

}
Loading

0 comments on commit ffedfb1

Please sign in to comment.