Skip to content

#21 #25

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

Merged
merged 3 commits into from
Nov 17, 2019
Merged

#21 #25

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.github.hauner.openapi.spring.converter

import com.github.hauner.openapi.spring.model.Api
import com.github.hauner.openapi.spring.model.Endpoint
import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter
import com.github.hauner.openapi.spring.model.parameters.PathParameter
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
import com.github.hauner.openapi.spring.model.Response
import com.github.hauner.openapi.spring.model.datatypes.DataType
Expand Down Expand Up @@ -111,13 +113,21 @@ class ApiConverter {
}
}

private QueryParameter createParameter (Parameter parameter, Api target, resolver) {
private ModelParameter createParameter (Parameter parameter, Api target, resolver) {
def info = new SchemaInfo (parameter.schema, parameter.name)
info.resolver = resolver

DataType dataType = dataTypeConverter.convert (info, target.models)

new QueryParameter(name: parameter.name, required: parameter.required, dataType: dataType)
switch (parameter.in) {
case 'query':
return new QueryParameter (name: parameter.name, required: parameter.required, dataType: dataType)
case 'path':
return new PathParameter (name: parameter.name, required: parameter.required, dataType: dataType)
default:
// todo throw
null
}
}

private String getInlineResponseName (String path, String httpStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.github.hauner.openapi.spring.model

import com.github.hauner.openapi.spring.model.parameters.QueryParameter
import com.github.hauner.openapi.spring.model.parameters.Parameter

/**
* Endpoint properties.
Expand All @@ -28,7 +28,7 @@ class Endpoint {
HttpMethod method

List<Response> responses = []
List<QueryParameter> parameters = []
List<Parameter> parameters = []

Response getResponse () {
responses.first ()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2019 the original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.hauner.openapi.spring.model.parameters

/**
* Common parameter interface implemented by all OpenAPI parameter types.
*
* @author Martin Hauner
*/
interface Parameter {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2019 the original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.hauner.openapi.spring.model.parameters

import com.github.hauner.openapi.spring.model.datatypes.DataType

/**
* OpenAPI path parameter.
*
* @author Martin Hauner
*/
class PathParameter implements Parameter {
String name
boolean required
DataType dataType

String getAnnotationName () {
"PathVariable"
}

String getAnnotationWithPackage () {
"org.springframework.web.bind.annotation.${annotationName}"
}

String getAnnotation () {
"@${annotationName}"
}

Set<String> getDataTypeImports () {
dataType.imports
}

/**
* Is the parameter required?
*
* @return true if the parameter is required, otherwise false.
*/
boolean isRequired () {
required
}

/**
* Create annotation?
*/
boolean withAnnotation () {
true
}

/**
* Create annotation with parameters?
*
* @return true if the annotation should have parameters, false otherwise
*/
boolean withParameters () {
true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
*
* @author Martin Hauner
*/
class QueryParameter {
class QueryParameter implements Parameter {
String name
boolean required
DataType dataType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.github.hauner.openapi.spring.writer

import com.github.hauner.openapi.spring.model.Endpoint
import com.github.hauner.openapi.spring.model.parameters.QueryParameter
import com.github.hauner.openapi.spring.model.parameters.Parameter
import com.github.hauner.openapi.support.Identifier

/**
Expand Down Expand Up @@ -48,11 +48,11 @@ class MethodWriter {
mapping
}

private String createParameterAnnotation (QueryParameter parameter) {
private String createParameterAnnotation (Parameter parameter) {
String param = "${parameter.annotation}"

if (! parameter.withParameters ()) {
return param;
return param
}

param += '('
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ paths:
param.name == 'foo'
param.required
param.dataType.name == 'String'
param.annotation == '@RequestParam'
param.annotationWithPackage == 'org.springframework.web.bind.annotation.RequestParam'
}

void "converts simple path parameter"() {
def openApi = parse (
"""\
openapi: 3.0.2
info:
title: test simple path parameter
version: 1.0.0

paths:
/endpoint/{foo}:

get:
tags:
- endpoint
parameters:
- name: foo
description: path, required, string
in: path
required: true
schema:
type: string
responses:
'204':
description: empty
""")

when:
def api = new ApiConverter ().convert (openApi)

then:
def itf = api.interfaces.first ()
def ep = itf.endpoints.first ()
def param = ep.parameters.first ()
param.name == 'foo'
param.required
param.dataType.name == 'String'
param.annotation == '@PathVariable'
param.annotationWithPackage == 'org.springframework.web.bind.annotation.PathVariable'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.github.hauner.openapi.generatr

import com.github.hauner.openapi.spring.generatr.ApiOptions
import com.github.hauner.openapi.spring.generatr.mapping.ArrayTypeMapping
import com.github.hauner.openapi.spring.generatr.mapping.EndpointTypeMapping
import com.github.hauner.openapi.spring.generatr.mapping.ResponseTypeMapping
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

Expand All @@ -30,29 +27,7 @@ class GeneratrPendingTest extends GeneratrTestBase {
@Parameterized.Parameters(name = "{0}")
static Collection<TestSet> sources () {
return [
// new TestSet(data: 'params-complex-data-types')
new TestSet(name: 'response-array-data-type-mapping',
options: new ApiOptions(
typeMappings: [
new ArrayTypeMapping(
targetTypeName: 'java.util.Collection'
),
new ResponseTypeMapping (
contentType: 'application/vnd.global-response',
sourceTypeName: 'array',
targetTypeName: 'java.util.List'
),
new EndpointTypeMapping (
path: '/array-endpoint-response',
typeMappings: [
new ResponseTypeMapping (
contentType: 'application/vnd.any',
sourceTypeName: 'array',
targetTypeName: 'java.util.Set')]
)
]
)
)
new TestSet(name: 'path-params-simple-data-types')
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This class is auto generated by https://github.com/hauner/openapi-generatr-spring.
* DO NOT EDIT.
*/

package generated.api;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

public interface EndpointApi {

@GetMapping(path = "/endpoint/{foo}")
ResponseEntity<void> getEndpointFoo(@PathVariable(name = "foo") String foo);

@GetMapping(path = "/endpoint-optional/{foo}")
ResponseEntity<void> getEndpointOptionalFoo(@PathVariable(name = "foo", required = false) String foo);

}
35 changes: 35 additions & 0 deletions src/testInt/resources/path-params-simple-data-types/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.2
info:
title: test simple path parameters
version: 1.0.0

paths:
/endpoint/{foo}:
get:
tags:
- endpoint
parameters:
- name: foo
description: path, required, string
in: path
required: true
schema:
type: string
responses:
'204':
description: empty

/endpoint-optional/{foo}:
get:
tags:
- endpoint
parameters:
- name: foo
description: path, not required, string
in: path
required: false
schema:
type: string
responses:
'204':
description: empty