Skip to content

Commit

Permalink
swagger-api#443 - Handling required parameters during swagger type di…
Browse files Browse the repository at this point in the history
…scovery
  • Loading branch information
cchafer committed Feb 23, 2015
1 parent bfc1ed3 commit 7a21711
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,9 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation

if(methodResponse != null) {
if (methodResponse.getSchema() != null) {
CodegenProperty cm = fromProperty("response", methodResponse.getSchema());

Property responseProperty = methodResponse.getSchema();
responseProperty.setRequired(true);
CodegenProperty cm = fromProperty("response", responseProperty);

if(responseProperty instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) responseProperty;
Expand Down Expand Up @@ -783,6 +783,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
LOGGER.warn("warning! Property type \"" + qp.getType() + "\" not found for parameter \"" + param.getName() + "\", using String");
property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + qp.getType() + " but not supported");
}
property.setRequired(param.getRequired());
CodegenProperty model = fromProperty(qp.getName(), property);
p.collectionFormat = collectionFormat;
p.dataType = model.datatype;
Expand All @@ -806,6 +807,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
else {
// TODO: missing format, so this will not always work
Property prop = PropertyBuilder.build(impl.getType(), null, null);
prop.setRequired(bp.getRequired());
CodegenProperty cp = fromProperty("property", prop);
if(cp != null) {
p.dataType = cp.datatype;
Expand All @@ -819,6 +821,7 @@ else if(model instanceof ArrayModel) {
CodegenModel cm = fromModel(bp.getName(), impl);
// get the single property
ArrayProperty ap = new ArrayProperty().items(impl.getItems());
ap.setRequired(param.getRequired());
CodegenProperty cp = fromProperty("inner", ap);
if(cp.complexType != null) {
imports.add(cp.complexType);
Expand Down
93 changes: 93 additions & 0 deletions modules/swagger-codegen/src/test/resources/2_0/requiredTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"swagger": "2.0",
"info": {
"description": "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters",
"version": "1.0.0",
"title": "Swagger Petstore",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"email": "apiteam@wordnik.com"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"host": "petstore.swagger.io",
"basePath": "/v2",
"schemes": [
"http"
],
"paths": {
"/tests/requiredParams": {
"get": {
"tags": [
"tests"
],
"summary": "Operation with required parameters",
"description": "",
"operationId": "requiredParams",
"produces": [
"application/json"
],
"parameters": [
{
"name": "param1",
"in": "formData",
"description": "Some required parameter",
"required": true,
"type": "integer",
"format": "int64"
},
{
"name": "param2",
"in": "formData",
"description": "Some optional parameter",
"required": false,
"type": "string"
}
],
"responses": {
"200": {
"description": "successful operation. Retuning a simple int.",
"schema": {
"type": "integer",
"format": "int64"
}
}
}
}
}
},
"securityDefinitions": {
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
},
"petstore_auth": {
"type": "oauth2",
"authorizationUrl": "http://petstore.swagger.io/api/oauth/dialog",
"flow": "implicit",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
},
"definitions": {
"CustomModel": {
"required": ["id"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string",
"example": "doggie"
}
}
}
}
}
40 changes: 31 additions & 9 deletions modules/swagger-codegen/src/test/scala/CodegenTest.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import com.wordnik.swagger.models._
import com.wordnik.swagger.util.Json
import io.swagger.parser._

import com.wordnik.swagger.codegen.DefaultCodegen
import java.util

import com.wordnik.swagger.codegen.{CodegenParameter, DefaultCodegen}
import com.wordnik.swagger.models.properties.Property
import io.swagger.parser._
import org.junit.runner.RunWith
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.Matchers

import scala.collection.JavaConverters._

@RunWith(classOf[JUnitRunner])
class CodegenTest extends FlatSpec with Matchers {
Expand Down Expand Up @@ -94,4 +90,30 @@ class CodegenTest extends FlatSpec with Matchers {
statusParam.required should equal (false)
statusParam.hasMore should be (null)
}

it should "handle required parameters from a 2.0 spec as required when figuring out Swagger types" in {
val model = new SwaggerParser()
.read("src/test/resources/2_0/requiredTest.json")

val codegen = new DefaultCodegen() {
override def getSwaggerType(p: Property) = Option(p) match {
case Some(property) if !property.getRequired =>
"Optional<" + super.getSwaggerType(p) + ">"
case other => super.getSwaggerType(p)
}
}
val path = "/tests/requiredParams"
val p = model.getPaths().get(path).getGet()
val op = codegen.fromOperation(path, "get", p)

val formParams = op.formParams
formParams.size should be (2)
val requiredParam = formParams.get(0)
requiredParam.dataType should be ("Long")

val optionalParam = formParams.get(1)
optionalParam.dataType should be ("Optional<string>")

op.returnType should be ("Long")
}
}

0 comments on commit 7a21711

Please sign in to comment.