Skip to content

Commit

Permalink
fix ()
Browse files Browse the repository at this point in the history
  • Loading branch information
nhomble committed May 12, 2020
1 parent 4c58a2c commit b98d925
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public interface {{classname}} {
{{/implicitHeaders}}
{{#hasAuthMethods}}
{{#useSpringSecurity}}
@PreAuthorize("{{#authMethods}}{{#isOAuth}}({{#scopes}}hasAuthority('{{scope}}'){{#hasMore}} and {{/hasMore}}{{/scopes}}{{/isOAuth}}{{#isBasicBearer}}{{#scopes}}hasAuthority('{{scope}}'){{#hasMore}} and {{/hasMore}}{{/scopes}}{{^scopes}}){{/scopes}}{{/isBasicBearer}}{{#hasMore}} or {{/hasMore}}{{/authMethods}}")
@PreAuthorize("{{#authMethods}}{{#isOAuth}}({{#scopes}}hasAuthority('{{scope}}'){{#hasMore}} and {{/hasMore}}{{/scopes}}){{/isOAuth}}{{#isBasicBearer}}({{#scopes}}hasAuthority('{{scope}}'){{#hasMore}} and {{/hasMore}}{{/scopes}}){{/isBasicBearer}}{{#hasMore}} or {{/hasMore}}{{/authMethods}}")
{{/useSpringSecurity}}
{{/hasAuthMethods}}
@RequestMapping(value = "{{{path}}}",{{#singleContentTypes}}{{#hasProduces}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public void testPreAuthorizeOnOauth() throws IOException {

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"(hasAuthority('write_pets') and hasAuthority('rw_pets') or (hasAuthority('read_pets')\")");
"@PreAuthorize(\"(hasAuthority('write_pets') and hasAuthority('rw_pets')) or (hasAuthority('read_pets'))\")");
}

@Test
Expand All @@ -621,7 +621,7 @@ public void testPreAuthorizeOnGlobalOauth() throws IOException {

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"(hasAuthority('read_pets')\")");
"@PreAuthorize(\"(hasAuthority('read_pets'))\")");
}

@Test
Expand Down Expand Up @@ -676,7 +676,7 @@ public void testPreAuthorizeOnJwtAuth() throws IOException {

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"hasAuthority('scope:another') or hasAuthority('scope:specific')\")");
"@PreAuthorize(\"(hasAuthority('scope:another')) or (hasAuthority('scope:specific'))\")");
}

@Test
Expand Down Expand Up @@ -728,6 +728,32 @@ public void testPreAuthorizeOnGlobalJwtAuth() throws IOException {

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"hasAuthority('scope:global')\")");
"@PreAuthorize(\"(hasAuthority('scope:global'))\")");
}

@Test
public void testTwoOauthScopes() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/two-oauth-scopes.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.additionalProperties().put(SpringCodegen.USE_SPRING_SECURITY, "true");

ClientOptInput input = new ClientOptInput();
input.setOpenAPI(openAPI);
input.setConfig(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"(hasAuthority('user') and hasAuthority('admin'))\")");
}
}
127 changes: 127 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/two-oauth-scopes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://localhost:8081/
security:
- OAuth2:
- user
- admin
paths:
/pets:
get:
summary: List all pets
security:
- OAuth2: [admin] # Use OAuth with a different scope
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
securitySchemes:
OAuth2:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://example.com/oauth/authorize
tokenUrl: https://example.com/oauth/token
scopes:
user: Grants user operations
admin: Grants access to admin operations
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public interface FakeApi {
}, tags={ "pet", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = ModelApiResponse.class) })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/fake/{petId}/uploadImageWithRequiredFile",
produces = { "application/json" },
consumes = { "multipart/form-data" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface PetApi {
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation"),
@ApiResponse(code = 405, message = "Invalid input") })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet",
consumes = { "application/json", "application/xml" },
method = RequestMethod.POST)
Expand All @@ -71,7 +71,7 @@ public interface PetApi {
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation"),
@ApiResponse(code = 400, message = "Invalid pet value") })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet/{petId}",
method = RequestMethod.DELETE)
ResponseEntity<Void> deletePet(@ApiParam(value = "Pet id to delete",required=true) @PathVariable("petId") Long petId,@ApiParam(value = "" ) @RequestHeader(value="api_key", required=false) String apiKey);
Expand All @@ -94,7 +94,7 @@ public interface PetApi {
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = Pet.class, responseContainer = "List"),
@ApiResponse(code = 400, message = "Invalid status value") })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet/findByStatus",
produces = { "application/xml", "application/json" },
method = RequestMethod.GET)
Expand All @@ -119,7 +119,7 @@ public interface PetApi {
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = Pet.class, responseContainer = "List"),
@ApiResponse(code = 400, message = "Invalid tag value") })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet/findByTags",
produces = { "application/xml", "application/json" },
method = RequestMethod.GET)
Expand Down Expand Up @@ -169,7 +169,7 @@ public interface PetApi {
@ApiResponse(code = 400, message = "Invalid ID supplied"),
@ApiResponse(code = 404, message = "Pet not found"),
@ApiResponse(code = 405, message = "Validation exception") })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet",
consumes = { "application/json", "application/xml" },
method = RequestMethod.PUT)
Expand All @@ -192,7 +192,7 @@ public interface PetApi {
}, tags={ "pet", })
@ApiResponses(value = {
@ApiResponse(code = 405, message = "Invalid input") })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet/{petId}",
consumes = { "application/x-www-form-urlencoded" },
method = RequestMethod.POST)
Expand All @@ -215,7 +215,7 @@ public interface PetApi {
}, tags={ "pet", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = ModelApiResponse.class) })
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets')")
@PreAuthorize("(hasAuthority('write:pets') and hasAuthority('read:pets'))")
@RequestMapping(value = "/pet/{petId}/uploadImage",
produces = { "application/json" },
consumes = { "multipart/form-data" },
Expand Down

0 comments on commit b98d925

Please sign in to comment.