View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.schemagen.json.schema;
28  
29  import com.fasterxml.jackson.databind.node.ArrayNode;
30  import com.fasterxml.jackson.databind.node.JsonNodeFactory;
31  import com.fasterxml.jackson.databind.node.ObjectNode;
32  
33  import gov.nist.secauto.metaschema.core.model.IValuedDefinition;
34  import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValue;
35  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
36  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
37  import gov.nist.secauto.metaschema.schemagen.AbstractGenerationState.AllowedValueCollection;
38  import gov.nist.secauto.metaschema.schemagen.json.impl.JsonGenerationState;
39  
40  import java.math.BigDecimal;
41  import java.math.BigInteger;
42  import java.math.MathContext;
43  
44  import edu.umd.cs.findbugs.annotations.NonNull;
45  
46  public class DataTypeRestrictionDefinitionJsonSchema
47      extends AbstractDefineableJsonSchema {
48    @NonNull
49    private final IValuedDefinition definition;
50    @NonNull
51    private final AllowedValueCollection allowedValuesCollection;
52  
53    public DataTypeRestrictionDefinitionJsonSchema(
54        @NonNull IValuedDefinition definition,
55        @NonNull AllowedValueCollection allowedValuesCollection) {
56      this.definition = definition;
57      CollectionUtil.requireNonEmpty(allowedValuesCollection.getValues());
58      this.allowedValuesCollection = allowedValuesCollection;
59    }
60  
61    @Override
62    public void resolveSubSchemas(JsonGenerationState state) {
63      // do nothing
64    }
65  
66    @NonNull
67    protected IValuedDefinition getDefinition() {
68      return definition;
69    }
70  
71    @NonNull
72    protected AllowedValueCollection getAllowedValuesCollection() {
73      return allowedValuesCollection;
74    }
75  
76    @Override
77    public boolean isInline(JsonGenerationState state) {
78      return state.isInline(getDefinition());
79    }
80  
81    @Override
82    protected String generateDefinitionName(JsonGenerationState state) {
83      return state.getTypeNameForDefinition(definition, "Value");
84    }
85  
86    @Override
87    public void generateSchema(JsonGenerationState state, ObjectNode obj) {
88      // generate a restriction on the built-in type for the enumerated values
89      ArrayNode enumArray = JsonNodeFactory.instance.arrayNode();
90  
91      AllowedValueCollection allowedValuesCollection = getAllowedValuesCollection();
92      for (IAllowedValue allowedValue : allowedValuesCollection.getValues()) {
93        switch (getDefinition().getJavaTypeAdapter().getJsonRawType()) {
94        case STRING:
95          enumArray.add(allowedValue.getValue());
96          break;
97        case BOOLEAN:
98          enumArray.add(Boolean.parseBoolean(allowedValue.getValue()));
99          break;
100       case INTEGER:
101         enumArray.add(new BigInteger(allowedValue.getValue())); // NOPMD unavoidable
102         break;
103       case NUMBER:
104         enumArray.add(new BigDecimal(allowedValue.getValue(), MathContext.DECIMAL64)); // NOPMD unavoidable
105         break;
106       default:
107         throw new UnsupportedOperationException(getDefinition().getJavaTypeAdapter().getJsonRawType().toString());
108       }
109     }
110     // get schema for the built-in type
111     IJsonSchema dataTypeSchema = state.getSchema(getDefinition().getJavaTypeAdapter());
112 
113     // if other values are allowed, we need to make a union of the restriction type
114     // and the base
115     // built-in type
116     ArrayNode ofArray;
117     if (allowedValuesCollection.isClosed()) {
118       // this restriction is allOf, since both must match
119       ofArray = obj.putArray("allOf");
120     } else {
121       // this restriction is anyOf, since any can match
122       ofArray = obj.putArray("anyOf");
123     }
124 
125     // add the data type reference
126     dataTypeSchema.generateSchemaOrRef(state, ObjectUtils.notNull(ofArray.addObject()));
127     // add the enumeration
128     ofArray.addObject()
129         .set("enum", enumArray);
130   }
131 }