Skip to content

Commit

Permalink
Skip model generation if it's a top-level map or array (#1296)
Browse files Browse the repository at this point in the history
* update samples

* remove string boolean map spec

* add logic to skip array alias being generated as model

* fix alias to array

* remove unused ruby files

* remove unused ruby (oas3) files

* unalias response schema

* fix NPE when no model defined

* fix ruby openapi3 script

* update samples

* add global openapi, schemas for unaliasing

* minor code cleanup/refactoring using globalSchemas

* Revert "minor code cleanup/refactoring using globalSchemas"

This reverts commit 20a2bbc.
  • Loading branch information
wing328 committed Nov 26, 2018
1 parent ce02538 commit 46a4ffe
Show file tree
Hide file tree
Showing 108 changed files with 92 additions and 5,999 deletions.
5 changes: 5 additions & 0 deletions bin/openapi3/ruby-client-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ then
mvn clean package
fi

# purge lib/doc folder
echo "purge ruby petstore lib, docs folder"
rm -Rf samples/openapi3/client/petstore/ruby/lib
rm -Rf samples/openapi3/client/petstore/ruby/docs

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/ruby-client -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ruby -c bin/ruby-petstore.json -o samples/openapi3/client/petstore/ruby -DskipFormModel=true $@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.swagger.v3.oas.models.servers.ServerVariable;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -261,4 +262,9 @@ public interface CodegenConfig {

public void setEnablePostProcessFile(boolean isEnablePostProcessFile);

// set OpenAPI and schemas
public void setGlobalOpenAPI(OpenAPI openAPI);

public void setGlobalSchemas(OpenAPI openAPI);

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public class DefaultCodegen implements CodegenConfig {
// flag to indicate whether to use environment variable to post process file
protected boolean enablePostProcessFile = false;

// make openapi and schemas available to all methods
protected OpenAPI globalOpenAPI;
protected Map<String, Schema> globalSchemas;

public List<CliOption> cliOptions() {
return cliOptions;
Expand Down Expand Up @@ -383,6 +386,32 @@ public String toEnumVarName(String value, String datatype) {
}
}


/**
* Set global OpenAPI based on OpenAPI object
*
* @param openAPI OpenAPI object
*/
public void setGlobalOpenAPI(OpenAPI openAPI) {
this.globalOpenAPI = openAPI;
}


/**
* Set global schema based on OpenAPI object
*
* @param openAPI OpenAPI object
*/
public void setGlobalSchemas(OpenAPI openAPI) {
if (openAPI != null && openAPI.getComponents() != null) {
this.globalSchemas = openAPI.getComponents().getSchemas();
}

if (this.globalSchemas == null) { // initalize with empty map if it's null
this.globalSchemas = new HashMap<String, Schema>();
}
}

// override with any special post-processing
@SuppressWarnings("static-method")
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Expand Down Expand Up @@ -1762,6 +1791,7 @@ public String getterAndSetterCapitalize(String name) {
return org.openapitools.codegen.utils.StringUtils.camelize(toVarName(name));
}


/**
* Convert OAS Property object to Codegen Property object
*
Expand All @@ -1775,6 +1805,10 @@ public CodegenProperty fromProperty(String name, Schema p) {
return null;
}
LOGGER.debug("debugging fromProperty for " + name + " : " + p);

// unalias schema
p = ModelUtils.unaliasSchema(globalSchemas, p);

CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
property.name = toVarName(name);
property.baseName = name;
Expand Down Expand Up @@ -2003,7 +2037,8 @@ public CodegenProperty fromProperty(String name, Schema p) {
if (itemName == null) {
itemName = property.name;
}
CodegenProperty cp = fromProperty(itemName, ((ArraySchema) p).getItems());
Schema innerSchema = ModelUtils.unaliasSchema(globalSchemas, ((ArraySchema) p).getItems());
CodegenProperty cp = fromProperty(itemName, innerSchema);
updatePropertyForArray(property, cp);
} else if (ModelUtils.isMapSchema(p)) {
property.isContainer = true;
Expand All @@ -2014,7 +2049,8 @@ public CodegenProperty fromProperty(String name, Schema p) {
property.maxItems = p.getMaxProperties();

// handle inner property
CodegenProperty cp = fromProperty("inner", ModelUtils.getAdditionalProperties(p));
Schema innerSchema = ModelUtils.unaliasSchema(globalSchemas, ModelUtils.getAdditionalProperties(p));
CodegenProperty cp = fromProperty("inner", innerSchema);
updatePropertyForMap(property, cp);
} else if (ModelUtils.isFreeFormObject(p)) {
property.isFreeFormObject = true;
Expand Down Expand Up @@ -2308,10 +2344,8 @@ public CodegenOperation fromOperation(String path,
op.responses.get(op.responses.size() - 1).hasMore = false;

if (methodResponse != null) {
Schema responseSchema = ModelUtils.getSchemaFromResponse(methodResponse);
if (openAPI != null && openAPI.getComponents() != null) { // has models/aliases defined
responseSchema = ModelUtils.unaliasSchema(openAPI.getComponents().getSchemas(), responseSchema);
}
Schema responseSchema = ModelUtils.unaliasSchema(globalSchemas, ModelUtils.getSchemaFromResponse(methodResponse));

if (responseSchema != null) {
CodegenProperty cm = fromProperty("response", responseSchema);

Expand Down Expand Up @@ -2559,7 +2593,12 @@ public CodegenResponse fromResponse(OpenAPI openAPI, String responseCode, ApiRes
} else {
r.code = responseCode;
}
final Schema responseSchema = ModelUtils.getSchemaFromResponse(response);
Schema responseSchema;
if (openAPI != null && openAPI.getComponents() != null) {
responseSchema = ModelUtils.unaliasSchema(openAPI.getComponents().getSchemas(), ModelUtils.getSchemaFromResponse(response));
} else { // no model/alias defined
responseSchema = ModelUtils.getSchemaFromResponse(response);
}
r.schema = responseSchema;
r.message = escapeText(response.getDescription());
// TODO need to revise and test examples in responses
Expand All @@ -2573,6 +2612,7 @@ public CodegenResponse fromResponse(OpenAPI openAPI, String responseCode, ApiRes
r.hasHeaders = !r.headers.isEmpty();

if (r.schema != null) {
Map<String, Schema> allSchemas = null;
CodegenProperty cp = fromProperty("response", responseSchema);

if (ModelUtils.isArraySchema(responseSchema)) {
Expand Down Expand Up @@ -3209,6 +3249,7 @@ private void addHeaders(OpenAPI openAPI, ApiResponse response, List<CodegenPrope
String description = headers.getValue().getDescription();
// follow the $ref
Header header = ModelUtils.getReferencedHeader(openAPI, headers.getValue());

CodegenProperty cp = fromProperty(headers.getKey(), header.getSchema());
cp.setDescription(escapeText(description));
cp.setUnescapedDescription(description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.ignore.CodegenIgnoreProcessor;
//import org.openapitools.codegen.languages.AbstractJavaCodegen;
import org.openapitools.codegen.utils.ImplementationVersion;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.URLPathUtils;
Expand Down Expand Up @@ -181,6 +180,11 @@ private void configureGeneratorProperties() {

config.processOpts();
config.preprocessOpenAPI(openAPI);

// set OpenAPI and schemas to make these available to all methods
config.setGlobalOpenAPI(openAPI);
config.setGlobalSchemas(openAPI);

config.additionalProperties().put("generatorVersion", ImplementationVersion.read());
config.additionalProperties().put("generatedDate", ZonedDateTime.now().toString());
config.additionalProperties().put("generatedYear", String.valueOf(ZonedDateTime.now().getYear()));
Expand Down Expand Up @@ -424,6 +428,25 @@ private Model getParent(Model model) {
}

Schema schema = schemas.get(name);

// check to see if it's a "map" model
if (ModelUtils.isMapSchema(schema)) {
if (schema.getProperties() == null || schema.getProperties().isEmpty()) {
// schema without property, i.e. alias to map
LOGGER.info("Model " + name + " not generated since it's an alias to map (without property)");
continue;
}
}

// check to see if it's an "array" model
if (ModelUtils.isArraySchema(schema)) {
if (schema.getProperties() == null || schema.getProperties().isEmpty()) {
// schema without property, i.e. alias to array
LOGGER.info("Model " + name + " not generated since it's an alias to array (without property)");
continue;
}
}

Map<String, Schema> schemaMap = new HashMap<>();
schemaMap.put(name, schema);
Map<String, Object> models = processModels(config, schemaMap, schemas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,22 +771,15 @@ public static Schema unaliasSchema(Map<String, Schema> allSchemas, Schema schema
return schema;
} else if (isFreeFormObject(ref)) {
return schema;
} else if (isArraySchema(ref) || isComposedSchema(ref)) { // array def should be created as models
} else if (isArraySchema(ref)) {
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
} else if (isComposedSchema(ref)) {
return schema;
} else if (isMapSchema(ref)) {
if (ref.getProperties() != null && !ref.getProperties().isEmpty()) // has properties
return schema; // treat it as model
else {
// treat it as a typical map
/* TODO unalias the map item if it's an alias
if (ref.getAdditionalProperties() != null) {
Schema innerSchema = (Schema) ref.getAdditionalProperties();
if (StringUtils.isNotEmpty(innerSchema.get$ref())) { // map item is a ref to something else
//Schema unaliasInnerSchema = unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(innerSchema.get$ref())));
//ref.setAdditionalProperties(unaliasInnerSchema);
}
}*/
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
}
} else if (isObjectSchema(ref)) { // model
Expand Down
2 changes: 0 additions & 2 deletions samples/client/petstore/csharp/OpenAPIClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ Class | Method | HTTP request | Description

- [Model.AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
- [Model.Animal](docs/Animal.md)
- [Model.AnimalFarm](docs/AnimalFarm.md)
- [Model.ApiResponse](docs/ApiResponse.md)
- [Model.ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [Model.ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
Expand Down Expand Up @@ -169,7 +168,6 @@ Class | Method | HTTP request | Description
- [Model.ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [Model.Return](docs/Return.md)
- [Model.SpecialModelName](docs/SpecialModelName.md)
- [Model.StringBooleanMap](docs/StringBooleanMap.md)
- [Model.Tag](docs/Tag.md)
- [Model.User](docs/User.md)

Expand Down
2 changes: 0 additions & 2 deletions samples/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Class | Method | HTTP request | Description

- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
- [Animal](docs/Animal.md)
- [AnimalFarm](docs/AnimalFarm.md)
- [ApiResponse](docs/ApiResponse.md)
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
Expand Down Expand Up @@ -102,7 +101,6 @@ Class | Method | HTTP request | Description
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [Return](docs/Return.md)
- [SpecialModelName](docs/SpecialModelName.md)
- [StringBooleanMap](docs/StringBooleanMap.md)
- [Tag](docs/Tag.md)
- [User](docs/User.md)

Expand Down
9 changes: 0 additions & 9 deletions samples/client/petstore/go/go-petstore/docs/AnimalFarm.md

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions samples/client/petstore/go/go-petstore/model_animal_farm.go

This file was deleted.

13 changes: 0 additions & 13 deletions samples/client/petstore/go/go-petstore/model_string_boolean_map.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -278,34 +278,6 @@ mkAnimal animalClassName =
, animalColor = Nothing
}

-- ** AnimalFarm
-- | AnimalFarm
data AnimalFarm = AnimalFarm
{
} deriving (P.Show, P.Eq, P.Typeable)

-- | FromJSON AnimalFarm
instance A.FromJSON AnimalFarm where
parseJSON = A.withObject "AnimalFarm" $ \o ->
pure AnimalFarm


-- | ToJSON AnimalFarm
instance A.ToJSON AnimalFarm where
toJSON AnimalFarm =
_omitNulls
[
]


-- | Construct a value of type 'AnimalFarm' (by applying it's required fields, if any)
mkAnimalFarm
:: AnimalFarm
mkAnimalFarm =
AnimalFarm
{
}

-- ** ApiResponse
-- | ApiResponse
data ApiResponse = ApiResponse
Expand Down Expand Up @@ -1326,34 +1298,6 @@ mkSpecialModelName =
{ specialModelNameSpecialPropertyName = Nothing
}

-- ** StringBooleanMap
-- | StringBooleanMap
data StringBooleanMap = StringBooleanMap
{
} deriving (P.Show, P.Eq, P.Typeable)

-- | FromJSON StringBooleanMap
instance A.FromJSON StringBooleanMap where
parseJSON = A.withObject "StringBooleanMap" $ \o ->
pure StringBooleanMap


-- | ToJSON StringBooleanMap
instance A.ToJSON StringBooleanMap where
toJSON StringBooleanMap =
_omitNulls
[
]


-- | Construct a value of type 'StringBooleanMap' (by applying it's required fields, if any)
mkStringBooleanMap
:: StringBooleanMap
mkStringBooleanMap =
StringBooleanMap
{
}

-- ** Tag
-- | Tag
data Tag = Tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ animalColorL f Animal{..} = (\animalColor -> Animal { animalColor, ..} ) <$> f a



-- * AnimalFarm



-- * ApiResponse

-- | 'apiResponseCode' Lens
Expand Down Expand Up @@ -603,10 +599,6 @@ specialModelNameSpecialPropertyNameL f SpecialModelName{..} = (\specialModelName



-- * StringBooleanMap



-- * Tag

-- | 'tagId' Lens
Expand Down
Loading

0 comments on commit 46a4ffe

Please sign in to comment.